Thread: To copy chars to a array perfectly

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    26

    Smile To copy chars to a array perfectly

    im new to c programming.i tried to get char inputs and assign them to char array.and then i printed that array.but when printing it shows another characters also after given chararcters.if can somebody help me to solve this il be very great ful to you.(pleas show me how to make char array from input)
    my code is

    Code:
    #include<stdio.h>
    #define MAX 100
    
    main()
    {
        char s[MAX];
        int c;
        int i=0;
        
    //assign to char array
         while((c=getchar())!='\n')
        {
        s[i]=c;
        i++;
        }
    
    //print char array
        int p=0;
        while(s[p]!='\0')
        {
            printf("%c",s[p]);
            p++;
        }
    }

    whe i give input as example i wrote in command prompt "rukshan"
    output is "rukshan#$^&*^"


    please help me
    thanx

    output is
    Last edited by Salem; 04-13-2011 at 12:19 AM. Reason: Added [code][/code] tags - learn to use them yourself

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You don't do a s[i] = '\0'; anywhere.

    So this
    while(s[p]!='\0')
    stops at some random later location, printing garbage as it goes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    26
    thank you mr salem .in c programming languages book by kerninigan and ritche it says every string is array a of characters.so in their example is like this(to find length)

    int strlen(char s[])
    {
    int i;
    while (s[i] != '\0')
    ++i;
    return i;
    }

    that means after a characters in char array they put '\0' at suffix.isn`t it? then ywhy here it is wrong please explain me.thank you very much.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    26
    i changed the code as follow.but it remains(i change '\0' to'\n').please some body help me.
    #include<stdio.h>
    #define MAX 100
    int strlen(char s[]);
    main()
    {
    char s[MAX];
    int c;
    int i=0;

    while((c=getchar())!='\n')
    {
    s[i]=c;
    i++;
    }


    int p=0;
    while(s[p]!='\n')
    {
    printf("%c",s[p]);
    p++;
    }




    }

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    26
    finally i found it.its about <string.h>.thank you mr salem for the help.

  6. #6
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79
    You're gonna gonna find yourself in big trouble later.
    As Salem said, you've got to put a '\0' in there. '\0' is a terminator for strings in C. You may end the printing at any character you want, but so see to it that all strings ends with '\0'.
    Code:
    [...]
    //assign to char array
        while((c=getchar())!='\n')
        {
        s[i]=c;
        i++;
        }
    
        s[i] = '\0';
    [...]

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    26
    thank you very much MR.fader.i got it.thanx again

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Quote Originally Posted by rukshan View Post
    book by kerninigan and ritche it says every string is array a of characters.
    No.

    Hope you got it straight that a string is an array of chars with a '\0' at the end.

    No '\0' = no string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-23-2010, 02:19 PM
  2. Replies: 5
    Last Post: 12-10-2007, 11:36 PM
  3. cin to array of chars
    By cdonlan in forum C++ Programming
    Replies: 1
    Last Post: 02-25-2005, 01:00 PM
  4. Array of pointers (chars)
    By Vber in forum C Programming
    Replies: 5
    Last Post: 02-08-2003, 04:29 AM
  5. How to encrypt a CD perfectly?
    By Yin in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 03-13-2002, 09:02 AM