Question

Write a program that can remove spaces from an input string, find the indexes of a...

Write a program that can remove spaces from an input string, find the indexes of a character within the string and replace that character with another character.

Here is an example input:

I am an input string
a b

The first line, "I am an input string" represents the input string. Please put it into a string variable using getline. In the second line "a b", a is the character that needs to be located within the input string, and b is the character that will replace a in the final output.

The output of the program for the above example is

No space output: Iamaninputstring
Searching for a
Found a at index 2
Found a at index 5
Replace finished, resulting string: I bm bn input string

The first step is to make a new string, then use this string to store the input string, but without spaces Then print this string in the format:

No space output: [no space string here]

Secondly, print "Searching for [char]" where [char] is the character that needs to be located within the input string. Then, using the original input string with spaces, for each match, output "Found [char] at index [index]" where [index] is the index that the character has been found at within the input string. Then replace that character within the string (b in the above example input). After all the replacements are finished, output

Replace finished, resulting string: [modified string here]

However, if no matches are found, instead of outputting the replaced results, output:

[char] was not found

Here is an example input and output with no matches input:

I am an input string
z b

output

No space output: Iamaninputstring
Searching for z
z was not found

#include <iostream>
#include <string>

using namespace std;

int main()
{

string str;

string str_no_space = "";

char to_find;

char to_replace;


return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PLEASE REFER BELOW CODE

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str; //input string
    string str_no_space = ""; //output string with no space
    char to_find; //char to find
    char to_replace; //char to replace

    std::getline (std::cin,str); //reading line from console
    cin>>to_find>>to_replace; //reading characters from console

    //for each char in input string we are checking space and if found we ignore and copy all chars
    //to output string
    for(unsigned int i = 0; i < str.length(); i++)
    {
        if(str[i] != ' ')
        {
            str_no_space += str[i];
        }
    }
    //display output in format specified
    cout<<"No space output: "<<str_no_space<<endl;
    cout<<"Searching for "<<to_find<<endl;
    bool flag = false;
    //searching char in input string and printing it's position or otherwise display not found
    for(unsigned int i = 0; i < str.length(); i++)
    {
        if(str[i] == to_find)
        {
            cout<<"Found "<<to_find<<" at index "<<i<<endl;
            flag = true;
        }

    }
    if(!flag)
        cout<<to_find<<" was not found"<<endl;


        //replacing find char to replace char
    for(unsigned int i = 0; i < str_no_space.length(); i++)
    {
        if(str[i] == to_find)
            str[i] = to_replace;
    }
    cout<<"Replace finished, resulting string: "<<str<<endl;
    return 0;
}


PLEASE REFER BELOW OUTPUT

Add a comment
Know the answer?
Add Answer to:
Write a program that can remove spaces from an input string, find the indexes of a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • Write a program that prompts the user to input a string. The program then uses the...

    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. #include <iostream> #include <string> using namespace std; void removeVowels(string& str); bool isVowel(char ch);...

  • Write a full MIPS that behaves exactly like the following C program. The following C code...

    Write a full MIPS that behaves exactly like the following C program. The following C code shows the proposed algorithm. The string is traversed with two indices, called old_index and new_index, where the latter always takes a value less or equal to the former. When a non-space character is found, the character at position old_index is copied to position new_index, and both indices are incremented. When a space is found, the current character is not copied, and only old_index is...

  • LANGUAGE = C i. Write a program that takes int numbers from user until user gives...

    LANGUAGE = C i. Write a program that takes int numbers from user until user gives a sentinel value (loop terminating condition). Sort the numbers in ascending order using Insertion sort method. Sorting part should be done in different function named insertionSort(). Your code should count the number of swaps required to sort the numbers. Print the sorted list and total number of swaps that was required to sort those numbers. (4 marks) ii. In this part take another number...

  • Write a C program to “de-vowel” an input string. Assume that the user provides input containing...

    Write a C program to “de-vowel” an input string. Assume that the user provides input containing only the characters a through z (i.e., all lowercase letters). Your program should create an output string that deletes all vowels from the input string, pushing the letters together to fill any gaps. For example, given the input “theturtleandthehare” your code should print out “thtrtlndthhr”. Your program should create an output string from the input string, before printing its output. Sample Input: Enter a...

  • This program will require you to create a basic parser for an input string that is...

    This program will require you to create a basic parser for an input string that is somewhat like Python. Python has an older parser module and a newer ast module to manage pure Python syntax, but they won't be useful here. The program will read a data string, identify the control characters, and print out the overall structure of the string. Here's an example Input String: [{1}] Output: Number inside a dictionary inside a list Here are some basic rules...

  • ​​​​​​public static int countCharacter(String str, char c) { // This recursive method takes a String and...

    ​​​​​​public static int countCharacter(String str, char c) { // This recursive method takes a String and a char as parameters and // returns the number of times the char appears in the String. You may // use the function charAt(int i) described below to test if a single // character of the input String matches the input char. // For example, countCharacter(“bobbie”, ‘b’) would return back 3, while // countCharacter(“xyzzy”, ‘y’) would return back 2. // Must be a RECURSIVE...

  • #include <iostream>   #include <string>   using namespace std;      void get_user_string(string *);   string convert_to_dash(String* );   int_search_and_replace(char, string,...

    #include <iostream>   #include <string>   using namespace std;      void get_user_string(string *);   string convert_to_dash(String* );   int_search_and_replace(char, string, string &);       int main (){    string s;    cout << "Enter a string:" << endl;    get_user_string(&s);        string dash_version = convert_to_dash(&s)    if ( dash_version != 32){    &s.push_back('-');    } Here is an example operation of the completed program: Please enter a string: the weather is great! The dash-version of your string is: Please tell me the char that...

  • Using the Arduino IDE, write a program for an Arduino board that will perform the following three...

    Using the Arduino IDE, write a program for an Arduino board that will perform the following three tasks: 1. Input: Prompt the user to enter a character string. Read the character string from your computer through the Arduino's serial interface. 2. Processing: Convert lowercase characters to uppercase and uppercase characters to lowercase, and 3. Output: Return the processed string through the serial output and display it on your computer screen. Example: If the input character string is, “Here I am”,...

  • PLEASE WRITE SIMPLE JAVA PROGRAM AND ALSO EXPLAIN THE LOGIC. Given a string, print the first...

    PLEASE WRITE SIMPLE JAVA PROGRAM AND ALSO EXPLAIN THE LOGIC. Given a string, print the first word which has its reverse word further ahead in the string. Input Format: A string of space separated words, ending with a 's'. Conditions ; Each string ends with a $ character. All characters in the string are either spaces. $ or lower case English alphabets, Output Format : In a single line pent either . The first word from the start of the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT