Skill Rack
Daily Challenge Solution
Date : 04/04/2019
ProgramID- 8462
Replace Additional Space
A string S with space is passed as the input to the program. The program must print the words in S separated by exactly one space character as the output and additional space characters must be removed if present.Boundary Condition(s):
1 <= Length of S <= 1000
Input Format:
The first line contains the string S.
Output Format:
The first line contains the words separated by space.
Example Input/Output 1:
Input:
Practice makes perfect
Output:
Practice makes perfect
Explanation:
The additional spaces between words are removed so there is exactly 1 space between every two words.
Example Input/Output 2:
Input:
The process needed 30 minutes to finish
Output:
The process needed 30 minutes to finish
Solution :
-----------------------------------------------------------------------------------
#include<stdio.h>
#include <stdlib.h>
int main()
{
char text[1000],blank[1000];
int c=0,d=0;
scanf("%[^\n]%*c",text);
while(text[c]!=NULL)
{
if(text[c]==' ')
{
int temp=c+1;
if(text[temp]!=NULL)
{
while(text[temp]==' '&&text[temp]!=NULL)
{
if(text[temp]==' ')
{
c++;
}
temp++;
}
}
}
blank[d]=text[c];
c++;
d++;
}
blank[d]=NULL;
printf("%s",blank);
return 0;
}
-----------------------------------------------------------------------------------
No comments:
Post a Comment