correct me please

This is a discussion on correct me please within the C Programming forums, part of the General Programming Boards category; hello guys, i had been working with one program where it eliminates one or more balnk space in a string ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    181

    Smile correct me please

    hello guys,
    i had been working with one program where it eliminates one or more balnk space in a string to single blank space. i have written a program which compiles and run success. and below is my code, can any one tell me how can i improve this program to work better and minimise the code or what i have writtin is more efficient. any comment would be appriciated.

    Code:
    /* program to replace a string of one or more blanks by a single blank */
    
    #include<stdio.h>
    
    int main()
    {
            char str[30],temp[20];
            int i,count=0,j=0;
            
            printf("Enter the value for the string\n");
            fgets(str,sizeof str, stdin);
            
            for(i=0;str[i];i++)
            {
                if(isspace(str[i]) && count==0)
                {
                temp[j++]=' ';
                count=1;
                continue;
                }    
                else if(!isspace(str[i]))
                {
               count=0;
               temp[j++]=str[i];
               }    
        }
        temp[j]='\0';
        fputs(temp,stdout);
        getchar();
        return 0;
    }
    s.s.harish

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    Code:
    str[30],temp[20]
    y make temp smaller than str? that will give you errors when you try to input a string, the modified version of which is longer than what temp can hold..say something like 25 characters.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void skipspace( char *in, char *out )
    {
        while( *in )
        {
            while( !isspace( ( *out++ = *in++ ) ) );
            while( isspace( *in ) ) in++;    
        }
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    Quzah thax very much that is brilliant way of doing it.

    cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux for GNU/Linux is not correct?
    By password636 in forum Linux Programming
    Replies: 8
    Last Post: 03-31-2009, 08:30 PM
  2. Is this correct : passing strings?
    By socket in forum C Programming
    Replies: 15
    Last Post: 11-25-2008, 01:03 PM
  3. correct order to insert new node?
    By campermama in forum C++ Programming
    Replies: 1
    Last Post: 06-16-2004, 07:51 PM
  4. Replies: 1
    Last Post: 05-26-2004, 12:58 AM
  5. Loop until enter correct value
    By jchanwh in forum C Programming
    Replies: 2
    Last Post: 11-27-2001, 12:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21