“C programming is the basic programming language. We can easily utilize this C programming language for developing different software such as Operating systems, databases, etc. It provides different data types like integer, float, and char. The string in C programming is the collection of characters that ends at the null value. We can easily declare, initialize and print the string in C programming. We can also copy the string in C. When we want to enter the string, then we have to write it in double quotes, and when we need to add a single character, then we utilize single quotes. We will show how to declare, initialize, print, and copy the string in C programming in this guide.”
Declaring a String in C
When we need to declare a string in C programming, then we must utilize the character array. First, we write the “char,” which is the data type, and then enter the name of the string. Also, the size of the string is given in the square brackets after putting the string name. Here in this image, you can notice that we place the syntax of the string here. If we want to enter the size of the string, then it will be added inside these square brackets, and also, we can declare this string without defining any string size here.
Initializing a String in C
We can also initialize the string in C programming, and it provides four distinct ways for initializing the string in C. All methods are shown below.
Initializing a string without mentioning the string size:
We can initialize a string without mentioning the size, as shown here. We didn’t add any size in numbers inside the square brackets. We simply place the empty square brackets and then initialize it with a string which is “My First String” here.
Initializing a string by mentioning the size:
We can also initialize a string in C by mentioning the size of the string in the square brackets, as shown. We have added “20” inside the square brackets, and this is the size of this string. After this, we initialized it with “My First String”. Now, this string is initialized here.
Initializing string by inserting character by character and mentioning the size:
The string is also initialized by assigning characters in this string. We have added “16” as the size of the string, and then we have added characters by putting each character inside single quotes, as shown. We have initialized this string with different characters. These characters are added in curly brackets, and also each character is enclosed in single quotes. We must add the “\0” at the end.
Initializing string by assigning characters without mentioning the size:
We didn’t add any size of the string here. We simply assign the character here without mentioning the size of the string. Also, we have added the Null character at the end. The string is also initialized in this way.
Printing a String in C
For printing the string in C programming, we can utilize the “printf” function, and the “<stdio.h>” header file helps in using this function in C.
Example # 1
The “printf” statement helps in printing the string which we have declared and initialized. First, we have included the header file, which helps in utilizing the input/output functions. Then, we called the “main()” function. After this, we declared and initialized the string here without mentioning any string size and assigned characters to this string. Below, we have utilized the “printf” statement for printing the string. This string is printed only when we pass the name of the string to this “printf” function.
We compiled this code by using the “F9” key, and then we executed this by hitting the “F10” key. After successful compilation and execution, we get this outcome which is also shown below. Here, the string which we have entered above is displayed.
Example # 2
We have included two header files here, and these are “stdio. h” and “string.h” because we have to utilize the functions of both header files. After this, we inserted the “int main()” function, and then we initialized a string with the name “my_str,” and we didn’t add any size of the string here. The string we used for initializing “my_str” is “String_Data”. We print this string by utilizing the “printf,” and then we initialize an integer “l” by putting the data type “int”. After this, we assigned the “strlen()” function to this “l” variable. We have passed the “my_str” string to this “strlen()” function, which will count the characters of the string and store it in the “l” variable. After this, we also print the length of this string below by using the same “printf()” method. Here, we are printing the size of the “my_str” string length, which is stored in the “l” variable.
First, it prints the string which we have added above, and then it counts the characters and displays the number of characters here that are present in the string, or we can say that it displays the length of the string here.
Copying a String in C
We can copy the string by using different methods in C programming. Here we are discussing two methods that help in copying the string in C. These methods are:
- By utilizing the “strcpy()” method.
- By utilizing the memcpy() method.
Example: By Utilizing the “strcpy()” Method
We include two header files in this example, and these are “stdio.h” and “string.h”. Then, we have called the “main()”. After this, we have initialized a string here with the name “my_str_1” and assigned “My String in C programming” to this “my_str_1”. Below, we have declared another string and didn’t initialize that string. The name of the second string is “my_str_2”. The size of both strings is “30” each. Then, we simply print the first string by using “printf,” and after printing this string, we use the “strcpy()” method here, which helps in copying the first string to the second string. Inside this “strcpy()” method, we have mentioned the name of the string where we want to copy the string, and then we place the name of the string which we want to copy. The “my_str_1” string is now copied to the “my_str_2” string. After this, we print the “my_str_2” where we have copied the string of the “my_str_1”.
The original, as well as the copied string, is displayed here. We have copied this string with the help of the “strcpy()” method in C programming.
Example: By Utilizing the “memcpy()” Method
Now, we are utilizing the “memcpy()” function for copying the string in C programming. We initialize the “s_1” string with “First string in C program is here”. Then, the “s_2” string is just declared after this. We put the size of both strings “50”.
After this, we print the “s_1” string and then use the “memcpy()” method in which we have added the name of the string where the string is copied and then the name of the string which is copied. Also, the “strlen()” method is added here, in which we have inserted the name of the first string. Now, we used “%s” in the “printf,” which helps in printing the string, and then write “s_2” in this “printf” method.
Both strings are shown in this outcome. The first string which is displayed here is the original string, and the second is the copied string.
Conclusion
The “string” in C programming is thoroughly discussed in this guide. We have explored how to declare, initialize, print, and copy the string in C programming. First, we have explained how to declare a string, and then we have explained four unique methods for initializing the string in C. We also explained and showed different examples in which we printed the string. We have explored two methods for copying the string in C in this guide. All the detail of the string in C programming is provided in this guide.