ASCII code of null character is 0.
Programming Example 1
In this programming, we will try to learn how a string is declared and initialized, then print all the characters of that particular String.
Output
Note
If we want to initialize the string, we can write each letter of a word with is ‘ & ‘ separate by, and end with a special character ‘ \0 ‘.
Explanation
Here we want to print the null character, but we can’t able to print the null character because it is not a printable character, but it has some other usefulness.
If we want to take a value or string from the user, then we don’t know how many times the loop will run, but when the loop sees that ‘ \0 ‘has come, then it will understand that this is the end of the string.
If we don’t want to print each char of a string usingLoop and want to print the total string, then-
Another Function can be used to print the string.
Output
Another Format of String Initialization
If we don’t want to initialize the string at the time of declaration, want to take a string from the user, then –
1. scanf ( “ %s ” , s ) ; // Here don’t require to use and sign .
2. scanf (“ %s” , %s [ 0 ]) ; // the meaning of two lines are same
scanf() has only one drawback. If we want to print a name like Avishek Dutta, Then it only prints Avishek. Because scanf() is like a delimiter of separation. If we use it, then it understands that information or string is finished. It has another form –
But it prints the total string – Avishek Dutta.
Programming Example 2
This programming example will teach how we declare and initialize a string with the help of the malloc () function.
#include <stdlib.h>
#include <string.h>
int main()
{
char *a ;
int b ;
printf( " How many characters in the string ? " ) ;
scanf("%d", &b);
a = ( char* ) malloc ( b * sizeof ( char ) ) ; // creating the block of string
printf( " Insert the string : " ) ;
scanf("%s", a);
printf( " Entered string : %s \n ", a ) ;
free ( a ) ;
return 0 ;
}
Output
Explanation
Here we want to input a string from the user. The string is created with the help of dynamic memory allocation. malloc() create the string and returns the address of the string to the variable a. Now we simply print the string.
String Related Function
strlen ( )
Passing the address of the string & it returns the length of the string. Passing only one argument.
strrev ( )
Reverse the string. It only takes one argument that is the address of the string & returns the reverse string.
strlwr ( )
It changes the upper case letter to the lower case letter.
strupr ( )
It changes the lower case letter to the upper case letter.
strcpy ( )
To copy the string
strcmp ( )
We can do below to compare the string.
It returns one integer value that is a corresponding mismatch of the ascii character.
strcat ( )
To append the two strings,
So, it is connected as “HELLO student “.
Handling multiple of string:
[3] Means how many string we can put in these 2D string.
Programming Example 3
In this programming, we are learning how the two-dimensional string is declared and initialized and its application.
Output
Explanation
Like an array, we can declare and initialize a two-dimensional string in the C language. Here we declare a 2D string named s [3][10]. Within brackets 3 and 10 means we assume that this particular string has three rows, and each row has ten columns. In the string, we assign three cities’ names. They are Bhopal, Delhi, and Kanpur. These values are taken from the user by using the gets () function through for loop. Now we simply print the cities name.
Conclusion
From the above discussion about string, we have come to the decision that String is a powerful concept in C language. String gives us a new dimension to store character type values. Through different functions in the string, we can operate different types of character values to concatenate, lower to capital, capital to lower, reverse, etc.