Like Tree1Likes
  • 1 Post By Matticus

Strange problem with input in a C programm with Strings.

This is a discussion on Strange problem with input in a C programm with Strings. within the C Programming forums, part of the General Programming Boards category; Hello to all.... I have a problem in my code. Well I have done a program (an exercise) that it ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    296

    Strange problem with input in a C programm with Strings.

    Hello to all.... I have a problem in my code. Well I have done a program (an exercise) that it takes your name (first + last) for instance you may have something like :

    Code:
     Enter your name: Bill Gates
    And the program will produce the following output :

    Code:
      Gates , B.
    The code is :

    Code:
    #include <stdio.h>
    #include <string.h>  // strcat()
    #include <ctype.h>
    #define STR_LEN 80
    void reverse_name(char *);
    void read_line( char * , int );
     
    int main(void)
    {
        char name[STR_LEN+1] = {'\0'};
        
        printf("Enter your name: ");
        read_line(name , STR_LEN);
        puts(name);
        reverse_name(name);
        puts(name);
        
        return 0;
    }
    void read_line(char name[] , int n)
    {
        int ch , i=0;
        
        while( (ch = getchar()) != '\n' ) {
            
            if(i < n )
            name[i++] = ch;
        }
            
            name[i] = '\0';
    }
    
    void reverse_name(char *name)
    {
        char *p = name, first_letter[1+1];
        
        while (*p == ' ') p++; // Skip blanks in the beginning 
        first_letter[0] = *p;  // save the 1st character of the first name
        
        while (*p != ' ') p++; // 
        while (*p == ' ') p++; // skip blanks between first and last name 
        
        for (; *p != ' ' && *p; p++, name++)
            *name = *p;     // Copy last name into first name 
           
    	  *name = '\0';
      
        
        strcat(name, ", ");  // Gates, 
        strcat(name, first_letter);  // Gates, B
        strcat(name, "."); // Gates, B.
    
    }
    If I don't use the first call of puts function in my main... the result of the program is undefined for example....

    Code:
     printf("Enter your name: ");
        read_line(name , STR_LEN);
        ///puts(name);
        reverse_name(name);
        puts(name);
    Code:
     Gates , B ^ | ? .
    If I use the first call of puts ... the result is regular. Why??????????

    I can't understand.... where is the problem ?

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    puts will print until it finds a null terminator and then will append a newline automatically. I just run your code on windows and on Linux and was ok..
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,367
    When program execution reaches line 48, what character does "first_name[0]" contain?
    What about "first_name[1]"?
    Mr.Lnx likes this.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    296
    Quote Originally Posted by Matticus View Post
    When program execution reaches line 48, what character does "first_name[0]" contain?
    What about "first_name[1]"?
    The 'B' character. If you assume the example with the output of Bill Gates. Generally it will have the first character of the first name.

    And YES that is the solution of the problem. Array first_name has no a null terminator. The call of strcat may has the problem.... I will find it.

    p.s The array first_name was uninitialized . That was the problem... With the initialization strcat knows what will append at the end of name string....

    the characters 'B' , '\0' .

    Gates, B.
    Last edited by Mr.Lnx; 02-04-2013 at 10:40 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange bug with Input stream
    By Chazij in forum C++ Programming
    Replies: 12
    Last Post: 11-18-2008, 03:52 PM
  2. strange input error
    By thestien in forum Game Programming
    Replies: 0
    Last Post: 12-26-2007, 11:20 AM
  3. Strange behavior of Strings
    By shyam168 in forum C Programming
    Replies: 9
    Last Post: 03-27-2006, 06:41 AM
  4. Strange Direct Input Behaviour
    By Diamonds in forum Windows Programming
    Replies: 2
    Last Post: 06-23-2003, 04:34 PM
  5. problem with my srand programm
    By datainjector in forum C Programming
    Replies: 10
    Last Post: 07-15-2002, 07:15 AM

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