C & DS




  • D. Vijay Kumar

    Asst. Prof., Dept IT

    MGIT, Hyd

    A string in the C language is simply an array of characters. Strings must have a NULL or \0 character after the last character to show where the string ends. A string can be declared as a character array or with a string pointer.
    Declaring and Initializing String
    “String is a array of character.”

    A string variable is a variable declared as array of character. The general format of declaring string is:
              char string_name [size];
    Here ‘string_name’ is a variable name and ‘size’ indicates your string contains how many character means length of string. When the compiler assigns the string to the character array, it automatically put the null character (‘\0’) at the end of string. So the size of string should be maximum number of characters plus one.
              e.g. char city[10];
    Here the variable name is ‘city’ and size is 10.
    Initialization of string variable can be done by two ways. One is a simple method which we are using in initialization of array.
         char string_name[size] = { list of the character in string within single cot};
    Another method is in which we assign the string directly within double cot.
         char string_name[size] = “String”;
         e.g. char name[10] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’};
    In first case we have to put null character at the end of string explicitly. While by using second method we can do directly.
         char name[10] = “Hello”;
    We can also initialize the string variable without specifying the size of variable.
         char name[ ] = “Hello”;

    Printing String
    To print the string we are using same ‘printf’ function but we have to specify the output format as ‘%s’
         printf(“%s”, string_name);
         e.g. char name[20] = “Swaminarayan”;
         printf(“%s\n”, name);

    Above statement print the string ‘name’. Output is “Swaminarayan”.
    We can also format the output of string as under:
    Here ‘w’ indicates the total width, ‘p’ indicates no. of characters you want to print, and ‘s’ indicate we are printing string value.
         e.g. char str[100]=”Hello”;
         printf(“%10.3s”, str);

    Here in output we are getting total width=10 but the output character is only 3 that is “Hel” since we have ‘p’ value =3.
    We can also use ‘puts’ function to print the string

    Here there is no need to put the new line character after the string variable but function itself automatically put the new line after the string.
         e.g. puts(name);
    It will print the value of variable ‘name’ with new line at end.

    Reading String
    To read we can use same ‘scanf’ function:
         scanf(“%s”, string_name);
    But here we have to not put ‘&’ operator because string variable automatically determine its address. We you complete the reading of string your computer put null character automatically at the end of string.
         There is one limitation of string reading by using simple scanf function. It cannot read white space (space, tab, new line). That means you cannot read entire string “Jay Swaminarayan” by using simple function since it contains white space.
    There are many solutions to read entire string with white space:
         1. We have to read the entire string character by character in loop up to we are not entering new line character. At the end of string we have to explicitly put the null character to indicate the end of string.

              char c, name[100];
              int i=0;
              do {
              c=getchar( ) ;
              name[i] = c;
              } while (c != ‘\n’);
              name[i] = ‘\0’;

         2. We have to use ‘gets’ function which read the string up to new line.
              char name[100];
              gets( name ) ; // this will read the string up to new line

         3. We can use same scanf function with formatting to read the string.
              scanf(“%[^\n]”, name);

    Here we have used ‘%[^character]” format means your computer read the string up to you are not entering the new line character.

    Arithmetic operations on characters:
    C allows the manipulation of characters similar to number. Whenever character constant is used in expression of arithmetic operations it is automatically converted into integer value by system. The integer value depends on the local character set used by system. Generally it is ASCII character set.
         e.g. char c = ‘a’;
         printf(“%d”, c);
    Here we have assigned ‘a’ to character variable ‘c’. We are using the %d format in printf statement means we are getting equivalent integer number assigned by the system to ‘a’. In our case it is 97 (ASCII value of ‘a’)
         char c;
         c = ‘z’ –1;
         printf(“%c”, c);
    Here first system determine the integer equivalent to ‘z’ (122) and subtract 1 from it (122-1) and then assign to ‘c’. (121 is assigned to ‘c’). In printf by using %c we are getting character equivalent to 121 means we get ‘y’ in output.
         int n=65;
         printf(“%c”, n);
    Here the character equivalent to number 65 is determined (which is A) and that character we are getting in output.
         char c;
         printf(“Enter your character : “);
         scanf(“%c”. &c);
         if(c >= ‘0’ && c < = ‘9’) {
         printf(“Your character is digit”);

    Here we are reading the value of character ‘c’. After getting it we check whether the integer equivalent of it is in between integer equivalent to ‘0’ and ‘9’. And it is there then your character is digit and we are getting output “Your character is digit”.

    ‘atoi( )’ function:
    This function is used to convert string into number. The format of this function is:
         int atoi(string);
    We have to pass the string as argument and function returns integer equivalent to string.
         e.g. int a;
         a = atoi(“123”);
    Here after executing above statement variable ‘a’ contains number 123.
         int a;
         a = atoi(“123H45”);

    If you insert any non digit character in between your number string the conversion is truncated when your computer encounter non digit character. Here value of ‘a’ is123 only.

         int a;
         a = atoi(“F123”);

    If your string contains first character as non-digit character then the return value is zero. Here the output of ‘a’ is 0.

    String handling function:
    In C it is not allow to assign one string value into another directly by using assignment operator. Also we cannot compare two string with using equal to operator (=  =). Similarly the joining of two strings cannot possible by using simple addition operator (+). For that purpose C provides some special function. These all functions are included into string.h header file.

    1. strlen( ) function:
    This function is used to determine the length of string. That means this function return integer number, your string contains how many characters. The format of the function is:
         int strlen(string);
    In function we have to pass string variable or string constant and function returns integer value which indicate the length of string.
         e.g. int l;
         char name[100];
         l = strlen(name); // Give the length of string
         printf(“Length of string = %d\n”, l);

    2. strcpy( ) function:
    This function is used to copy or assign the value of one string into another string. This function contains two arguments and returns nothing.
         void strcpy(destination, source);
    Function has two arguments one is ‘source’ from which we have to copy the string and another is ‘destination’ into which we have to copy the string.
         e.g. char str1[100] = “Hello”;
         char str2[100];
         strcpy(str2,str1); // Copy the value of str1 into str2
    Here we have two string variable one is ‘str1’ another is ‘str2’. First variable is initialized by the value “Hello” and second variable contains no fixed value. When we are using ‘strcpy’ function we have to pass ‘str2’ as destination and ‘str1’ as source.

    3. strcat( ) function:
    This function is used to join two strings. Function has two arguments and returns nothing.
         void strcat(destination, souce);
    Function has one argument ‘destination’ another is ‘source’ and returns void. ‘destination’ is a string which contains the joined string after execution of function and ‘source’ contains the string which we have to join with the previous value of ‘destination’ before execution of function.
         e.g. char str1[100] = “Hello”;
         char str2[100] = “How r u?”;
         strcat (str1, “ “); // Join space as string to str1
         strcat (str1, str2); // Join str2 to str1
         printf(“The joined string is %s\n”,str1);
    In above example first string variable ‘str1’ is initialized by “Hello” and second ‘str2’ is initialized by “How r u?”. Then we call function ‘strcat’ to join the space with the ‘str1’. Now your ‘str1’ value is “Hello “. Then again we call ‘strcat’ function to join ‘str2’ with the ‘str1’. So after execution ‘str1’ contains the value “Hello How r u?” and ‘str2’ is remain as it is.

    4. strcmp( ) function:
    This function is used to compare two strings. It determines which string is larger or smaller out of two strings. The format of function is:
         int strcmp ( string1, string2);
    Here one argument is string1 and another is string2. We want to compare two strings. The return value is integer. Depending on the return value we can judge which is larger string and which is smaller string. If string1 is larger then the return value is positive, if string1 is smaller then return value is negative, and if both strings are equal then return value is zero.

         string1 > string2 output > 0
         string1 < string2 output < 0
         string1 = string2 output = 0

         e.g. char str1[100] = “pqr”;
         char str2[100] = “xyz”;
         int a;
         a = strcmp ( str1, str2); // Compare str1 and str2
         if (a > 0 ) {
         printf(“String1 is larger\n”);
         else if(a < 0 ) {
         printf(“String1 is smaller \n”);
         else {
         printf(“Both strings are equal\n”);

    Here string1 is smaller then the string2 so the output of ‘a’ is negative value and we get output “String1 is smaller”.

    Array of String:
    When we need the array of the string then we have to use two-dimensional array of characters. The format of the array of the string is:
         char string_name[array size][length of each string];

    We have to specify two dimensions, one is size of the array and another is the length of each string.
         e.g. char name[10][100];
    Here we have the array of the string ‘name’ which size is 10. That means we have 10 different ‘name’ elements. The length of the character is 100 means each string elements can contains maximum 100 characters.

    To read the string or print the string or manipulate the string elements we have to use array subscript.
         e.g. scanf(“%s”, name[0]); // Read first array string element
         scanf(“%s”, name[1]); // Read second array string element
         printf(“%s\n”, name[0]); // Print first array string element
         prin tf(“%s\n”, name[1]); // Print second array string element


Industry      Interaction

Higher Education

Job Skills

Soft Skills

Comm. English

Mock Test