Handling of Charactor Strings


A string is an array of characters. Any group of characters (except double quote sign) defined between double quotation marks is a constant string.

"He is good";
If you want to include a double quote in the string we may use it with a back slash.

printf ("\" he is good \" ");

Out put is:

he is good"

operations performed on character strings are-

  1. Reading and writing strings.
  2. Combining strings together.
  3. Copying one string to another.
  4. Comparing strings for equality.
  5. Extracting a portion of a string.
Declaring and initializing a string variable:
A string variable is any valid C variable name and is always declared as an array. When the compiler assigns a character string to a character array, it automatically supplies a null character '\0' at the end of the string.
Therefore, the size should be equal to the maximum number of characters in the string +1.
This extra location is to store the null character '\0'. Character arrays may be initialized when they are declared.


Example:
char name[25] = "RAMESH";
or
char name[25] = 'R', 'A', 'M', 'E', 'S', 'H', '\0'}
or
char name[ ] = "RAMESH";

Different String Handing Functions:
Different kinds of manipulations can be performed on strings.

C library provides string handling functions and few of which are listed below:

(1) strcat ( )
This function concatenates two strings. The process of combining two strings together is called concatenation.


Syntax:
strcat (string1, string2);

Example:
char string1[25] = "he is good";
char string2[10] = " smart too";

Example 1:
strcat (string1, string2);


As shown in example1 string1 and string2 are character arrays, when the function strcat( ) is executed, string2 is appended to string1.
It does so by removing the null character at the end of string1 and placing string2 from there.
The string at string2 remains unchanged.
Thus now string1 will have he is good smart too.



Example 2:
strcat (string1, "smart too");
The function strcat( ) can also append a string constant to a string variable as shown in example2.


Note:
We must make sure that the size of string1 to which string2 or a string constant appended, is large enough to accommodate the final string.

It is also possible to nest strcat( ) functions.

Example:
strcat (strcat (string1, string2), string3);
char string1[ ] = "he is"
char string2[ ] = "good"
char string3[ ] = "smart too"

All the three strings string1, string2 and string3 are combined together to form a resultant string stored in string1.

(2) strcmp( )

This function compares two strings identified by the arguments and returns a value 0 if they are equal.
If they are not equal it returns the numeric difference between the first non-matching characters in the string

. Syntax:
strcmp (string1, string2);
string1 and string2 may be string variables or string constants.


Example:
#include <stdio.h>
#include <conio.h>
void main()
{
char string1[ ] = "Mahatma";
char string2[ ] = "Mahatma";
int n;
n = strcmp (string1, string2);
if (n == 0)
{
printf("Strings are equal");
}
else
{
printf ("Strings are not equal");
}
}

Output


We can also compare a string variable with a constant as shown in the example below.

strcmp (string1, "END");

In the above example the string constant END is compared with the contents of character array variable string1.
If variable string1 contains the string END then the strcmp() function returns zero else it returns a nonzero.

(3) strcpy ( )

This function works like a string assignment operator.
The source string is assigned i.e. copied on to the destination character array.

Syntax :
strcpy (string 1, string 2);


Example:
char string1[ ] = "He is good";
char string2[ ] = "hello";
strcpy(string1, string2);

The contents of string2 is assigned to string1, string2 can be a character array variable or a string constant.

In the above example after the execution of the statement strcpy(string1, string2); the character array variable string1 will contain hello overwriting earlier value of He is good.

strcpy (string1 , "hai");
In the above example the string hai is assigned to string1 overwriting the earlier value present in string1.
In both the cases, the size of the array string1 should be large enough to receive the contents of string variable string2 or the string constant.



(4) strlen( )

This function counts and returns the number of characters in a given string.

Syntax:
n = strlen (string1);
Where n is an integer variable and string1 is an array of characters.


Example:
#include <stdio.h>
#include<conio.h>
void main()
{
char string1[ ] = "He is good";
int length;
length = strlen(string1);
printf("Length of the string is %d\n",length);
}


Where length is an integer variable, which receives the value of the length of the string.
The argument can be a string variable or a string constant. The counting ends at the occurrence of the first null character.
Similarly you can have string constant as argument for the function strlen() as shown below.
length = strlen ("he is good");

Note:
All the string handling functions are defined in the header file string.h.



Arrays << Previous
Next >>Searching & Sorting

Our aim is to provide information to the knowledge seekers.


Support us generously

comments powered by Disqus






Footer1