Strings and String Functions

A string is an array of characters terminated by the null character. The complier automatically inserts a null character ('") at the end of the string. The null is the first ASCII character, which has a value of zero. The null character can be used as a single to let the compiler know when the text has ended.

String Declaration

The string that will hold an array of characters can be declared as

char variablename [size];

For example. the declaration char name[10] declares the name as the character array(string) variable that can hold a maximum of ten characters. Suppose we read the following string constant into the string variable name

"SOFTWARE"

each character is treated as an element of the array name and stored in memory as follows

  0   1    2    3    4    5    6     7    8   9
  S    O     F     T     W    A    R    E    'O'  

When the compiler encounters a constant string. it terminates it with an additional null character. Thus, the variable name[10] holds the character '" the end. When declaring character arrays,

We must always reserve one extra element space for the null terminator. The string "SOFTWARE" can be read using scanf statement as

scanf ("%s", name);

The scanf statement usually expects an address of the variable, but the name of the array. by itself, indicates the starting address of the array and hence it is an error to put the &operator. while inputting the strings. The s option uses a blank space as a delimiter, since it considers the characters separated by a blank space as two strings. The array name is used without a subscript in the scanf statement.

Initializing a string

A string cannot be initialized like other variables. The following way of declaring a string is not possible


char name[16];

name[16] = "software";The general rule is that each element of an array - each character of a string is a separate variable. Therefore, the logic of C dictates that each character in a string should be initialized individually, as we always do when we initialize an array.

The same string can he initialized as follows

name (0) = 's';

name (1) = 'o';

name (2) = 'f';

name (3) = 't';

name (4) = 'w';

name (5) = 'a';

name (6) = 'r';

name (7) = 'e';

name (8) = '';

String functions

The following functions perform operations on null-terminated character strings. In the description of these functions, string, string 1, string2 represent the address of the first element such character strings, c represents a single character and n represents an integer.

String concatenation

strcat (string1, string2) concatenates the character string string2 to the end of string1. placing a null character at the end of the final string. The function returns string1.

Example 3.4

This example illustrates how the strcat string function concatenates string2 at the end of string1. The header file string .h should be included before working with string functions.

#include <stdio.h>
int main() 
{
    char string1 []="spot" ; 
    char string2 []="buying";
    strcat (string1, string2); 
    printf ("%8", string1):
}

String location

strchr (string, character) Locates the position of the first occurrence of the character within the string. Returns the address of the character.

String Comparison

strcmp (string1, string2) compares strings string1 and string2 and returns a value less than zero if string1 is lexicographically less than string2, equal to zero if string1 is equal to string2 and greater than zero if string1 is lexicographically greater than string2.

Example 3.5

The strcmp () function returns an integer result, after comparing the two strings. in the following example, if the value of integer x is zero, it is inferred that the two strings are equal and if the value is not equal to zero, the result is that both the strings are not lexicographically equal.

#include <stdio.h>
#include <string.h>
int main() 
 {
      char stringl [10), string2 (10) ; 
       int x;
       printf ("Enter a word\n"); 
       scanf ("%s", string1) ;
       printf("Enter the word to be compared\n" ); 
       scanf ( "%s", string2 ) ; 
       x = strcmp (string1, string2 ) ;
       if (x = = 0) 
            printf ( "They are similar words \n ");
        else 
            printf( "They are not similar words \n"):
}

String copying

strcpy (string1, string2) copies the string string2 to string1, returning string 1.

This function provides an easy way to initialize a string. A string can be initialized easily, using this function, in the following form.

char name[10];

strcpy(name, "software")

This code copies the string "software into the character array, name [10], and automatically places the null terminator after the string.

Example 3.6

In the example given below, the function strcpy0, copies the string 2 to string1. If string1 is given as "Database" and the second string is given as "Administration", the output of the following program will be "Administration".

#include <stdio.h>
 int main() 
{
     char string1 [10] , string2 [10] ; 
     printf ("Enter the first word\n"); 
     scanf ( "%s", string1) ; 
     printf("Enter the word to be copied\n"); 
     scanf ("%s",string2) ; 
     strcpy (string1, string2 );
     printf("%s", s1);
}

String length

strlen(string) returns the number of characters in string, excluding the null character.

Example 3.7

The following example uses the strlen () function to compare the length of two strings.

#include <stdio.h>
#include <string.h>
int main()
{
    Int n1, n2;
    char s [20] ,s2 [20];
    printf ("Enter the two words\n"); 
    scanf ( "%s%s", s1,s2) ; 
    n1 = strlen(s1); 
    n2 = strlen(s2); 
    if (n1 == n2)
 	printf ("They are of equal size\n")
    else 
        printf ("They are not equal\n");
}