C Strings
C Strings
C Strings
The character
array or the string is used to manipulate text such as word or sentences. Each character in the array occupies one byte of
memory, and the last character must always be 0. The termination character ('\0') is important in a string since it is the
only way to identify where the string ends. When we define a string as char s[10], the character s[10] is implicitly initialized
with the null in the memory.
By char array
By string literal
array index starts from 0, so it will be represented as in the figure given below.
0 1 2 3 4 5
h E l l o \0
While declaring string, size is not mandatory. So we can write the above code as given below:
We can also define the string by the string literal in C language. For example:
char ch[]="hello";
In such case, '\0' will be appended at the end of the string by the compiler.
Difference between char array and string literal: There are two main differences between char array and literal.
We need to add the null character '\0' at the end of the array by ourself whereas, it is appended internally by the
compiler in the case of the character array.
The string literal cannot be reassigned to another set of characters whereas, we can reassign the characters of the
array.
Example:
Program where a string is declared and being printed. The '%s' is used as a format specifier for the string in c language.
#include<stdio.h>
#include<conio.h>
#include <string.h>
int main(){
char ch[]="hello";
getch;}
Output
Traversing String: Traversing the string is one of the most important aspects in any of the programming languages. We
may need to manipulate a very large text which can be done by traversing the text. Traversing string is somewhat different
from the traversing an integer array. We need to know the length of the array to traverse an integer array, whereas we
may use the null character in the case of string to identify the end the string and terminate the loop.
#include<stdio.h>
void main ()
char ch[6]="hello";
int i = 0;
int count = 0;
while(i<6)
count ++;
i++;
Output
#include<stdio.h>
void main ()
char ch[6]="hello";
int i = 0;
int count = 0;
while(s[i] != NULL)
count ++;
i++;
Output
Accepting string as the input: We have used scanf to accept the input from the user. However, it can also be used in the
case of strings but with a different scenario. Consider the below code which stores the string while space is encountered.
#include<stdio.h>
void main ()
char s[20];
scanf("%s",s);
}
Output
C gets() and puts() functions: The gets() and puts() are declared in the header file stdio.h. Both the functions are
involved in the input/output operations of the strings.
C gets() function: The gets() function enables the user to enter some characters followed by the enter key. All the
characters entered by the user get stored in a character array. The null character is added to the array to make it a
string. The gets() allows the user to enter the space-separated strings. It returns the string entered by the user.
Declaration
char[] gets(char[]);
#include<stdio.h>
void main ()
char s[30];
gets(s);
Output
C puts() function: The puts() function is very much similar to printf() function. The puts() function is used to print the
string on the console which is previously read by using gets() or scanf() function. The puts() function returns an integer
value representing the number of characters being printed on the console. Since, it prints an additional newline
character with the string, which moves the cursor to the new line on the console, the integer value returned by puts()
will always be equal to the number of characters present in the string plus 1.
Declaration
int puts(char[])
Example to read a string using gets() and print it on the console using puts().
#include<stdio.h>
#include<conio.h>
#include <string.h>
void main()
char name[50];
getch();
Output: