Strings in C

 String is a set of characters that are enclosed in double quotes. In the C programming language, strings are created using one dimension array of character datatype. Every string in C programming language is enclosed within double quotes and it is terminated with NULL (\0) character. Whenever c compiler encounters a string value it automatically appends a NULL character (\0) at the end. The formal definition of string is as follows...

String is a set of characters enclosed in double quotation marks. In C programming, the string is a character array of single dimension.

In C programming language, there are two methods to create strings and they are as follows...

  1. Using one dimensional array of character datatype ( static memory allocation )
  2. Using a pointer array of character datatype ( dynamic memory allocation )
Creating string in C programming language

In C, strings are created as a one-dimensional array of character datatype. We can use both static and dynamic memory allocation. When we create a string, the size of the array must be one more than the actual number of characters to be stored. That extra memory block is used to store string termination character NULL (\0). The following declaration stores a string of size 5 characters.

char str[6] ;

The following declaration creates a string variable of a specific size at the time of program execution.

char *str = (char *) malloc(15) ;

Assigning string value in C programming language

String value is assigned using the following two methods...

  1. At the time of declaration (initialization)
  2. After declaration
Examples of assigning string value
 
int main()
{
    char str1[6] = "Hello";
    char str2[] = "Hello!";
    char name1[] = {'s','m','a','r','t'};
    char name2[6] = {'s','m','a','r','t'};

    char title[20];
    *title = "bca class";
    
    return 0;
 
Traversing String

Traversing the string is one of the most important aspects in any of the programming languages.  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.

Hence, there are two ways to traverse a string.

  • By using the length of string
  • By using the null character.

Let's discuss each one of them.

Using the length of string

Let's see an example of counting the number of vowels in a string.

    #include<stdio.h>  
    void main ()  
    {  
        char s[11] = "prasanjeet";  
        int i = 0;   
        int count = 0;  
        while(i<11)  
        {  
            if(s[i]=='a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o')  
            {  
                count ++;  
            }  
            i++;  
        }  
        printf("The number of vowels %d",count);  
    }  

 Output


 

Using the null character

Let's see the same example of counting the number of vowels by using the null character.

     #include<stdio.h>  
    void main ()  
    {  
        char s[11] = "c programming";  
        int i = 0;   
        int count = 0;  
        while(s[i] != NULL)  
        {  
            if(s[i]=='a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o')  
            {  
                count ++;  
            }  
            i++;  
        }  
        printf("The number of vowels %d",count);  
    }  
 
Output
 

 
Reading string value from user in C programming language

We can read a string value from the user during the program execution. We use the following two methods...

  1. Using scanf() method - reads single word
  2. Using gets() method - reads a line of text

Using scanf() method we can read only one word of string. We use %s to represent string in scanf() and printf() methods.

Examples of reading string value using scanf() method
 
#include<stdio.h>
#include<conio.h>

int main(){

   char name[50];
   printf("Please enter your name : ");
   
   scanf("%s", name);
   
   printf("Hello! %s , welcome to BCA class !!", name);
   
   return 0;
} 
 

When we want to read multiple words or a line of text, we use a pre-defined method gets(). The gets() method terminates the reading of text with Enter character.

Examples of reading string value using gets() method
#include<stdio.h>
#include<conio.h>

int main(){

   char name[50];
   printf("Please enter your name : ");
   
  gets(name)|;
   
   printf("Hello! %s , welcome to BCA class !!", name);
   
   return 0;
} 
 
C Programming language provides a set of pre-definied functions called String Handling Functions to work with string values. All the string handling functions are defined in a header file called string.h.
 

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.

 

#include<stdio.h>  
#include <string.h>    
int main(){    
char name[50];    
printf("Enter your name: ");    
gets(name); //reads string from user    
printf("Your name is: ");    
puts(name);  //displays string    
return 0;    
}   

 

 

No comments:

Post a Comment