Answer to Question #25582 in C++ for Sam

Question #25582
Please write code on C++ for following problem.
In the given string replace each sequence containing more than one space by one space.
For example: the string “a bcd ef . gh 90” - “a bcd ef . gh 90”
1
Expert's answer
2013-03-05T07:43:42-0500
#include <stdio.h>
#include <string>

int main()
{
char s[256];
gets(s); // read input string
std::string input = s, output;

for (int i = 0; i < input.length(); i++)
& if ( input[i] != ' ' || input[i+1] != ' ' ) // if current or next char
output += input[i]; & & // is not space copy to output

printf("%s\n",output.c_str()); // write output string
getchar();
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS