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.
  • strlen( )
This function return the length of the string i.e the number of characters in the string excluding the terminating null character. It accepts a single argument, which is pointer to the first character of the string. For example strlen("bob") returns the value 3. similarly if var is an array that contains the name "bob" then strlen(var) returns the value 3.

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( ) :
This Function is used for copying one string to another string. strcpy( str1, str2) copies str2 to str1. Here str2 is the source string and str1 is destination string. if str2 = "john" then this function copies "john" into str1. This function takes two pointer to two strings as arguments and returns the pointer to first string.


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

Popular posts from this blog

TOP 10 PROGRAMMING LANGAUAGES COMMING 2021 | ARTICLE

Write a C-program to swap two number:

Print Fibonacci Series using Recursion | C program :