By using the char array
When declaring a string using the char array, the programmer has to add the null character at the end of the string. A char array can be reassigned to another set of characters.
Example
#include <stdio.h>
#include<string.h>
int main()
{
char arr[]={'H','e','l','l','o',' ','W','o','r','l','d','\0'};
printf("The string is : %s",arr);
}
You can also try this code with Online C Compiler
Run CodeOutput
The string is : Hello World
By using the string literal
When declaring a string using a string literal, the compiler automatically adds the null character at the end of the string. A string literal once assigned cannot be reassigned to another set of characters.
Example
#include <stdio.h>
#include<string.h>
int main()
{
char arr[]="Hello World";
printf("The string is : %s",arr);
}
You can also try this code with Online C Compiler
Run CodeOutput
The string is : Hello World
Inputting a String
Example
#include<stdio.h>
#include<string.h>
int main()
{
char string[20];
printf("Enter the string : ");
scanf("%[^\n]s",string);
printf("Entered string is : %s",string);
}
You can also try this code with Online C Compiler
Run CodeOutput
Enter the string : Hello World!
Entered string is : Hello World!
Explanation
In C language, in order to accept input from the user, scanf() is used. We have not used ‘&’ with string (char array name) because the ‘&’ operator is used to provide the address of the variable to the scanf() in order to store the values entered by the user into the memory.
While reading inputs from the user, it must be taken into consideration that we must specify whether to take space-separated strings or not. In the above code, we have used ‘[^\n]’ in the scanf() to instruct the compiler to store the string till a new line is encountered. If we don't use [^\n] in the above code, only the characters before the first space will be considered and stored in the string.
While taking string inputs, it is preferred to use the gets() function, which is an inbuilt function in the ‘string.h’ header file. The gets() function is capable of receiving one string at a time, and the gets() function reads input until it encounters a newline or end of file (EOF). The main reason why we don't prefer scanf() for taking string inputs is that while using scanf(), the compiler doesn't perform any bound checking, that is, if the length of the string exceeds the size of the character array, then some important data is overwritten. You can practice it on an online c editor for better understanding.
Important Functions
strcpy(string1,string2)
strcpy stands for string copy. The strcpy() function takes two strings as its arguments and copies string2 into string1.
Example
#include<stdio.h>
#include<string.h>
int main()
{
char string1[]="Hello";
char string2[]="Ninja";
printf("String-1 before strcpy() function : %s",string1);
strcpy(string1,string2);
printf("\nString-1 after strcpy() function : %s",string1);
}
You can also try this code with Online C Compiler
Run CodeOutput
String-1 before strcpy() function : Hello
String-1 after strcpy() function : Ninja
strcat(string1,string2)
strcat stands for string concatenation. The Strcat() function takes two strings as its arguments and concatenates the string2 at the end of string1.
Example
#include<stdio.h>
#include<string.h>
int main()
{
char string1[]="Hello ";
char string2[]="Coders!!";
printf("String-1 before concatenation : %s",string1);
strcat(string1,string2);
printf("\nString-1 after concatenation : %s",string1);
}
You can also try this code with Online C Compiler
Run CodeOutput
String-1 before concatenation : Hello
String-1 after concatenation : Hello Coders!!
strlen(string1)
strlen stands for string length. The strlen() function takes one string as its argument and returns the length of the string1.
Example
#include<stdio.h>
#include<string.h>
int main()
{
char string1[]="Coding Studio";
int len=strlen(string1);
printf("Length of %s is %d",string1,len);
}
You can also try this code with Online C Compiler
Run CodeOutput
Length of CodingStudio is 13
strcmp(string1,string2)
strcmp stands for string compare. The strcmp() function takes two strings as its argument and compares them lexicographically. It returns
- A value 0 if both strings are identical
- A value greater than zero if the first not matching character in string1 has a greater ASCII value than the corresponding character in string2
- A value less than zero if the first not matching character in string1 has an ASCII value lesser than the corresponding character in string2.
Example
#include<stdio.h>
#include<string.h>
int main()
{
char string1[]="Ninja";
char string2[]="Ninja";
int val=strcmp(string1,string2);
printf("On comparing %s and %s the value returned is %d",string1,string2,val);
}
You can also try this code with Online C Compiler
Run CodeOutput
On comparing Ninja and Ninja the value returned is 0
strrchr(string1,ch)
The strrchr() function takes two arguments : a string and a character. It is used to return a pointer to the last occurrence of the character ch in string1.
Example
#include<stdio.h>
#include<string.h>
int main()
{
char string1[]="Coding Ninjas!";
char ch='j';
char *val=strrchr(string1,ch);
printf("The string after last occurrence of %c in the string %s is %s",ch,string1,val);
}
You can also try this code with Online C Compiler
Run CodeOutput
The string after last occurrence of j in the string Coding Ninjas! is jas!
strstr(string1,string2)
The strstr() function takes two strings as its arguments. It is used to return a pointer to the first occurrence of string2 in string1.
Example
#include<stdio.h>
#include<string.h>
int main()
{
char string1[]="Hi Ninjas,Work Hard!!!";
char string2[]="Ninjas";
char *val=strstr(string1,string2);
printf("The string after the first occurrence of %s in %s is : %s",string2,string1,val);
}
You can also try this code with Online C Compiler
Run CodeOutput
The string after the first occurrence of Ninjas in Hi Ninjas,Work Hard!!! is : Ninjas,Work Hard!!!
You can also check out Reverse A String In C
C String Examples
In C programming, strings are represented as arrays of characters terminated by a null character ('\0'). Here are a few examples illustrating the use of strings in C:
Example 1: Declaring and Initializing a String
#include <stdio.h>
int main() {
char str[] = "Hello, World!";
printf("%s\n", str);
return 0;
}
You can also try this code with Online C Compiler
Run CodeOutput:
Hello, World!
Example 2: Using fgets to Safely Read a String
#include <stdio.h>
int main() {
char sentence[100];
printf("Enter a sentence: ");
fgets(sentence, sizeof(sentence), stdin); // Safely reads the input
printf("You entered: %s", sentence);
return 0;
}
You can also try this code with Online C Compiler
Run CodeOutput
Enter a sentence: Hello World
You entered: Hello World
Frequently Asked Questions
Is it necessary to specify the character array size while declaring a string?
No, it is not necessary to specify the character array size while declaring a string.
What happens if we don't have a null character at the end of the string?
Some of the functions which are used with strings require a null char. In the case of the absence of a null character, the program will terminate with a Segmentation Fault since the pointer tries to access memory that is not owned by the program.
What is the difference between scanf() and gets() while taking input?
The scanf() function can read multiple values of different data types, whereas the gets() function can read-only character string data.
Conclusion
In this blog, we explored key concepts related to strings in the C programming language. We began by understanding what strings are and how they are represented in C. Next, we examined various ways to declare and initialize strings in C. Finally, we covered how to input strings and highlighted some important functions used for string manipulation.
Questions related to strings are asked frequently in personal interviews, you can practice and learn more about them from this.
Recommended problems -
Almost every programming language supports string and its functions, you can learn how to convert an array into a string in any language from this.
In order to write a structured code, knowledge of structures, functions, and different data types is very important.