Thread: problem in my strcpy strcat function

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    12

    problem in my strcpy strcat function

    i m trying to write my own function of strcpy and strcat but 2nd character doesnt even getoutputed
    Code:
    #include <stdio.h>
    int main(void){
    	char name[21];
    	char surname[21];
    	char buffer[46];
    	int a,b;
    	printf("Please Enter your first name:  ");
    	scanf("%[^\n]",name);
        printf("Please Enter your surname name:  ");
    	scanf("%[^\n]",surname);
    	for(a=0;name[a]='\0';a++);
    	buffer[a]=name[a];
    	buffer[a]=' ';
    	for(b=0;surname[b]!='\0';b++,a++){
    	buffer[b]=surname[b];
    	}printf("Here is ur both value :  ",buffer);
    	return 0;}

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by genie View Post
    i m trying to write my own function of strcpy and strcat but 2nd character doesnt even getoutputed
    Code:
    #include <stdio.h>
    int main(void){
    	char name[21];
    	char surname[21];
    	char buffer[46];
    	int a,b;
    	printf("Please Enter your first name:  ");
    	scanf("%[^\n]",name);
        printf("Please Enter your surname name:  ");
    	scanf("%[^\n]",surname);
    	for(a=0;name[a]='\0';a++);  /*I wonder what you expect this to do? */
    	buffer[a]=name[a]; /*This line has no effect, since you immediately change it below*/
    	buffer[a]=' ';
    	for(b=0;surname[b]!='\0';b++,a++){
    	buffer[b]=surname[b];
    	}printf("Here is ur both value :  ",buffer);
    	return 0;}
    See comments.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    12
    for loop i want it just to get name like from variable 0 untill null terminator and in the buffer i want it to save the first name till i print both in the end

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well your for loop currently just sets name[0] to '\0', effectively wiping out the string, and that's it. Notice also that there are no statements in your for loop (the for loop goes from the end parenthesis to the next semicolon, which conveniently is the very next character). If you intend the next line to be in the for loop, you should probably fix that. Note also that you do not have a check in your for loop, but an assignment.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    12
    here almost done it but prints weird characters along
    Code:
    #include <stdio.h>
    int main(void){
    	char name[21];
    	char surname[21];
    	char buffer[46];
    	int a,b;
    	printf("Please Enter your first name:  ");
    	scanf("&#37;s",name);
        printf("Please Enter your surname name:  ");
    	scanf("%s",surname);
    	for(a=0;name[a]!='\0';a++)
    	buffer[a]=name[a];
    	buffer[a]=' ';
    	a++;
    	for(b=0;surname[b]!='\0';b++,a++){
    	buffer[a]=surname[b];
    	}printf("Here is ur both value : %s ",buffer);
    	return 0;}

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Strings don't end until there is a '\0'. If you don't put one in, the string won't end.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Ugh, what horrible looking code.

    Use consistent indentation to make your program flow very clear.
    Use newlines to separate functional steps.

    Code:
    #include <stdio.h>
    int main(void){
        char name[21];
        char surname[21];
        char buffer[46];
        int a,b;
        
        printf("Please Enter your first name:  ");
        scanf("&#37;s",name);
        printf("Please Enter your surname name:  ");
        scanf("%s",surname);
        
        for(a=0;name[a]!='\0';a++){
            buffer[a]=name[a];
        }
    
        buffer[a]=' ';
        a++;
    
        for(b=0;surname[b]!='\0';b++,a++){
            buffer[a]=surname[b];
        }
    
        printf("Here is ur both value : %s ",buffer);
        return 0;
    }
    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.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    12
    yah i got it fixed thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Noob problem - function call
    By Ultraman in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2006, 02:28 AM
  3. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM