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

Codzify's

1:1 & 1:Many Mentorship Program

Be job-ready. Learn how to crack the interviews with industry ready training program.


Learn More

Data Structures & Algorithms

What is Dynamic Programming and how to use it

This is a quick introduction to Dynamic Programming.

3 ways to check anagrams

Let's discuss the 3 ways to check anagrams and their time complexity.

Bubble Sort Algorithm

Topics covered: 1) Basics of Bubble Sort Algorithm 2) Worst Case, Best Case complexity

Introduction to Trees

What is a Tree and Terminologies like root, sibling, parent node, children, Leaf node, Ancestor & Descendant, Properties of Tree Data Structure, Depth of node, Height of node

Address Calculation in 2d array

Let's discuss about the Address Calculation using row-major order & column-major order.

Javascript

Javascript - Postfix Vs Prefix

Let's discuss the difference between postfix and prefix with a practical example.

Javascript Identifiers, Variables

In this session, we will discuss the concept of identifiers, variables and operators in javascript with a practical example.

Bootstrap

Bootstrap 4 Tutorial: Modal Popups Made Easy | Live Demo

Let's discuss the Modal Popups in Bootstrap.

How to create a dropdown list in navbar bootstrap

In this video, we will discuss How to create a dropdown list in navbar bootstrap with a practical example.

Navigation tabs in Bootstrap

Let's discuss about the navigation tabs in Bootstrap

Bootstrap Buttons

Let's discuss about the button classes in bootstrap.


Recommended tutorials