C Language | String | Start your journey with C | Part 6
Strings :
There is no separate data type for strings in C. They are treated as arrays of type char. A character array is a string if it ends with a null character ( "\0" ). This null character is an escape sequence with ASCII value 0. String are generally used to store and manipulate data in text form like word or sentences. Some example are below :
"mary jane"
"hello world"
"Winner winner chicken dinner"
"enemies ahead"
"i got supplies"
String Constant or String Literal :
A string constant is a sequence of characters enclosed in double quotes. It is sometimes called a literal. The double quotes are not a part of the string. Some examples of string constants are same as shown as above.
String constant is stored in memory as an array of characters terminated by a null character ( "\0" ). The string constant itself becomes a pointer to the first character in the array. For example the string "mary jane" will be stored in memory as -
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
m | a | r | y | j | a | n | e | "\0" |
String Library Functions :
There are several library functions used to manipulate string. The prototype for these functions are in header file string.h . Some of the commonly used in functions from this header file are discussed below.
To know more about the functions of string.h header file, you can easily get it on the internet. Just type " all functions of string.h header file in c language " of your favorite search engine.
- strlen( )
Here is an program to understand the work of strlen( ) function :
#include<stdio.h>
#include<string.h>
void main()
{
char name[20];
int len;
printf("Enter your name > ");
scanf("%s",name);
len = strlen(name);
printf("Length of your name is : %d\n",len);
}
Output :
Enter your name > jason
Length of your name is : 5
- Strcpy( ) :
Here is an program to understand the work of strlen( ) function :
#include<stdio.h>
#include<string.h>
void main()
{
char strl [10], str2 [10];
printf ("Enter the first string :" );
scanf("%s",str1);
printf ("Enter tne second string :" );
scanf("%s",str2) ;
strcpy(str1,str2);
printf ("First string ; %s
strcpy(str1,"Delhi");
strcpy(str2,"Calcutta");
printf ("First string : %s \t\t Second string : %s\n",str1,str2) ;
strcpy(str1,"Delhi");
strcpy(str2,"Calcutta");
printf ("First string : %s \t\t Second string : %s\n",str1,str2) ;
}
Output :
Enter the first string : London
Enter the second string : Paris
First string : London Second string : Paris
First string : Delhi Second string : Calcutta
To know more about the functions of string.h header file, you can easily get it on the internet. Just type " all functions of string.h header file in c language " of your favorite search engine.
---------------------------------------------------------------------------------------------------------------
ADS BY AMAZON.IN
Comments
Post a Comment
Give your feedback!
DO NOT SPAM !!
NO THIRD PARTY PROMOTIONAL LINK !!!! (else comment will be deleted.)