- C Programming Basics
- C Tutorial
- C Program Structure
- C Basic Syntax
- C Data Types
- C Constants
- C Variables
- C Operators
- C Ternary Operator
- C Storage Classes
- C Flow of Control
- C Decision Making
- C if if-else Statement
- C switch Statement
- C Loops
- C for Loop
- C while Loop
- C do-while Loop
- C goto Statement
- C break Statement
- C continue Statement
- C Popular Topics
- C Arrays
- C Strings
- C Pointers
- C Functions
- C Recursion
- C Scope Rules
- C Programming Advance
- C Structures
- C Unions
- C Bit Fields
- C Enumerations
- C Input & Output
- C Typedef
- C Preprocessors
- C Type Casting
- C Recursion
- C Error Handling
- C Linked Lists
- C Stacks
- C Queues
- C Binary Trees
- C Header Files
- C File I/O
- C Variable Arguments
- C Memory Management
- C Command Line Arguments
- C Programming Examples
- C Programming Examples
- C Programming Test
- C Programming Test
C Strings
In C, a string is simply a null-terminated character array. In other words, a string is actually a single dimensional array of characters, terminated by null character ('\0').
Declare a String in C
Here is an example showing how to declare a string in C:
char str[80];
Here, str can hold upto 80 characters including null terminated character (i.e. '\0').
Note - Whenever declaring a character array that will hold a string, you must have to declare it to be one character longer than the largest string, that it will hold.
Note - The compiler automatically adds the null to the end of the string constant.
A string constant is a list of characters enclosed in double quotes. Here is an example.
"i am string"
C String Example
Here is an example program demonstrating the array of character of simply strings.
/* C Strings - This program illustrates the * concept of array of characters or simply * strings in C language */ #include<stdio.h> #include<conio.h> void main() { char str[40] = "Hello Compiler, I am a String"; clrscr(); printf("%s", str); getch(); }
Here is the sample output of the above C program:

Here is another example. This program initialize strings to str entered by the user.
/* C Strings */ #include<stdio.h> #include<conio.h> void main() { char str[40]; clrscr(); printf("Enter a sentence: "); gets(str); printf("\nYou entered:\n"); printf("%s", str); getch(); }
Below is a sample run of the above C program:

Functions to Manipulate Strings in C
C provides a lot of functions that manipulates strings, such as strcat(), strchr(), strcmp(), strcoll(), strcpy(), strcspn(), strerror(), strlen(), strncat(), strncmp(), strncpy(), strpbrk(), strrchr(), strspn(), strstr(), strtok() etc. Here the following table lists the most common function that manipulate strings in C:
Function Name | Works |
---|---|
strcpy(s1,s2) | Copies s2 into s1 |
strcat(s1,s2) | Concatenates s2 onto the end of s1 |
strlen(s) | Returns the length of s |
strcmp(s1,s2) | Returns 0, if s1 and s2 are same. Returns less than 0, if s1 is less s2. Returns greater than 0, if s1 is greater than s2. |
strchr(s,ch) | Returns a pointer to the first occurrence of ch in s |
strstr(s1,s2) | Returns a pointer to the first occurrence of s2 in s1 |
To see in detail about all the functions that manipulate strings and characters, then refer C String and Character Functions.
Now let's take some examples demonstrating strings in C language.
/* C Strings */ #include<stdio.h> #include<conio.h> void main() { char str1[80], str2[80]; clrscr(); printf("Enter any two strings: "); gets(str1); gets(str2); printf("\nLength of first string is %d\n", strlen(str1)); printf("Length of second string is %d\n", strlen(str2)); if(!strcmp(str1, str2)) { printf("\nThe strings are equal\n"); } strcat(str1, str2); printf("\nNow the string 1 becomes\n"); printf("%s", str1); getch(); }
Here is the two sample run of the above C program. The first one, when user enters equal string. The second one, when user enters unequal strings.


Here is one more example. This program ask to the user to enter the two string and then a character. Then this programs checks whether the character present in the first string or not. If present then a message will be displayed telling that the character is present in the first string otherwise no message will be displayed. And then this program checks whether the second string is present in the first string or not. If present then a message will displayed saying that this string is present in the first string. Otherwise, no message will be displayed.:
/* C Strings */ #include<stdio.h> #include<conio.h> void main() { char str1[80], str2[80]; char ch; clrscr(); printf("Enter any two strings: "); gets(str1); gets(str2); fflush(stdin); printf("Enter a character: "); scanf("%c", &ch); if(strchr(str1, ch)) { printf("%c is present in the first string\n", ch); } if(strstr(str1, str2)) { printf("Second string found in the first string\n"); } getch(); }
Here are 4 sample run of this C program given.




More Examples
Here are some more examples demonstrating strings in C, that you can go for:
- Find Length of String
- Compare two String
- Copy String
- Concatenate String
- Reverse String
- Delete Vowels from String
- Delete Words from Sentence
- Find Frequency of Character
- Count Word in Sentence
- Remove Spaces from String
- Sort a String
- Convert Uppercase to Lowercase
- Convert Lowercase to Uppercase
- Swap two Strings
- Check Anagram or Not
« Previous Tutorial Next Tutorial »
Follow/Like Us on Facebook
Subscribe Us on YouTube