Strings in C Programming

In datatypes of C, we have learned character datatype. It stores single character in it and occupies 1 byte of space. This will support the use of only one character at a time. But any programs will not end with single characters. There will be words and sentences to be input or output in any program. For example, consider a program which accepts student / employee / department/ stores/ book names and displays it. These names will have one or more words. In C terminology we call them as strings. Since character datatype supports only single character at a time, we cannot use a character variable to store these values. We cannot even create multiple character variables to store single word; moreover it is not feasible. Hence we use the feature of array with character types to store string values.

In C, we do not have any datatype to store string values. Rather we use the character arrays to store the string values. When we use a variable with character type, compiler knows that it can store only one character. But in the case of strings, it depends on the number array element. Though we have predefined the number of elements in the array, the string that we are going to store will not occupy all the elements in the array. It may have less number of characters than array elements. We know that any uninitialized members of the array will have garbage values. Hence if we try to print the string with shorter size, along with the string value it may print garbage values too. In order to avoid these garbage values to be displayed or being used in any of the operations, we add a null terminator at the end of the string that we store to indicate the end of string. Thus the compiler will know that end of the string has reached and no need to display or traverse further in the array. This null terminator is denoted by ‘\0’. But this null terminator will not be displayed or used any string manipulations. It is just the indication for the compiler about the end of the string / sentence. It occupies 1 byte of space. It is always necessary to remember that string length is length of the string + 1 byte of null character.

String Declaration   

Any string is considered as an array of type character or a pointer to characters (this is discussed in pointer section). Hence we can declare any string variable as array with datatype as character.

char chrArr [10]; // is a character array of 10 elements
char chrName [15]; // allows to store names with 14 character + 1 byte of null terminator.
char *chrPtr; //is a pointer to the character. it points to the beginning character of the string

 

 

String Initialization

When we declare a string variable (array of characters), it creates a memory space in the RAM. If we have specified the number of elements in the array, then it will create so many bytes of space in memory. Actual values for the string are assigned when we initialize the string variable. String variables can be initialized in different ways:

While declaring a variable

String variables can be initialized while declaring the variable itself. This can be done in different ways – using the method of initializing the array or by directly assigning the string value.

char chrString[15] = { 'S', 't', 'r', 'i', 'n', 'g' ' ', 'V', 'a', 'l', 'u', 'e', '\0'}; 
char chrString[] = { 'S', 't', 'r', 'i', 'n', 'g' ' ', 'V', 'a', 'l', 'u', 'e', '\0'}; // automatically calculates number of elements of array as 13
char chrString [] ="Example"; // this type of initialization does not take more than one word
char chrString [15] = "String Value"; // this can have more than one word in initialization value
char *chrPtr = "C String Initialization";

Above are the different methods of string initialization while declaring the string. In the first and second method, it uses the regular array initialization technique to initialize the string. It initializes the each element of the array using the characters of the string and terminates the string by adding null character, ‘\0’.

In the third method, we have not specified the number of elements of the array, but the initialization value determines the number of elements in the array. But this type of initialization can take only one word. It cannot accept any white space within the string. Here whole string itself is entered as string value by using double quotes. This will automatically assigns each character to the array elements and adds null character at the end. We need not specify ‘\0’ at the end of the string.

Forth method is another way of initializing the string where we specify the number of elements. Hence it can take multiple words as string. When the string value is complete, it automatically adds ‘\0’ at the end to the array element.

Last method is another way of string initialization using character pointer. The pointer variable will be initially pointing to the beginning address of the string. As the number of character increases in the string, the pointer to the character is moved further to store the whole string. More details about this is discussed in pointer section.

After declaring the string

In this method we cannot assign any string to the string variable directly as shown above. We need to use string copy function, strcpy () to copy the string value to the string variable. i.e.;
strcpy chrString, "String Value"); //copies the string value to string variable

When we use character pointer, we cannot direct assign the values. We have to make it to point to the address of another string, which is equivalent to assigning the values to the pointer. i.e.;

chrPtr = chrString;// copies the address of chrString to pointer variable, hence gets the value of chrString

#include 
#include 

void main(){
	char chrString[15];
	char *chrPtr;
 
  	 strcpy(chrString, "String Value"); //copies the string value to string variable
	printf("  \nchrString[15] = \"String Value\"== > %s ", chrString);
 
	chrPtr = chrString;// copies the address of chrString to pointer variable, hence gets the value at chrString
	printf("\nValue of String Pointer is: %s", chrPtr);
}

 

Inputting value using files or keyboard

String values can be input using keyboard or standard input file. We use scanf function to input the values. The scanf function accepts the value from the keyboard or user and puts the value at the address pointed by the variable name. Hence it we use address operator to accept the values into the variable. But here we are going to insert string values into an array of characters. When we specify the array name without any index they act like a pointer – it points to the address of the first element of the array. Hence when we accept the values using character array we need not specify address operator. But the scanf function accepts only one word at time. If we need to enter the string with multiple words, then we can use gets () function. Below example program illustrates the same.

#include 
#include 

void main(){
	char chrString[20];
 	printf("\nPlease enter any String Value:");
	gets(chrString); // Accepts multiple words of string value
	printf("\nEntered String Value using puts() function is:");
	puts(chrString);
	printf("\nPlease enter another String Value:");
	scanf("%s", chrString);// Accepts single word of string value
	printf("\nEntered String Value is : %s", chrString);
}

 

String Access

String variables can be accessed like any other variable. Even though it is an array of characters, when accessed as a normal variable, they give the whole value stored in it. But it is not true with integer array.

If we need to see whole string value in the variable, we can use ‘%s’ instead of ‘%c’ in the printf function. if we use %c, it will display only one character while %s will display whole string. The function puts () will display the whole string without any issue.

printf("  \Value at chrString is: %s ", chrString);
puts (chrString);

#include 
#include 

void main(){
	char chrString[20]; 
	 strcpy(chrString,"String Example"); //copies the string value to string variable
	printf(" \nValue at chrString using printf function is: %s ", chrString);
 	printf(" \nValue at chrString using puts() function is: ");
	puts(chrString);
}


String Functions

Since string is an array in C, performing various operations like copying a string to another variable, comparing two strings, finding the length of the string etc involve lots of steps. But these are the most common operations performed on the string. Hence whenever a string is used in the program, we cannot write the programs for all these operations every time. If we have some general  function created for these strings, then it can be reused whenever it is requires. Thus C has created lots of general functions on strings and has stored in string.h header files. Whenever we use strings and their functions, if we include the preprocessor directive string.h in the program, it makes our tasks easier.

Most commonly used string functions in C are :

Strlen ()

This function in C is used to find the length of the string. It returns the number of characters in the string. It does not consider last null terminator while calculating the length of the string. It returns an integer value which is the length of the string that is passed to the function as argument.

intStrLen= strlen(chrStr);

A simple program which accepts the string value from the user and finds the length of it is shown below.

#include 
#include 

void main(){
	char chrStr[50];
	int intStrLen;

	printf("\nPlease enter any String Value:");
	gets(chrStr); // Accepts multiple words of string value
 
	intStrLen= strlen(chrStr); //Returns the string length
	printf("\nLenght of the string is: %d", intStrLen);
}

 

Strcpy ()

If a string has to be copied to another string variable, we have to copy it character by character – element by element of array to another string variable. Same is done in a function called strcpy () and is made available to use whenever required. It is also available in string.h header file.

The general syntax for strcpy is as shown below:

strcpy(string1, string2);

It copies the value in string2 to stirng1. If the size of the string1 is smaller than the string2, then it copies only those many characters as string1 has. Rest of the characters is discarded.

#include 
#include 

void main(){
	char chrStr[50];
	char chrCopyStr[50];
 
	printf("\nPlease enter any String Value:");
	gets(chrStr); // Accepts multiple words of string value
	printf("\nEntered String Value is:");
	puts(chrStr);

	strcpy(chrCopyStr, chrStr); // Copies the value at chrStr to chrCopyStr
	printf("\nCopied String Value is: %s", chrCopyStr);
	 
}

 

Strcat ()

In a code we might have to combine two strings into one. This is also done by copying character by character of second string and appending at the end of the first string. It also needs to check the size of the first string is sufficient to hold all the value in second string .

the same checking and appending the values to another string is done by string function strcat(). It concatenates two strings. The general syntax for it is:

strcat(string1, string2);

it copies the value of string2 into string1 and the concatenated result is found at string1 itself. When it concatenates two strings, it removes the null terminator at the end of first string and starts appending it. It does not add any space between first string and second string. It adds the string soon after the first string ends. The same can  be observed in below program.

#include 
#include 

void main(){
	char chrStr1[50], chrStr2[50];
 
	printf("\nPlease enter first String Value:");
	gets(chrStr1); // Accepts multiple words of string value
	printf("\nPlease enter second String Value:");
	get(chrStr2); // Accepts multiple words of string value
   
	strcat(chrStr1, chrStr2); // Combines the value at chrStr2 with chrStr1 and stores the result at chrStr1
	printf("\nCopied String Value is: %s", chrStr1);
	 
}

 

Strcmp ()

In any string program, one of most common requirement is to compare the strings. Comparing any two strings is not direct. It is again character by character. The same is performed in strcmp function, which compares two strings and returns integer value depending on the comparison results. It returns

  • 0, if both the strings are same.
  • -1, if they are not equal and first string is less than second string.
  • 1, if they are not equal and first string is greater than second string.

When we say less than or greater than a string, it actually fetches the ASCII value of the string character and checks if it is greater or less than the other character. The string comparison is actually done on the ASCII values of the character than the actual value of the character in the string.

The general syntax for string compare would be:

intResult = strcmp(string1, string2);

It compares the string1 with string2 and returns the result to intResult.

A simple program below shows how to use strcmp to compare two strings.

#include 
#include 

void main(){
	char chrStr1[50], chrStr2[50];
	int intResult;

	printf("\nPlease enter first String Value:");
	gets(chrStr1); // Accepts multiple words of string value
	printf("\nPlease enter second String Value:");
	gets(chrStr2); // Accepts multiple words of string value

	intResult = strcmp(chrStr1, chrStr2); // Compares two strings chrStr1 and chrStr2 and returns the result to intResult
	
	if (intResult == 0)
		printf("\nBoth the strings are same!");
	else if (intResult < 0)
		printf("\nString 1 is smaller than String 2");
	else
		printf("\nString 1 is greater than String 2");
}

 


Next > < Prev
Scroll to Top