Thread: braking a string

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    22

    braking a string

    there we go. what is wrong with it please?

    Get a string if it an empty char replace with \n

    Code:
    void main()
    {
        int i;
        char x='\n';
        char first[256], second[256];
    
        printf("enter a string: \n");
        fgets(first,sizeof(first), stdin);
    
        for(i=0; i<sizeof(first); i++)
        {
            if(first[i] == ' ')
            {
            second[i] = x;
            }
        second[i]=first[i];
    
    
        }
     second[i]='\0';
    
    printf("second string is: %s \n", second);
      }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ph3s View Post
    there we go. what is wrong with it please?
    The type of main is int, not void.

    You are missing an "else" or "continue" in your loop. Without that, the newline gets overwritten.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  2. Replies: 7
    Last Post: 06-16-2011, 06:21 PM
  3. Replies: 12
    Last Post: 03-07-2011, 01:24 AM
  4. Replies: 5
    Last Post: 05-09-2010, 10:58 AM
  5. Replies: 1
    Last Post: 10-31-2005, 11:36 AM