strcat

This is a discussion on strcat within the C Programming forums, part of the General Programming Boards category; I am trying to get a better understanding of c strings I always seem to see myselft missing a big ...

  1. #1
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    strcat

    I am trying to get a better understanding of c strings I always seem to see myselft missing a big gap everytime I program something I am trying to replace strcat(for a better understanding) The code compiles and runs but I think it hits and infinite loop
    Code:
    #include <stdio.h>
    
    int main(void){
            char fname[256];
            char lname[256];
            int x=0,y=0;
            printf("Enter your first name: ");
            fflush(stdout);
            fgets(fname,sizeof fname,stdin);
            printf("Enter your last name: ");
            fflush(stdout);
            fgets(lname,sizeof lname,stdin);
            while(fname[x]!='\n'){
                    x++;
            }
            fname[x]=' ';
            x++;
            printf("%s\n",fname);
            while(lname[y]!='\n'){
                    fname[x]=lname[y];
            }
            printf("%s\n",fname);
            return 0;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >I think it hits and infinite loop

    Unless you entered no last name, that is likely.
    Code:
    while(lname[y]!='\n'){
       fname[x++]=lname[y++];
    }
    fname[x]='\0';
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An interesting question about strcat
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 07-07-2009, 12:59 PM
  2. strcat the inside of a structure
    By ebullient in forum C Programming
    Replies: 3
    Last Post: 12-09-2005, 04:58 PM
  3. strcat and a char
    By jjacobweston in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2005, 04:10 PM
  4. strcat makes program crash
    By imoy in forum C++ Programming
    Replies: 4
    Last Post: 05-09-2002, 02:41 PM
  5. Removing 'strcat' ed string.
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-13-2001, 11:09 PM

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