#include<stdio.h>
int main()
{
    int i,j,k,c;   
    char s[1000];  //array to store the input string
    for(i=0;(c=getchar())!='.';++i)  //string to be ended with '.'
    {
        s[i]=c;                        
        for(j=0;j<=i;++j)               
        {
            if((s[j]==s[j-1])&&(s[j]=='\n' || s[j]=='\t' ||s[j]==' '))
            {
                j=j-1;
            }
        }
        for(k=0;k<=i;++k)
        printf("%c", s[k]);
    }
    return 0;
}

I am trying to create a program that will remove a consecutive occurrence of white space character. But the output is not as intended. Pls tell me where i went wrong ???? A little support will be helpful !!

get all input first using fgets() instead of that loop. Then to remove trailing white space start at the end of the string, search backward for the first non-white-space character and truncate the string at that point.

If you also want to remove instances of two or more white space characters inside the string, then use a pointer to find the beginning of the sequence, another pointer to find the end of the sequence, then call strcpy() to shift everything left, overwriting the excess white-space characters.

Not sure what you mean by this line - "remove a consecutive occurrence of white space character". Do you mean remove all the spaces in a line of text? Because if that's your intent then try looking up the "C squeeze algorithm".

get all input first using fgets() instead of that loop. Then to remove trailing white space start at the end of the string, search backward for the first non-white-space character and truncate the string at that point.

If you also want to remove instances of two or more white space characters inside the string, then use a pointer to find the beginning of the sequence, another pointer to find the end of the sequence, then call strcpy() to shift everything left, overwriting the excess white-space characters.

hi thx for reply, but i will be grateful if you suggest corrections in my existing program !!!!

Not sure what you mean by this line - "remove a consecutive occurrence of white space character". Do you mean remove all the spaces in a line of text? Because if that's your intent then try looking up the "C squeeze algorithm".

hi the problem is that if there are two or more continuous white space characters then only one remains and others are to be eliminated from the string. pls suggest me what's wrong with my existing program as its not giving the intended output.

hi thx for reply, but i will be grateful if you suggest corrections in my existing program !!!!

I did. My suggestion is to rewrite it. The code you posted is much more difficult than it needs to be. There's such a thing as over-thinking a problem.

k i will do that
thx for ur help !!!!!

#include<stdio.h>
int main()
{
    int i,j,k,c;   
    char s[1000];  //array to store the input string
    for(i=0;(c=getchar())!='.';++i)  //string to be ended with '.'
    {
        s[i]=c;                        
        for(j=0;j<=i;++j)               
        {
            if((s[j]==s[j-1])&&(s[j]=='\n' || s[j]=='\t' ||s[j]==' '))
            {
                j=j-1;
            }
        }
        for(k=0;k<=i;++k)
        printf("%c", s[k]);
    }
    return 0;
}

I am trying to create a program that will remove a consecutive occurrence of white space character. But the output is not as intended. Pls tell me where i went wrong ???? A little support will be helpful !!

may be u could get some help from my this program...

/* program tht replaces two or more consecutive blanks in a string by a single blank.. */
# include<stdio.h>
# include<conio.h>
void main(){
int i;
char s[50],*p,*q;
clrscr();
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);
getch();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.