Test your Programming knowledge

50% users failed to get the best score. It's your turn to test now.
Questions curated by the expert mentors at codzify.com

Start Quiz

Article

Strings in C, String Manipulations In C Programming Using Library Functions

3 min 58 sec read

Do you know the character? 'a', 'b', 'C' etc.. Yeah these are characters . Strings in c is simply an array of characters. C strings or character arrays look like this in memory.

M A N I S H \0

'\0' at the end of an array is called as 'Null Character'. Null Character indicates the end of the string. Whenever compiler parses the array one by one through index, how does it knows where the string gonna ends. So this '\0' character indicates the end of the string.

Example :-

void main( )
{
char name[ ] = "Manish" ; 
int i=0;

   while ( i <= 7 ) 
   {
     printf ( "%c", name[i] ) ;
     i++ ;
   } 
}  

Output :-

Manish

This program simply loops through character array named 'name' and gets the character value one by one. It's simple.

while ( i <= 7 )

This condition tells the compiler to stop the loop. But this works only for static strings. That means we initialized a string at the start and we know that string remains same till the entire lifetime of the program. Now the question is,

What in case of Dynamic Strings?

Dynamic Strings are like allow the user to enter any name. And we know that names must be of different lengths like Manish , ABC, PQR etc. So how our while loop condition gets to know the length of the string? And how does it knows where it gonna ends?

So with the help of '\0' NULL Character, we get where the string ends. So condition in that case becomes,

while ( name[i] != `\0' )

That means while loop checks each time whether the character value is the NULL Character or not. If it founds NULL character '\0' loop stops.

void main( ) 
{
char name[ ] = "Manish" ; 
int i=0;
   while ( name[i] != `\0' ) 
   {
   printf ( "%c", name[i] ) ;
   i++ ; 
   }
}

Output :-

Manish

Standard Library String Functions

Functions Purpose
strlen Finds the length of a string
strlwr Converts a string to lowercase
strupr Converts a string to uppercase
strcat Appends one string at the end of another
strncat Appends first n characters of a string at the end of another
strcpy Copies a string into another
strncpy Copies first n characters of one string into another
strcmp Compares two strings
strncmp Compares first n characters of two strings
strcmpi Compares two strings without regard to case ("i" denotes that this function ignores case)
stricmp Compares two strings without regard to case (identical to strcmpi)
strnicmp Compares first n characters of two strings without regard to case
strdup Duplicates a string
strchr Finds the first occurrence of a given character in a string
strrchr Finds the last occurrence of a given character in a string
strstr Finds the first occurrence of a given string in another string
strset Sets all characters of string to a given character
strnset Sets first n characters of a string to a given character
strrev Reverses string

Recommended Articles

Did you found this article helpful?

Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor