Question

In: Computer Science

I need to write a C++ program that appends "not" into the string without using the...

I need to write a C++ program that appends "not" into the string without using the append method or any standard libraries. It should return the string if there isn't an "is" in it.

Examples:

is is = is not is not

This is me = This is not me

What is yellow? = What is not yellow?

The sky is pink = The sky is not pink

isis = isis

What happened to you? = What happened to you?

Solutions

Expert Solution

C++ Code :

#include<iostream>
using namespace std;

// this method appends "not" into the string without using the append method or any standard libraries.
// this method returns the string if there isn't an "is" in it.
// this method accepts a parameter of user input string
string solve(string s)
{
   string res = "";           // this is the resultant string
   int n = (int)s.size();               // getting the size of the input string
  
   // if the size of the input string is 1, then directly return it
   if(n==1)          
       return s;
      
   // if the size of the input string is 2, then check whether it is "is" or not
   else if(n==2)
   {
       // if the input string is "is", then simply add "is not" to the resultant string and return it
       if(s[0]=='i' && s[1]=='s')
       {
           res += "is not";
           return res;
       }
      
       // if the input string is not "is", then simply return it directly
       else
           return s;
   }
  
   // if the size of the input string is greater than 2
   else
   {
       for(int i=0;i<n;i++)
       {
           // this is for first index of the input string
           if(i==0)
           {
               // if the first word of the input string is "is"
               if(s[i]=='i' && s[i+1]=='s' && s[i+2]==' ')
               {
                   res += "is not";
                   i++;
               }
              
               // else simply add the current character to the resultant string
               else
                   res += s[i];
           }
          
           // this is for (n-2)th index of the input string
           else if(i==n-2)
           {
               // checking if the last word of the input string is "is"
               if(s[i-1]==' ' && s[i]=='i' && s[i+1]=='s')
               {
                   res += "is not";
                   i++;
               }
              
               // else simply add the current character to the resultant string
               else
                   res += s[i];
           }
          
           // this is for rest of the character of the input string
           else
           {
               // if the current index word is "is", then simply add "is not" to the resultant string
               if(s[i-1] == ' ' && s[i]=='i' && s[i+1]=='s' && s[i+2]==' ')
               {
                   res += "is not";
                   i+=2;
               }
              
               // else simply add the current character to the resultant string
               res += s[i];
           }
       }
      
       // returning the resultant string from this method
       return res;
   }
}

int main()
{
   string s;           // this variable will take user input of string
   cout<<"Enter a string : ";               // asking for the user input of string
   getline(cin,s);                   // taking input from the user
  
   string result = solve(s);               // calling solve() for getting the resultant string or expected output and storing to the result variable
  
   cout<<"Resultant string is : "<<result<<"\n";               // printing the resultant string
}

Output :


Related Solutions

For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
How would I write a program for C++ that checks if an entered string is an...
How would I write a program for C++ that checks if an entered string is an accepted polynomial and if it is, outputs its big-Oh notation? Accepted polynomials can have 3 terms minimum and no decimals in the exponents.
I need the code for a C++ program that creates an array of 5000 String objects...
I need the code for a C++ program that creates an array of 5000 String objects that will store each word from a text file. The program will read in each word from a file, and store the first 5000 words in the array. The text file should be read in from the command line.
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
In c++, using stack structure, write a program that will take a sequence of characters (string)...
In c++, using stack structure, write a program that will take a sequence of characters (string) and determine whether it is a palindrome. Use the linked version of the stack.
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
Write a C++ program using produces Huffman code for a string of text entered by the...
Write a C++ program using produces Huffman code for a string of text entered by the user. Must accept all ASCII characters.
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
Write a C program that will read a character string and then encrypt the string based...
Write a C program that will read a character string and then encrypt the string based on one of the 3 different encryption methods. The type of encryption is to be selected by the user. Encryption method 1: Swapping by position. Characters in the array are swapped with the opposite characters based on their position in the string. Example: Input string – apple. Encrypted string – elppa Method: The first character ‘a’ and the last character ‘e’ – swap their...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT