Javatpoint Logo
Javatpoint Logo

Add String in C

Strings are an important data type in programming languages, representing sequences of characters. They are used in real-life applications, including text processing, data storage, and user input/output.

In text processing, strings are used to manipulate and analyze natural language text. This can include spell-checking, sentiment analysis, and language translation. Strings are also commonly used for data storage in databases and file systems. In user input/output, strings display messages to users and receive input from them. In addition to these specific uses, strings are often used in more general tasks such as data validation, data parsing, and string matching. They are also commonly used in web development to handle URLs, HTML, and JSON.

In summary, strings are a fundamental data type that is widely used in programming languages for a variety of real-life applications, including text processing, data storage, and user input/output. Their versatility and ubiquity make them an essential tool for any programmer to master.

Here is an example of C code that demonstrates different applications of strings, including text processing, data storage, and user input/output.

C Code

This program demonstrates text processing by using the strlen() function to find the length of the string "Hello, World!". It demonstrates data storage by using the scanf() function to store the user's name in the variable name, and then using it to greet the user. Finally, it demonstrates user input/output by using the fgets() function to read a line of text from the user and then printing it back to the screen.

Output

The length of str1 is: 13
What is your name? John
Hello, John!
Enter a line of text: Hi there
You entered: Hi there

Explanation:

It first prints the length of str1, which is "Hello, World!" which is 13. Then it prompts the user to input their name and the user inputs "John". So the program prints "Hello, John!" and then it prompts the user to enter a line of text and the user input "Hi there" and the program prints "You entered: Hi there"

Add String in C

In C, you can concatenate two strings using the strcat() function from the string.h library. The strcat() function takes two arguments: the destination string and the source string. The source string is added to the end of the destination string. Here is an example of how to use the strcat() function:

C Code

This program will concatenate the string "World!" to the end of the string "Hello, ", resulting in "Hello, World!" being printed to the screen.

Alternatively, you can use the "+" operator to concatenate two strings, but this is not an efficient method as it creates a new string variable that's the combination of two existing strings.

Output

char str1[] = "Hello, ";
char str2[] = "World!";
char str3[20];
strcpy(str3,str1);
strcat(str3,str2);
printf("%s\n", str3);

Explainaton:

This program will also concatenate the string "World!" to the end of the string "Hello, ", resulting in "Hello, World!" being printed to the screen. It is important to note that when using the strcat() function, the destination string should be large enough to hold the concatenated string, otherwise, it may cause buffer overflow. The above code is a C program that demonstrates how to concatenate two strings using the strcat() function from the string.h library.

The program first declares two char arrays, "str1" and "str2", with initial values of "Hello, " and "World!", respectively. It then uses the strcat() function to concatenate "str2" to the end of "str1", resulting in "Hello, World!" being stored in "str1". The program then uses the printf() function to print the contents of "str1" to the screen.

The alternative method is using "+" operator, but it creates a new variable and it's not efficient. The program also use strcpy() function to copy the contents of one string to another. It is important to keep in mind that destination string should be large enough to hold the concatenated string to avoid buffer overflow.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA