Strings in C-programming

Strings in C-programming

A string is a sequence of characters. Any sequence or set of characters defined inside double quotation symbols is a constant string. In c it is expected to do some significant operations on strings they are:

  • Studying string exhibiting strings
  • Combining or concatenating strings
  • Copying a single string to one more.
  • Evaluating string & checking whether they are equal
  • Extraction of a portion of a string

Strings are saved in memory as ASCII codes of characters that make up the string appended with ‘'(ASCII price of null). Usually each and every character is saved in a single byte, successive characters are saved in successive bytes.

Initializing Strings

Pursuing the discussion on characters arrays, the initialization of a string have to the following kind which is less difficult to a single dimension array.

char month1[ ]=‘j’,’a’,’n’,’u’,’a’,’r’,’y’

Then the string month is initializing to January. This is correctly valid but C features a distinctive way to initialize strings. The higher than string can be initialized char month1[]=”January” The characters of the string are enclosed inside a aspect of double prices. The compiler takes care of string enclosed inside a pair of a double prices. The compiler usually takes care of storing the ASCII codes of characters of the string in the memory and also suppliers the null terminator in the close.

/*String.c string variable*/ 
#contain < stdio.h> 
char month[fifteen] 
printf (“Enter the string”) 
receives (month) 
printf (“The string entered is %s”, month) 

In this illustration string is saved in the character variable month the string is shown in the statement.

printf(“The string entered is %s”, month”)

It is a single dimension array. Every character occupies a byte. A null character () that has the ASCII price terminates the string. The figure reveals the storage of string January in the memory recall that specifies a single character whose ASCII price is zero.








Character string terminated by a null character ‘’. 

A string variable is any valid C variable name & is constantly declared as an array. The typical kind of declaration of a string variable is

Char string_name[size]

The size establishes the amount of characters in the string name.


char month[ten] 
char deal with[100]

The size of the array should really be a single byte extra than the actual room occupied by the string since the complier appends a null character at the close of the string.

Studying Strings from the terminal:

The function scanf with %s format specification is required to read the character string from the terminal.


char deal with[fifteen] 
scanf(“%s”,deal with) 

Scanf statement has a draw again it just terminates the statement as soon as it finds a blank room, suppose if we kind the string new york then only the string new will be read and since there is a blank room just after term “new” it will terminate the string.

Be aware that we can use the scanf without the ampersand symbol ahead of the variable name. 
In quite a few applications it is expected to approach text by looking through an total line of text from the terminal.

The function getchar can be used consistently to read a sequence of successive single characters and retailer it in the array.

We cannot manipulate strings since C does not supply any operators for string. For instance we cannot assign a single string to one more straight. 

For illustration:


Are not valid. To copy the chars in a single string to one more string we may do so on a character to character foundation.

Creating strings to display:

The printf statement together with format specifier %s to print strings on to the display. The format %s can be used to show an array of characters that is terminated by the null character for illustration printf(“%s”,name) can be used to show the total contents of the array name.

Arithmetic operations on characters:

We can also manipulate the characters as we manipulate figures in c language. When at any time the procedure encounters the character details it is immediately transformed into a integer price by the procedure. We can characterize a character as a interface by applying the following approach.


Will show ninety seven on the display. Arithmetic operations can also be done on characters for illustration x=’z’-one is a valid statement. The ASCII price of ‘z’ is 122 the statement the therefore will assign 121 to variable x.

It is also possible to use character constants in relational expressions for example 
ch>’a’ && ch < = 'z' will check whether the character stored in variable ch is a lower case letter. A character digit can also be converted into its equivalent integer value suppose un the expression a=character-‘1' where a is defined as an integer variable & character contains value 8 then a= ASCII value of 8 ASCII value ‘1'=56-49=7.

We can also get the support of the c library function to converts a string of digits into their equivalent integer values the typical format of the function in x=atoi(string) in this article x is an integer variable & string is a character array that contains string of digits.

String operations (string.h)

C language acknowledges that string is a distinctive course of array by letting us input and output the array as a device and are terminated by null character. C library supports a big amount of string managing features that can be used to array out quite a few o f the string manipulations these as:

  • Length (amount of characters in the string).
  • Concatentation (introducing two are extra strings)
  • Evaluating two strings.
  • Substring (Extract substring from a presented string)
  • Copy(copies a single string more than one more)

To do all the operations described in this article it is necessary to contain string.h library header file in the program.

strlen() function:

This function counts and returns the amount of characters in a string. The length does not contain a null character.

Syntax n=strlen(string)

Wherever n is integer variable. Which receives the price of length of the string.



The function will assign amount of characters 9 in the string to a integer variable length.

/*writr a c program to uncover the length of the string applying strlen() function*/ 
#contain < stdio.h> 
contain < string.h> 
void primary() 
char name[100] 
int length 
printf(“Enter the string”) 
printf(“nNumber of characters in the string is=%d”,length) 

strcat() function:

when you merge two strings, you insert the characters of a single string to the close of other string. This approach is referred to as concatenation. The strcat() function joins two strings jointly. It usually takes the following kind


string1 & string2 are character arrays. When the function strcat is executed string2 is appended to string1. the string at string2 remains unchanged.



From the higher than program phase the price of string1 will become sribhagavan. The string at str2 remains unchanged as bhagawan.

strcmp function:

In c you cannot straight evaluate the price of two strings in a issue like if(string1==string2) 
Most libraries having said that consist of the strcmp() function, which returns a zero if two strings are equal, or a non zero amount if the strings are not the identical. The syntax of strcmp() is presented under:


String1 & string2 may be string variables or string constants. String1, & string2 may be string variables or string constants some desktops return a adverse if the string1 is alphabetically much less than the next and a positive amount if the string is larger than the next.


strcmp(“Newyork”,”Newyork”) will return zero due to the fact two strings are equal. 
strcmp(“their”,”there”) will return a 9 which is the numeric variation amongst ASCII ‘i’ and ASCII ‘r’. 
strcmp(“The”, “the”) will return 32 which is the numeric variation amongst ASCII “T” & ASCII “t”.

strcmpi() function

This function is identical as strcmp() which compares two strings but not case sensitive.


strcmpi(“THE”,”the”) will return .

strcpy() function:

C does not let you to assign the characters to a string straight as in the statement name=”Robert” 
As a substitute use the strcpy( function located in most compilers the syntax of the function is illustrated under.


Strcpy function assigns the contents of string2 to string1. string2 may be a character array variable or a string constant.


In the higher than illustration Robert is assigned to the string referred to as name.

strlwr () function:

This function converts all characters in a string from uppercase to lowercase.



For illustration:

strlwr(“EXFORSYS”) converts to exforsys.

Comments are closed.