Strings in C programming are an array of characters with a NULL character (''
) appended at the end index. Unlike character arrays, a c-string is a sequence of characters that are treated by the compiler as a single item. But just like an array, a string can be traversed along with its indices.
1. Declaration of Strings in C
Declaration of a String in C is exactly the same as in the case of an Array of characters.
char String_Name[No.of.elements];
Note: here the number of elements should include the NULL character (''
) too.
1 |
char demo[10]; |
2. The Initialization of Strings in C
A string in C could be initialized in different ways. When creating a string by specifying individual characters like example 2 and 4 below, you also make sure that the NULL character is added to it.
- char string[20]=”JournalDev”;
- char string[20]= { ‘J’ , ‘o’, ‘u’ , ‘r’ , ‘n’ , ‘a’ , ‘l’ , ‘D’ , ‘e’ , ‘v’ , ‘’ };
- char string[]=”JournalDev”;
- char string[]= { ‘J’ , ‘o’, ‘u’ , ‘r’ , ‘n’ , ‘a’ , ‘l’ , ‘D’ , ‘e’ , ‘v’ , ‘’ };
For both 3rd and 4th cases the number of elements is not initially mentioned. In these situations, the size of the array is by-default set to the total number of elements(including ''
).
3. Traversing a String in C
For example, let us take a look at how a string can be traversed similar to an array.
1 2 3 4 5 6 7 8 9 10 |
char string[20]="JournalDev"; char ch; int i=0; ch=string[i]; while(ch!=NULL) { printf("%c",ch); i++; ch=string[i]; } |
Output:
1 |
JournalDev |
4. Reading String in C (using scanf() gets() or fgets())
Both scanf() or gets() functions are useful for reading a string from the user. But, unfortunately, the scanf()
function only reads a sequence of characters until it encounters a newline, space, or tab, etc. Whereas, gets()
reads the sequence of characters until a newline(or enter key). Further, fgets()
is used to avoid the buffer overflow possible due to gets()
. Let’s take a look at how all the above-mentioned functions work.
For example,
1 2 3 4 5 6 7 8 |
#include<stdio.h> int main() { char string_input[20]; printf("Enter the string: "); scanf("%s", string_input); puts(string_input); } |
Output:
As you can see, due to the presence of whitespaces in between the string, scanf()
takes in only the characters before the first space. This can be avoided by the gets()
function.
1 2 3 4 5 6 7 8 |
#include<stdio.h> int main() { char string_input[20]; printf("Enter the string: "); gets(string_input); puts(string_input); } |
Output:
Again using fgets()
function,
1 2 3 4 5 6 7 8 |
#include<stdio.h> int main() { char string_input[20]; printf("Enter the string: "); fgets(string_input,sizeof(string_input),stdin); puts(string_input); } |
Output:
5. C String Library functions
The standard C library incorporates many in-built string oriented functions. All these functions are available inside the string.h
header file. So, including the string.h
header file is going to be useful for easier string manipulation.
Some common functions are,
strlen(string)
– This function returns the length of the passed string or the total number of elements in the string,strcmp(string1,string2)
– This function compares two string. It returns 0 if the two strings are equal, and else it returns a non-zero value,strcat(string1,string2)
– This function concatenates two given strings. It Appends string2 to the end of string1 and stores the result to string1,strcpy(string1,string2)
– This function copies the contents of the string2 into the string1,strrev(string)
– This function reverses the string inside,- And many more.
For further reading – Check all the previous tutorials on C Programming on Journaldev.