String In C [Declaration, Initializing With Example] – CsTutorialpoint
Hello friends, in today’s article we are going to talk about String. Today we will learn in detail about What is String In C? And how to use sting in C language?
So without wasting any time let’s first understand what is string in C
What is String in C
A String is an array of characters that is used to manipulate text such as words and sentences.
In simple words, “Sequence of characters which is terminated by null character (‘\0’) is called string.” For example – the name of a city (eg – Bhopal, Raipur, Delhi), the name of a person (eg – Ramesh, Suresh), all these are a string whose last character is null character (‘\0’).
Note -: The null character (‘\0’) is not a printable symbol, it is like a space, due to which we do not need to know how many characters are in the string. We can easily tell where a string ends by looking at the null character (‘\0’).
The ASCII code for the null character (‘\0’) is zero (0).
The only difference between a string and a normal array is that the string is terminated by the null character (‘\0’) and a normal array does not.
In an array of character type, each character takes up one byte of memory and the last character is always the null character (0).
The last character of the string must be the null character (‘\0’) so that the compiler can know where the string ends.
Declaration of String in C
In C language, we can declare a string like this -:
char str_name[size];
char -: It is a character data type which indicates that we can store only character in the variable created with its help.
str_name –: The name of the variable that you are going to create will come in place of this str_name. You can keep this name as per your choice like – my_string, name, city etc.
size –: It tells the size of the string, if 20 is written here then the size of char string variable will be 20 and it will be able to store only 20 characters. You can define the size of the string variable according to the size of the string.
Initializing a String in C
We can initialize the string in many different ways. Let’s know about all those ways to initialize the string.
In the example below, we have named the string str and initialized CsTutorialpoint in it.
Method – 1
char str[] = "CsTutorialpoint";
When we initialize a string variable like this, then the null character is automatically added to the end of the string. In this type of initialization, we do not need to declare the size of the string. The number of characters we initialize in a string variable, the size of the variable also becomes of that number of characters.
Note -: We define a single character with single quote ( ‘a’ ) and a string which is a group of characters is represented by double quote ( “Jeetu” ).
Example -:
#include<stdio.h>
void main ()
{
char str[] = "CsTutorialpoint";
int i;
int count = 0;
for(i=0;str[i]!='\0';i++)
{
{
count ++;
}
}
printf("The number of Characters is: %d",count);
}
Output -:
The number of Characters is: 15
Like in the above example the size of str[] is 15 because in this we have initialized a string of 15 characters.
Method – 2
char str[25] = "CsTutorialpoint";
In this type of initialization, the null character is automatically added to the end of the string, we do not need to write the null character separately at the end of the string.
Method – 3
char str[] = {'C','s','T','u','t','o','r','i','a','l','p','o','i','n','t','\0'};
When we initialize the character array in this way, then the null character is not automatically added to the end of the string, so we have to add the null character ourselves to the end of the string.
In this type of initialization, we do not need to declare the size of the string. The number of characters we initialize in a string variable or character array, the size of the variable also becomes that number of characters.
Example -:
#include<stdio.h>
void main ()
{
char str[] = {'C','s','T','u','t','o','r','i','a','l','p','o','i','n','t','\0'};
int i;
int count = 0;
for(i=0;str[i]!='\0';i++)
{
{
count ++;
}
}
printf("The number of Characters is: %d",count);
}
Output -:
The number of Characters is: 15
Like in the above example the size of str[] character array is 15 because in this we have initialized a string of 15 characters.
Note – The null character (\0) is not counted with the string. This is a character which is used only to indicate where the string is ending. If we do not use a null character at the end of the string, then the compiler will not know where the string is ending.
Method – 4
char str[18] = {'C','s','T','u','t','o','r','i','a','l','p','o','i','n','t','\0'};
In this type of initialization, we have to write the null character separately at the end of the string.
Let us make a program to understand the string better.
Example -:
// C program to illustrate strings
#include<stdio.h>
void main()
{
// declare and initialize string
char str[] = "CsTutorialpoint";
printf("%s",str); // print string
}
In this example we declared an array named str and stored the CsTutorialpoint string in it and printed the string on the screen with the help of printf() function
Output -:
CsTutorialpoint
String Input
In C language, we can take string as input in many ways, let’s understand about those methods -:
- by scanf() Function
- by %[^\n]s
- by gets() function
Method 1 – By scanf() Function
By scanf() function we can easily store string in Array Elements.
Example -:
#include<stdio.h>
int main ()
{
char s[20];
printf("Enter the string\n");
scanf("%s",s);
printf("\nYou entered String is :%s",s);
return 0;
}
Output -:
Enter the string
CsTutorialpoint
You entered String is: CsTutorialpoint
In this example, we have input the string into s array by scanf() function and have also printed the string stored inside the array in the screen by printf().
Note -: Like %d Format Specifier is used in scanf() and printf() function to store the value of Integer type in a variable and print the stored integer value in a variable, in the same way, %s Format Specifier is used to input the string into a variable and print the string stored in the variable.
When we take a string input by the scanf() function, then scanf() does not know to store the string character after the space in a variable like – “CsTutorial Point” , there is a space between CsTutorial and Point so scanf(), only will store “CsTutorial” in the s variable and ignore the string “Point” after the space.
When we take a string input by the scanf() function, the compiler sees the space and understands that the string has ended, and it does not know to store the string after the space in the variable.
If we want to store the string after the space in the variable also, then for this we have to write the statement in some other way. Let’s talk about that second method in method 2.
Method 2 – By %[^\n]s
As I mentioned above, %s can only store single string, cannot store string after space. If we want to store the string after the space also, then for this we have to write the statement like this – %[^\n]s
Example -:
#include<stdio.h>
int main ()
{
char s[20];
printf("Enter the string\n");
scanf("%[^\n]s",s);
printf("\nYou Entered String Is: %s",s);
return (0);
}
Output -:
Enter the string
CsTutorial Point
You Entered String Is: CsTutorial Point
Method 3 – By gets() function
We can also easily store the string in a variable by the gets() function. When we use the gets() function to take string input, then we can store the string in a variable with as many spaces as we want.
In the previous method, we had learned that when we tried to take string as input by the scanf() function, then the scanf() function only took a single string as input.
If we tried to take input of string with more than one space by scanf() function, then scanf() function would skip the string after space but it does not happen in gets() function.
With the help of gets() function, we can easily store the character after the space in the variable.
Example -:
#include<stdio.h>
int main ()
{
char s[20];
printf("Enter the string\n");
gets(s);
printf("\nYou Entered String Is: %s",s);
return (0);
}
Output -:
Enter the string\n
Jeetu Sahu
You Entered String Is: Jeetu Sahu
String Output
In C language, we can display a string in the screen in different ways, let’s know about those methods -:
- by printf() Function
- by puts() function
Method 1 – By printf() Function
By printf() function we can easily print the stored value in string variable with the help of %s format specifier.
Example -:
#include<stdio.h>
int main ()
{
char s[20] = "CsTutorialpoint";
printf("\nYou Entered String Is: %s",s);
return (0);
}
Output -:
You Entered String Is: CsTutorialpoint
By printf() , we can also print each character of a string separately. For this we use %c format specifier inside printf() function.
#include<stdio.h>
int main ()
{
char s[20] = "Jeetu Sahu";
int i;
printf("You Entered String Is:");
for(i=0;s[i]!='\0';i++){
printf("\n%c",s[i]);
}
return (0);
}
Output -:
You Entered String Is: Jeetu Sahu
Method 2 – by puts() Function
By puts() function, when we print the string in the screen, then we have to write only the name of that string in parenthesis() of puts function
Example -:
#include<stdio.h>
int main ()
{
char s[20] = "Jeetu Sahu";
printf("String is: ");
puts(s);
return (0);
}
Output -:
String is: Jeetu Sahu
Input & Output Strings in C using Scanf() and Printf() functions
Example program
#include<stdio.h>
int main ()
{
char s[20];
printf("Enter Your Name\n");
scanf("%s",s);
printf("Your Name Is: %s",s);
return (0);
}
Output -:
Enter Your Name\n
Jeetu
Your Name Is: Jeetu
Input & Output Strings in C using gets() and puts() functions
Example program
#include<stdio.h>
int main ()
{
char s[20];
printf("Enter Your Name\n");
gets(s);
printf("Your Name is : ");
puts(s);
return (0);
}
Output -:
Enter Your Name
Jeetu Sahu
Your Name is : Jeetu Sahu
Read More
- Structure In C
- Union in C
- Enum in C
- Typedef in C
- What is Pointer in C
- Download C Language Notes Pdf
- C Language Tutorial For Beginners
- C Programming Examples With Output
- 250+ C Programs for Practice PDF Free Download
Conclusion
Friends, in today’s article we talked about Sting In C and know what is string in C language. And how to use sting in C language?
Friends, I hope that you have found the answer to your question and you will not have to search about the sting In c language.
However, if you want any information related to this post or related to programming language, or computer science, then comment below I will clear your all doubts
If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about Sting In C.
To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.