Ignore the C++ stuff. Use underscores instead of spaces, read in the string usin
ID: 3774306 • Letter: I
Question
Ignore the C++ stuff. Use underscores instead of spaces, read in the string using scanf()
1. Read a string from keyboard.
2. Eliminate all the leading, trailing and repeating spaces.
3. Print the string.
Example:
Given a string like
_____abc____def________z______
(where _ is a space)
your program should print
abc_def_z
Since scanf() stops the input on any whitespace, use getline() method to read in the input.
Write the void noSpaces(char[]) function using assembly language and call it from your
C++ program. Print the result using your C++ program.
Explanation / Answer
#include <iostream>
#include <string>
int main ()
{
int stringSize,j=0;
std::string inputText;
std::cout << "Enter the Text: ";
std::getline (std::cin,inputText);
std::cout << "This is the text you Entered:" << inputText << "! ";
stringSize = inputText.size();
char myOutput[stringSize];
for (int i = 0; i < stringSize; i++) {
if (inputText[i] == ' ' && inputText[i+1] == ' ')
continue;
myOutput[j] = inputText[i];
if(myOutput[0]!=' ')
j++;
}
myOutput[j] =NULL;
std::cout << "This is the text you wanted to display:" << myOutput;
return 0;
}