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

3 min 58 sec read Basic


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

How was the tutorial? Nice. Right?



Deep Concept videos to crack the highly-piad interviews.

Chekout out our Youtube Channel to get detailed video content on important topics in interviews.



What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!

Have questions about the trial coding class?

Chat with our experts to discuss

Connect on WhatsApp


Recommended tutorials

#online compiler for c     #python for programming    

#dfs and bfs algorithm     #programming with c language

#storageclass in C    #listcomprehension in python