Welcome folks, in this module we are going to talk about what is a string in C Programming, how to declare them, how to initialize them, and what are various strings function is commonly used.
So, let’s read the What is a string in C Programming module in order to gain deeper knowledge.
What is a string in C Programming?
A string in C programming is the linear sequence of the collection of characters that is terminated by a null character i.e., ‘\0’. It is also said to be the array of characters. The string in C is considered as a single data irrespective of the number of white spaces it contains.
Let us understand this by taking an example.
char greet[6] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’};
How to declare a string in C programming?
To declare a string in C programming, write the data type followed by the name and then the size mentioned in square brackets for example.
char test[6];
Here, we have declared the string named test of size 6.
How to initialize a string in C programming?
Now that we have declared the string in C programming, it’s time to initialize now with some value. We can simply achieve this by.
char greet[6] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’};
If the rule of array initialization is attained and followed, then the above statement can be resembled or written as follows:
char greet[ ] = “Hello”;
The memory representation of the above-declared string looks like this.
We don’t have to mention the null character every time we declare the string because the C compiler automatically terminates the strings with the null character. To hold the null character at the end of the array, the size of the array of characters holding the string is always one more than the size of the string.
#include <stdio.h> int main () { // string declared and initialized char greet[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; // The string is printed printf("The message is: %s\n", greet ); return 0; }
The output of the above code is:
The message is: Hello |
In the above example code, we have simply initialized the string named greet and then printed the same value.
How to take the input of the string from the user?
Users can simply enter the string with the help scanf( ) function, it takes the input until or unless it encounters the white space.
Let’s see one example.
#include <stdio.h> int main() { // string declared char name_person[30]; printf("Enter name of the person:"); // taking input through scanf( ) scanf("%s", name_person); // printing the name of the person printf("The entered name is: %s.", name_person); return 0; }
The output of the above program is.
Enter name of the person: string The entered name is: string |
In the above program, we have taken the input of the string from the user with the help of the scanf() function and then printed the same string as output in the screen.
How to read a full line or string in C programming?
In order to read the full line in C, we have to use fgets( ) function for reading the input and puts( ) function to display the same output.
Let’s see one example program.
#include <stdio.h> int main() { char name_person[40]; printf("Enter the name of the person:"); // reading the line as input fgets(name_person, sizeof(name_person), stdin); printf("The entered name is: "); // display the line as output puts(name_person); return 0; }
The output of the above program is.
Enter the name of the person: String in C programming The entered name is: String in C programming |
In the above program, we are reading a line or statement as input and then printing the same in the output screen. fgets( ) function is used for reading the line and in this one parameter is sizeof(name_person) which is 40 in this case that means you can’t enter more than 40 characters of line-in input.
What are all the functions supported by a string in C programming?
C supports a wide range of string functions which are used widely to perform different types of action in our program. Let’s see the list of string functions available in C.
Function name | Function purpose |
strcpy(s1,s2); | Copies the content of string s2 into string s1. |
strcat(s1,s2); | Concatenates the content of string s2 onto the end of the content of string s1. |
strlen(s1); | Returns the length or size of string s1. |
strcmp(s1,s2); | It is used to compare two strings i.e., it returns 0 if s1 and s2 are the same, less than 0 if s1<s2, greater than 0 if s1>s2. |
strchr(s1,ch1); | It points the pointer to the very first occurrence of a character named ch1 in string s1. |
strstr(s1,s2); | It returns a pointer to the first occurrence of string s2 in string s1. |
These are some of the string functions available in C with their purpose, that for which it is being used.
Let’s see one example of computing some of the above-mentioned functions in C.
#include <stdio.h> #include <string.h> int main () { char name[20] = "String"; char new_name[20] = "Programming"; char name1[20]; int len1; // copy name to name1 strcpy(name1, name); printf("The result is : %s\n", name1); // concatenates two string i.e., name + new_name strcat( name, new_name); printf("The result of strcat( name, new_name): %s\n", name); // computing the length of the string name len1 = strlen(name); printf("The result of strlen(name): %d\n", len1); return 0; }
The output of the above program is.
The result is: String The result of strcat( name, new_name): StringProgramming The result of strlen(name): 17 |
In this program, we have demonstrated the use of strcpy( ), strcat( ) and strlen( ) functions briefly.
I hope this module helped you all a lot and got the full knowledge about this wonderful concept of What is a string in C Programming. So keep learning, practising, and Happy coding!