6
Contributors
5
Replies
7
Views
6 Years
Discussion Span
Last Post by vinitmittal2008
0

to test for two blanks in the middle of a string, use a byte compare. Don't use a string compare because it expects a null at the end of two blanks.

0

try this.I got correct ans
#include<stdio.h>
#include<string.h>
void main()
{
char x[25],a[10];
int i,p=0,l;
clrscr();
printf("enter name\n");
gets(x);
l=strlen(x);
for(i=0;i<l;i++)
if(x==32)
p++;
printf("space %d",p);
getch();
}

0

That is some of the worst code I've seen! 16 lines with the following problems:
1) void main() -- main is ALWAYS an int.
2) clrscr() -- no current compiler has this function and it's a bad idea to use it in the first place.
3) gets() -- the most dangerous input function ever written, can easily cause your program to crash
4) getch() -- another function that should not be used, very non-standard
5) no conio.h which is required for getch() and clrscr()
6) doesn't really find multiple (consecutive) spaces, only counts spaces
7) no CODE Tags

IOW, don't bother with the above post...

0
/* program tht replaces two or more consecutive blanks in a string by a single blank.. */
# include<stdio.h>
int main(){
int i;
char s[50],*p,*q;
printf("\n\n :");
gets(s);
 for(i=0;s[i];i++){
  if(s[i]==32){
     p=&s[i+1];
    while(*p && *p==32){
	  q=p;
	  while(*q){
	  *q=*(q+1);
	  q++;
	  }
     }
  }
 }
printf("\n\n :%s",s);
return 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.