Write a program that accepts a string where multiple spaces
are given in between the words. Print the string ignoring
the multiple spaces.

Example:
Input: “ We.....Are....Student “ Note: one .=1 Space
Output: "We Are Student"

Answers were Sorted based on User's Feedback



Write a program that accepts a string where multiple spaces are given in between the words. Print ..

Answer / pramod

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
int i,j,k,len;
char a[100];

printf("Enter the String\n");
gets(a);
printf("The Given String is %s \n",a);
len=strlen(a);
for(i=0; i<len;i++)
{
if(a[i] == ' ')
{
if(a[i+1]==' ')
{
int j=i+1;
while(a[j]==' ')
{
j++;
len--;
}
for(k=i+1;a[j]!=NULL;)
{
a[k++]=a[j++];
}
}
}
}
a[i]='\0';
printf("The Resulted String is %s\n ",a);

}

Is This Answer Correct ?    5 Yes 1 No

Write a program that accepts a string where multiple spaces are given in between the words. Print ..

Answer / vignesh1988i

#include<stdio.h>
#include<conio.h>
void main()
{
char str[100],temp;
printf("enter the string :");
gets(str);
for(int i=0,j=0;str[j]!='\0';j++)
{
if(str[j]!=' ')
{
if(str[j+1]==' ')
{
temp=str[j];
str[j]=' ';
str[i]=temp;
i=i+2;
str[i-1]=' ';
}
else if(str[j+1]!=' ')
{
str[i]=str[j];
i++;
}
}
str[i]='\0';
printf("%s",str);
getch();
}

Is This Answer Correct ?    8 Yes 6 No

Write a program that accepts a string where multiple spaces are given in between the words. Print ..

Answer / vikramaditya.n

int main()
{
int i,j,k;
char a[100];

printf("Enter the String\n");
gets(a);

printf("The Given String is %s \n",a);

for(i=0; a[i] != '\0';i++)
{
if(a[i] == ' ')
{
for(k=i+1;a[k] != '\0';k++)
{
if(a[k] == ' ')
{
for(j=k;a[j] != '\0';j++)
{
a[j] = a[j+1];
}
a[j] ='\0';
}
}
}
}
printf("The Resulted String is %s\n ",a);

}

Is This Answer Correct ?    3 Yes 2 No

Write a program that accepts a string where multiple spaces are given in between the words. Print ..

Answer / santhi perumal

#include<stdio.h>
#include<conio.h>

int main()
{
int i,j,k;
char a[100];

printf("Enter the String\n");
gets(a);

printf("The Given String is %s \n",a);

for(i=0; a[i] != '\0';i++)
{
if(a[i] == ' ')
{
for(k=i+1;a[k] != '\0';k++)
{
if(a[i+1] == ' ')
{
for(j=i+1;a[j] != '\0';j++)
{
a[j] = a[j+1];
}
a[j] ='\0';
}
}
}
}
printf("The Resulted String is %s\n ",a);

}

Is This Answer Correct ?    3 Yes 4 No

Write a program that accepts a string where multiple spaces are given in between the words. Print ..

Answer / vadivel t

Hi all,

Its enough to have this much length of code below. And I
feel no need to have any temp variables(but i used one temp
pointer).

#include<stdio.h>
main()
{
char *p, *q, *q1;
p = (char *)malloc(100);
q = (char *)malloc(100);
q1 = q;
printf("ENTER THE SENTENCE WITH MULTIPLE SPACES: \n");
gets(p);
while(*p != '\0')
{
if(*p != ' ' || *(q -1) != ' ')
{
*q++ = *p++;
}
else
p++;
}
*q = '\0';
printf("%s", q1);
getch();
}

Is This Answer Correct ?    0 Yes 1 No

Write a program that accepts a string where multiple spaces are given in between the words. Print ..

Answer / jyotsna

/* Assume ' ' at the place of '.' */

#include<conio.h>
#include<stdio.h>
void main()
{
char *s="Hello...this...is...jyotsna";
int i=0;
clrscr();

while(*s!='\0')
{
if(*s!='.')
{
printf("%c",*s);
i=0;
s++;
}
else
{
while(*s=='.')
s++;

printf(".");
}
}
getch();
}

Is This Answer Correct ?    0 Yes 2 No

Post New Answer

More C Interview Questions

What is variables in c?

0 Answers  


Write a program to write a given string in maximum possibilities? i.e str[5]="reddy"; i.e we can write this string in 120 ways for that write a program

3 Answers   Subex,


what is the output of printf("%d",(scanf("%d",10));

10 Answers  


What is the difference between text files and binary files?

0 Answers  


What is the output of below code? main() { static int a=5; printf("%3d",a--); if(a) main(); }

1 Answers  






what are advantages of U D F?

1 Answers   Google,


Which is not valid in C a) class aClass{public:int x;}; b) /* A comment */ c) char x=12;

0 Answers  


Why static variable is used in c?

0 Answers  


Design a program using an array that lists even numbers and odd numbers separately from the 12 numbers supplied by a user.

8 Answers  


What is return in c programming?

0 Answers  


How are 16- and 32-bit numbers stored?

0 Answers  


What does typedef struct mean?

0 Answers  


Categories