The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

Strings in C

A string is an one dimensional character array which is terminated by a null ( \0 ) character. Each character of the array occupies 1 byte of space in memory.In any programming language, string is a data structure used for text manipulation. Let's see how strings are represented and manipulated in C.

String Representation in C
C language supports string representation with an array of characters terminated by a null character ' \0 '. The null character is a must because it is the only way for compiler to know the end of string. Consider the following statements to initialize a string (character array) :

char str [6] = { 'W', 'O', 'R', 'L', 'D', '\0' }; /* Initializing a NULL terminated string 'WORLD' */
char str [ ] = { 'W', 'O', 'R', 'L', 'D', '\0' }; /* Array dimension not specified */
char str [ ] = " WORLD " ; /* Another way of initializing */

Please note that the second statement doesn't contain ' \0 ' since in this case, NULL character is automatically appended by the C compiler.

Standard string manipulation function
The header file < string.h > provides a number of standard functions which can be used for string manipulation. Some of the most commonly used functions are :

FunctionPurpose
strlen ( str1 )Returns the length of string str1
strcpy ( str1, str2 )Copies string str1 into str2
strcat ( str1, str2 )Appends string str2 at the end of string str1
strcmp ( str1, str2 )Compares string str1 and str2 character by character and returns 0 if
they are equal, else returns the difference of ascii values of the first
mismatching characters of the two strings.

There are a lot of standard functions offered by the library. Following program demonstrates the use of some string manipulation functions :

#include <stdio.h>
#include <string.h>

int main() {
   char str1[20] = { 'H', 'e', 'l', 'l', 'o', '\0' };
   char str2[] = "World";
   char str3[10]; /* declare a string to store 10 characters */
   int len = strlen(str1); /* get the length of str1 */
   printf("Length of str1 is %d\n", len);
   printf("str1 : %s\nstr2 : %s\n", str1, str2);
   strcpy(str3, str2); /* copy str2 into str3 */
   printf("str3 : %s\n", str3);
   if (strcmp(str2, str3) == 0) { /* compare two strings */
      printf("Two strings are equal\n");
   }
   else {
      printf("Strings are not equal\n");
   }
   strcat(str1, str2); /* concatenate/append str2 to str1 */
   printf("After concatenating, str1 : %s\n", str1);
   return 0;
}


Pointer to Strings
A string is stored in some location in memory. We can use a character pointer to hold the address of that memory location. Precisely, the character pointer stores the address of the first character of the array. Since all the characters are stored in contiguous memory location, we can use the character pointer to access and manipulate the string characters. Following program illustrates the use of pointer to strings.

#include <stdio.h>

int main() {
    char str[] = "csegeek";
    char *sptr = "program"; /* declare and initialize a pointer to a string */
    char *ptr;  /* declare character pointer [ can point to a string ] */
    ptr = str;  /* 'ptr' now points to string 'str' */
    printf("ptr points to string : %s\n", ptr);       /* prints "csegeek" */
    printf("ptr stores the address of : %c\n", *ptr); /* prints 'c' */
    ptr++; /* increment ptr to now store the address of 's' */
    printf("ptr now stores the address of : %c\n", *ptr); /* prints 's' */
    printf("ptr now points to string : %s\n", ptr);   /* prints "segeek" */
    printf("sptr points to string : %s\n", sptr);     /* prints "program" */
    ptr = sptr; /* ptr now points to sptr */
    printf("ptr now points to string : %s\n", ptr);   /* prints "program" */
    return 0;
}


Two Dimensional Array of Characters
We can use a 2 dimensional character array to store multiple strings. Following program illustrates the use of 2 dimensional character arrays.

/*
 * This program inputs 3 names and greets them differently
 */

#include <stdio.h>

int main() {
    char names[3][20]; /* we can store 3 strings of max 20 chars each */
    /* declare and initialize a two dimensional character array */
    char greet[3][10] = { "Hi!", "Hello!", "Hey!" };
    int i;
    for ( i = 0; i < 3; i++ ) { /* input and store three names */
        printf("Enter Name %d : ", i+1);
        scanf("%s", &names[i]);
    }
    for ( i = 0; i < 3; i++ ) { /* display and greets the input names */
        printf("%s %s\n", greet[i], names[i]);
    }
    return 0;
}
Run this program in your system to take input at run-time  

Back | Next