String Handling

There are many library function for string handling. To use that we have to include string library , which we can do by writing

#include<string.h>.

One by one we are going to discuss library function and other method to perform same task without using library function.

strlen : string length

 This function is used to find length of string.

<int variable> = strlen(<name of string variable>);

Example program of strlen with using library function.

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
  char str[]="abcd";
  int i;
  clrscr();
  i=strlen(str);
  printf("length of string = %d ",i);
  getch():
}

Output:

Example for doing same thing without using string function.

 In this program, for loop is working till it does not get null ‘\0’, so when it gets null, it prints last value of i which is length of string.

#include<stdio.h>
#include<conio.h>
void main()
{
char str[]="abcd";
int i;
clrscr();
    for(i=0;str[i]!='\0';i++);
    printf("length of string = %d ",i);
getch();
}

Output:

strcmp : string compare

This function is used to compare two strings. Function returns negative value if 1st string is bigger than 2nd , positive value if 1st string is smaller than 2nd , zero if both are same. This comparison is of ASCII value of 1st character of both string and if both are same then 2nd and so on.

<int variable> = strcmp(<name of 1st string variable>,<name of 2nd string variable>);

Example program of strlen with using library function.

#include<stdio.h>
#incluse<conio.h>
#include<string.h>
void main()
{char str1[]="abcd",str2[]="ABCD";
int i;
clrscr();
i=strcmp(str1,str2);
    if(i>0)
    printf("str1 > str2");
    else if(i<0)
    printf("str1 < str2");
    else if(i==0)
    printf("str1 = str2");
getch();
}

Output:

Example program of strlen without using library function.

The for loop here has 3 conditions so loop runs until str1 is not ended (1st condition), str2 is not ended(2nd condition) and if any of string reached to end then it jumps out from loop and makes comparison of next character of strings. Now taking about 3rd condition, this says that this loops runs if both strings have same character and then loop jumps to comparison of next character and so on.

#include<stdio.h>
#include<conio.h>
void main()
{
char str1[]="abcd",str2[]="ABCD";
int i;
clrscr();
    for(i=0;str1[i]!='\0' && str2[i]!='\0' && str1[i]==str2[i];i++);
    
    if(str1[i]==str2[i])
    printf("same strings");
    else if(str1[i]>str2[i])
    printf("str1>str2");
    else if(str1[i]<str2[i])
    printf("str2>str1");
getch();
}

Output:

strcmpi

This is same as strcmp, but ‘i’ stands for ‘ignore’. Means it ignores case so letters in small casa and capital case are treated in same way.
Ex. ABCD and abcd both are same.

<int variable> = strcmpi(<name of 1st string variable>,<name of 2nd string variable>);

C program is same as strcmp but just ignore the case.

strcpy : string copy

strcpy is used to copy one string(source string) to another string(destination string). While copying strings, the old content of destination string is completely erased.

strcpy(<name destination string variable>,<name of source string variable>);

Example program to demonstrate strcpy using string function

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
  char str1[]="abcd",str2[]="ABCD";
  clrscr();
  strcpy(str1,str2);
  printf("str1=%s ,str2=%s",str1,str2);
  getch();
}

Output:

Example program of strcpy without using string function.

Condition in for loop is used to check end of str2, if it is not reached to end then it copies character to destination string. Here we performed the same copying task as done by string function strcpy.

But here we did not add NULL character ‘\0’ at end of destination string.(Which was automatically performed in library string function strcpy). So for that, after for loop we have to write a line to add NULL character ‘\0’ at end of str1.

#include<stdio.h>
#include<conio.h>
void main()
{ 
  int i;
  char str1[]="abcd",str2[]="ABCD";
  clrscr();
  for(i=0;str2[i]!='\0';i++)
    str1[i]=str2[i];
    str1[i]='\0';
  printf("str1=%s ,str2=%s",str1,str2);
  getch();
}

Output:

strncpy

 This is same as strcpy, but in this we have to define that how much letters we want to copy.

<int variable> = strncpy(<name of 1st string variable>,<name of 2nd string variable>,n);

C program is same as strcmp but add defined number of letters

strcat : string concatenation

 This is used to concatenate two strings.

strcat(<destination string>,<source string>);

Example program to demonstrate use of strcat

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
  int i;
  char str1[]="abcd",str2[]="ABCD";
  clrscr();
  strcat(str1,str2);
  printf("str1=%s ,str2=%s",str1,str2);
  getch();
}

Output:

Example program to do same task as done by strcat, without using string function

Here 1st for loop is used to find length of string1 so we can get last index of str1(which is done by finding length of string1) and after that we can concatenate 2nd.

That thing is done by 2nd for loop. 2nd for loop copies one by one characters of str2(using j variable) to str1(using i variable) until there is end of str2 which is ‘\0’.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j;
char str1[]="abcd",str2[]="ABCD";
clrscr();
for(i=0;str1[i]!='\0';i++);

for(j=0;str2[j]!='\0';j++,i++)
{
    str1[i]=str2[j];
}
str1[i]='\0';
printf("str1=%s ,str2=%s",str1,str2);
getch();
}

Output:

strncat

 This is same as strncat, but in this we have to define that how much letters we want to concatenate.

<int variable> = strncat(<name of 1st string variable>,<name of 2nd string variable>,n);

C program is same as strcat but add defined number of letters.

strrev

 This is used to reverse string. After performing below program, string is reversed automatically.

strrev(<string variable name>);
strupr

This is used to convert all string letters into capital. If any is capital then it remains as it is.

strupr(<string variable name>);
strlwr

 This is used to convert all string letters into small letters. If any is small letter then it remains as it is.

strlwr(<string variable name>);

Learning  from this blog:

1 ) Library including for string function.
2) What is use of strlen, strcpy, strncpy, strcmp, strcmpi, strcat, strncat, strrev, strupr, strlwr.
3 ) Syntax of library functions.
4 ) Programs using library function.
5 ) Programs without using library function.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments