15. C-Strings

C-Arrays and Functions: Previous                                                  Next: C-Structures

Strings are collection of characters used to store alphabets, names, text and sentences, but C doesn`t provide a string data type. To store the collection of characters, char type of array is to be made. Each and every character of char array occupy one byte in memory. Character can be like single letter, numeric value, any symbol.
The main characteristic of string is that, it is terminated by NULL character [‘\0’].
The string is always internally represented by ASCII value and every character has a unique ASCII value.
Suppose you want to store name and full address of an student the char array [string] is used.
Syntax: char[size];

Size is an integer value which defines maximum number of characters an array will store including NULL character ‘\0’.
Eg:      char name[15];
“%s” format specifier written in scanf( ) is used to take input of characters from keyboard but the main drawback of taking input from scanf( ) is, it takes only till the white space.
Eg:      Entered string is Krishna Vijay and string will only get stored is ‘Krishna’ so to overcome this problem gets(); function is used. this function is capable for taking string with the tabs and white spaces. The termination key for this function is Enter key. It means it takes input of string until enter key is not pressed.
Syntax: gets();
Eg:      Simple program for understanding the concept of string in c programmig
#include
                     #include
                     void main( )
                     {
                                char name[15];
                                clrscr();
                                printf(“Enter your Full Name : ”);
                                scanf(“%s”, name);                                                  Ajay Sharma
                                printf(“Your name is %s”, name);                           Ajay
                                printf(“Enter your Full Name : ”);
                                gets(name);                                                              Ajay Sharma
                                printf(“Your name is %s”, name);                           Ajay Sharma
                     }
                     Note:  ‘&’ operator is not required in scanf( ) while taking input of string.
Initialization of character Array: -
As we have initialized the integer or floating arrays in same way we can initialized character array also. There is two way for initialization of an array:
  1. Through character by character
  2. Through assigning whole string

  1. Character by Character: - 

char name[ ] = {‘G’, ‘A’, ‘U’, ‘R’, ‘A’, ‘V’}; it is invalid because when we initialize an array character by character then ‘\0’ is compulsory to store at the End.
Valid syntax is: char name[ ] = {‘G’, ‘A’, ‘U’, ‘R’, ‘A’, ‘V’, ‘\0’};
  1.  Assigning whole String: -

char name[ ] = “Gaurav”;
no need to terminate a string while initializing through this method.
To read each and every character of string loop can be run till ‘\0’ character.
Eg:                Write a program to enter your city name and count vowels in it?
Solution:       #include
                     #include
                     void main( )
                     {
                                char city[20];
                                char ch;
                                int vowel = 0, i, conso = 0;
                                clrscr();
                                printf(“Enter your City Name : ”);
                                gets( city);
                    
                                for(i = 0; city[i] != ‘\0’; i++)
                                {
                                                ch = city[i];
                                                switch(ch)
                                                {
                                                                case ‘A’:
                                                                case ‘E’:
                                                                case ‘I’:
                                                                case ‘O’:
                                                                case ‘U’:
                                                                case ‘a’:
                                                                case ‘e’:
                                                                case ‘i’:
                                                                case ’o’:
                                                                case ‘u’:
                                                                                vowel++;
                                                               
                                                }
                                }
                                printf(“\nThe count of vowels : %d”, vowel);
                                getch( );
                     }
Eg:                Write a program to print number of words in entered string?
Solution:       #include
                     #include
                     void main( )
                     {
                                char data[80];
                                int count= 0, i ;
                                clrscr();
                                printf(“Enter String: ”);
                                gets(data);
                                for(i = 0; data[i] != ‘\0’; i++)
                                {
                                                if(data[i] == ‘ ‘)
                                                                count++;
                                }
                                printf(“\n The count of Words is: %d”, count);
                                getch( );
                     }
Output:

Pointer to character string:

As we mentioned before in cprogramming, the use of pointer array is mainly in string. Following Eg. For creating character array of pointer.
Char * names[5]={“Gaurav”, “Vijay”,, “Manisha”, “Divya”, “Girisha”};
No NULL character is appended because it is array of string not array of characters.
Eg:  Illustrate the Pointer and String array
Solution:             #include
                                                #include
                                                void main( )
                                                {
                                                                char*pstr[4];
                                                                clrscr();
                    
                                                                *pstr[0] = “Arjun”;
                                                                *pstr[0] = “Manisha”;
                                                                *pstr[0] = “Lavina”;
                                                                *pstr[0] = “Guddu”;
                                                                printf(“Value at pointer1 is : %s ”,pstr[0]);
                                                                printf(“\nValue at pointer2 is : %s ”,pstr[1]);
                                                                printf(“\nValue at pointer3 is : %s ”,pstr[2]);
                                                                printf(“\nValue at pointer4 is : %s ”,pstr[3]);
                                                }             
Some standard Library Functions related with String: - C provides some predefined string functions to make a work easy. Some built in String functions are:
strlen( )                              strcpy( )                   strcmp( )                  strcat( ) etc.
strlen( ): - this function is used to get length of string means the total number of characters in a string. This function doesn`t count the NULL character in its length.
Syntax:               strlen();
The return data type of this function is integer.
Eg:      int length;
                                char name[25];
                                length = strlen(name);
                                                or
                                printf(“The length is %d”, strlen(name));
String Concatenation ( strcat( ) ): - this function is used to join string together and makes a one string. Generally use to copy array of string. It takes two arguments first one Destination where resultant string is to be stored and second is source, which string is to be copied to the Destination string.
Syntax:               strcat(, );
The both argument must be character array.
Eg:      char str1[30], srt2[15];
                                printf(“Enter First String : ”);                                                  
                                gets(str1);                                                                                                Gaurav
                                printf(“Enter Second String : ”);
                                gets(str2);                                                                                                Vijay
                                strcat(str1,str2)
                                printf(“The Concatenation String :%s ”,str1);                           

The Concatenation String : Gaurav Vijay
String Copy ( strcpy( ) ): - we can overwrite the value of normal variable by using the another value.
Eg:      int num2 = 20;
int num2 = 50;
In same way base address can`t be overwritten or this can`t be performed with string. Suppose we have two string arrays
char arr1[20], arr2[20];
arre1 = arr                                -invalid, can`t assign so to copy content of one string array into another string array variable, strcpy() is used. it also contain two arguments i.e. source and destination.
Syntax:               strcpy(, );
Eg:                      strcpy(Science, Computer);
Result is Computer.
Eg:      char str1[15], srt2[15];
                                printf(“Enter First String : ”);                                                  
                                gets(str1);                                                                                                Gaurav
                                printf(“Enter Second String : ”);
                                gets(str2);                                                                                                Vijay
                                strcpy(str1,str2)
                                printf(“Final String :%s ”,str1);                                                                Final String : Vijay
String Comparison (strcmp( )) : - It is used to compare two string, and compares string character by character. It takes two parameters and returns an integer value. Comparison is based on following condition: -
Suppose we have two strings str1 and str2:
If str1 < str2 returns -1.
If str1 > str2 returns 1.
If str1 = = str2 returns 0.
Syntax: strcmp(string1, string2);
Eg:      #include
                                #include
                                void main( )
                                {
                                                char str1[20], str[20];
                                                int ans = 0;
                                                clrscr();
                    
                                                printf(“Enter First String : ”);
                                                gets(str1);
                                                printf(“Enter Second String : ”);
                                                gets(str2);
                                                ans = strcmp(str1, str2);
                                                if(ans == -1)
                                                                printf(“\n %s is less than %s”,str1,str2);
                                                else if(ans == 0)
                                                                printf(“\n %s is Equal to %s”,str1,str2);
                                                if(ans == 1)
                                                                printf(“\n %s is greater than %s”,str1,str2);
                                                getch();
                                }
Output:

C-Arrays and Functions: Previous                                                  Next: C-Structures