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”

Expert's answer

#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;
}
LATEST TUTORIALS
APPROVED BY CLIENTS