position: fixed; top: auto !important; margin-left: 112px;

Strings in C

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

Solve the Quiz of Article

1) What do you think the format specifier %s is used to print a string?
Yes
No

2) A string is a collection of characters terminated by '\0'?
Yes
No


Previous Next Article







codzify.com


Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

Social Profile


Linkedin
Twitter
Facebook

Copyright 2018. All rights reserved.