Thread: Array length error using strlen()

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    22

    Array length error using strlen()

    Greetings.

    I seek assistance with gaining an understanding of the following code. Below I submit a sample run, followed by the code itself.

    Brief Synopsis: main() passes a char array reference to function getWord() which receives stdin from user. main() then attempts to print the string.

    Error: strlen() is unable to ascertain the correct length of the string in main().

    Code:
    $ ./a.exe
    hello
    len = 7     //Incorrect. Proper length of 'hello' is 5
    hello
    P              // junk characters


    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 256
    
    int getWord(char * newString, int size);
    
    int main(){
      int j;
      int len;
      char get[SIZE];
      
      getWord(get, SIZE);
      len = strlen(get);
      printf("len = %d\n", len);
    
      for(j=0; j<len; j++){
         printf("%c", get[j]);
      }
    
    
    
    int getWord(char * newString, int size){
      char a;
      int i = 0;
    
      while(i < size){
        i++;
        a = getchar();
        if(a != '\n' && a != ' '){
          *newString = a;
        }
        else if(a == '\n'){
          *newString = '\n';
          break;
        }
        else return 0;
        newString += 1;
      }
        return 1;
    }


    Look forward to a prompt response.

    Best regards,
    wirefree101

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You never null terminate your string.
    *newString = '\n';
    should probably be:
    *newString = '\0';
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    2
    You need another brace to close main. You store the newline in newstring, which strlen is going to count as a character. Could also be getting a carriage return, which would make the count 2 more than the number of letters.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    22
    Quote Originally Posted by bithub View Post
    You never null terminate your string.
    *newString = '\n';
    should probably be:
    *newString = '\0';
    Greatly appreciate that, bithub.

    Best regards,
    wirefree101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  2. length of char array
    By taurus in forum C Programming
    Replies: 41
    Last Post: 09-22-2007, 05:33 PM
  3. array length question
    By cs_newbie in forum C Programming
    Replies: 24
    Last Post: 10-12-2006, 09:10 AM
  4. length of an array
    By chrismiceli in forum C Programming
    Replies: 8
    Last Post: 02-27-2003, 11:18 PM
  5. 2-dem array
    By alika in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 10:59 AM