Introduction:
This C program concatenates two strings provided by the user. It reads two separate strings and combines them into a single string while demonstrating fundamental string manipulation techniques. String concatenation is a common operation in programming, often used in data formatting, user input processing, and building dynamic messages.
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100], result[200];
printf("Enter the first string: ");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = 0; // Remove newline character
printf("Enter the second string: ");
fgets(str2, sizeof(str2), stdin);
str2[strcspn(str2, "\n")] = 0; // Remove newline character
strcpy(result, str1); // Initialize result with the first string
strcat(result, str2); // Concatenate the second string
printf("Concatenated string: %s\n", result);
return 0;
}
- Include Header Files:
#include <stdio.h>
: This header file is included for standard input/output functions likeprintf
andfgets
.#include <string.h>
: This header file is included for string manipulation functions likestrcat
andstrcpy
.
- Declare String Arrays:
char str1[100], str2[100], result[200];
: Three character arrays are declared:str1
: To hold the first input string.str2
: To hold the second input string.result
: To hold the concatenated result of both strings. It is sized to accommodate both strings plus a null terminator.
- Input the First String:
printf("Enter the first string: ");
: Prompts the user for input.fgets(str1, sizeof(str1), stdin);
: Reads a line of text, including spaces, and stores it instr1
.str1[strcspn(str1, "\n")] = 0;
: This line removes the newline character from the input.strcspn
finds the length of the string until the newline, and this is set to0
to terminate the string there.
- Input the Second String:
- Similar to the first string, the program prompts for and reads the second string, storing it in
str2
and removing any newline character.
- Similar to the first string, the program prompts for and reads the second string, storing it in
- Copy the First String to Result:
strcpy(result, str1);
: Copies the contents ofstr1
intoresult
. This initializesresult
with the first string.
- Concatenate the Second String:
strcat(result, str2);
: Appends the contents ofstr2
toresult
. After this operation,result
contains bothstr1
andstr2
.
- Display the Concatenated Result:
printf("Concatenated string: %s\n", result);
: Prints the concatenated string to the console.
- Return Statement:
return 0;
: Indicates successful completion of the program.
Input/Output Block:
Input:
- The user is prompted to enter two strings to be concatenated.
Example Input:
Enter the first string: Hello
Enter the second string: World!
Output:
- The program outputs the combined result of the two strings.
Example Output:
Concatenated string: HelloWorld!
Conclusion:
The string concatenation program effectively demonstrates how to combine two strings in C using manual string manipulation techniques. By utilizing pointer arithmetic and loop constructs, the program illustrates a clear method for appending one string to another. This capability is essential in various programming tasks, such as building user interfaces, generating dynamic content, and processing textual data. Overall, the program serves as a practical example of string handling in C, reinforcing fundamental programming concepts and techniques.
I抳e read several good stuff here. Definitely worth bookmarking for revisiting. I wonder how much effort you put to make such a great informative site.
Great items from you, man. I have remember your stuff previous to and you’re just extremely wonderful. I actually like what you have got right here, certainly like what you are saying and the best way during which you assert it. You are making it entertaining and you continue to care for to stay it smart. I cant wait to learn far more from you. That is really a great website.
Sweet blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Thank you
Thank you for your help and this post. It’s been great. http://www.kayswell.com
I enjoyed reading your piece and it provided me with a lot of value. http://www.kayswell.com
Your articles are extremely helpful to me. Please provide more information! http://www.ifashionstyles.com
The articles you write help me a lot and I like the topic http://www.hairstylesvip.com
You helped me a lot by posting this article and I love what I’m learning. http://www.goodartdesign.com
Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You obviously know what youre talking about, why throw away your intelligence on just posting videos to your weblog when you could be giving us something informative to read?
I want to thank you for your assistance and this post. It’s been great. http://www.kayswell.com
How can I find out more about it? http://www.kayswell.com
Thank you for providing me with these article examples. May I ask you a question? http://www.kayswell.com
Thanks for posting. I really enjoyed reading it, especially because it addressed my problem. http://www.kayswell.com It helped me a lot and I hope it will help others too.
You helped me a lot by posting this article and I love what I’m learning. http://www.hairstylesvip.com
The articles you write help me a lot and I like the topic http://www.kayswell.com
Thanks for posting. I really enjoyed reading it, especially because it addressed my problem. http://www.kayswell.com It helped me a lot and I hope it will help others too.
Thank you for writing this article. I appreciate the subject too. http://www.ifashionstyles.com
Thank you for your help and this post. It’s been great. http://www.kayswell.com
I enjoyed reading your piece and it provided me with a lot of value. http://www.ifashionstyles.com
Thank you for your help and this post. It’s been great. http://www.ifashionstyles.com