I am trying to write a code that asks for the user first and last name and concatenate the function displaying the user's lastname and then firstname example:
bob allen // first and then last name
allen, bob // last and then first name using the concatenate function:
This is what I done so far, this is my first time using this function my book C for dummies doesn't explain much for me or my book on-line

#include <stdio.h>
#include <string.h>

 main()
{
        char str1[40];
        char str2[40];
 int name;
 
 
 printf("Please enter your first name: ")
 scanf("%d", &first);
 printf("\nPlease enter your last name: ")
 scanf("%d", &last);
 
 printf("\n%s\n", strcat(str2, str1));
 getchar();
 return 0;
 

 
 
 
}

But I was able to get this to work:

#include <stdio.h>
#include <string.h>
 
main()
 
{
       char str1[25]="James";
       char str2[]="Ballard";
 
       printf("\n%s\n", strcat(str2, str1));
       getchar();
       return 0;
}

I got this one off of an on-line book example.

Recommended Answers

All 7 Replies

The first one will not compile because of these:
the printf statements need to have a ; at the end.
And the scanf statements need to be changed with the proper declared arrays. Instead of first needs to be str1 and instead of last needs to be str2. When you use scanf with arrays you don't the & operator. Also the "%d" in both functions need to be changed to "%s".

The second has the problem that you declared str1 of size 25 and str2 of size 8 by virtue of leaving empty the inside of the []. Ballard is only 7 plus the null terminator.
Then you want to add str1 to str2. There's not room there, declared to hold that.

Still neither one will give you exactly what you are asking for.
considere this:
>bob allen // first and then last name
The array that is going to hold the concatenate string needs to be big enough.

strcat( bob_array, " " ); /* you want space between name and last name */
strcat( bob_array, allen_array );

>allen, bob // last and then first name using the concatenate function:

Being allen array big enough you could do this:

strcat(allen_array, ", "); /* , and space in between lastname and name */
strcat(allen_array, bob_array);

It is possible to use a different array designated for the concatenation, in that way the input entered by the user is not modified. The only difference is that you need to use strcpy the first time, if you haven't initialized this array.

strcpy(temp_array, bob_array);
strcat(temp_array, " ");
strcat(temp_array, allen_array);

Here is the finished product, this compiled and worked. Is there any improvements to be made any suggestions are fine with me.

//created by James Ballard
//This program was created on June 8, 2007
//Purpose of this program is to Create a program that will ask for the User’s First Name and Last Name
//and use the concatenate function from your text-book to make the output display the name last and then firt 
 
 
 
#include <stdio.h>
#include <string.h>
main()
{
char str1[40];    //declares a character array size of 40 for first string
char str2[40];    //declares a character array size of 40 for first string
 
printf("Please enter your first name: ");     //prompts user for first name
scanf("%s", str1);                            //Identifies first string as first character array
printf("\nPlease enter your last name: ");    //prompts user for last name
scanf("%s", str2);                            //Identifies second string as last character array
printf("\n%s\n", strcat(str2, str1));         //This strings the character arrays together with last name and then first name
getchar();                                    //This gets the char to display
return 0;                                     //This declares the entire function without any errors.
}
Member Avatar for iamthwee

possibly Something to think about.

Not looked at the link properly though.

>Is there any improvements to be made any suggestions are fine with me

As a matter of fact, yes.
Since printf() doesn't contain a newline, you should flush the stdout like this:
fflush( stdout );

You chose to do the concatenation inside the printf function, stealing yourself the opportunity of addressing the issue of adding space, etc, between lastname and name.

I haven't compile or run your program, however I'm quite sure if the inputed data were your name `James' and `Ballard', the output woul be `BallardJames'.

I'll sugest that you stop using the function scanf() for reading strings. Here's why.

Btw, you thanked Salem for his help but you ignored his advice. Click that link he gave you and learn how to tag your code next time, or suffer his wrath, that no amount of thanks can block out.

Here is the finished product, this compiled and worked. Is there any improvements to be made any suggestions are fine with me.

Since you asked, read
this,
this
and this

>char str1[40];
>char str2[40];

You haven't solved anything; if the total number of characters of both names entered by the user exceeds 39, you'll blow your array boundaries when you strcat(str2, str1) . A better approach would be to create a third string that's at least 81 (39 + comma + space + 39 + terminating null character), and initialize it with nulls. Then strcat() in the names, but don't forget to include the comma and space.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.