String Functions / Operations in C
String Functions / Operations in C
1) strlen() in C :
This function is used to determine the length of a given string.
Syntax :
variable=strlen(string);
Sample Program
str_length.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30];
int l;
printf("Enter a string : ");
scanf("%s",&a);
l=strlen(a);
printf("\n Length of the Given String is : %d",l);
getch();
}
Output :
2) strcpy() in C :
This function copies the string from one variable to another variable.
Syntax :
strcpy(source_variable, destination_variable);
Sample Program
str_cpy.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30],b[30];
printf("Enter a string : ");
scanf("%s",&a);
strcpy(b,a);
printf("Value of a : %s",a);
printf("\nValue of b : %s",b);
getch();
}
Output :
3) strncpy() in C :
This function also copies the string from one variable to another variable, but only
upto the specified length.
Syntax :
Sample program :
strn_cpy.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30],b[30];
printf("Enter a string : ");
scanf("%s",&a);
strncpy(b,a,3);
printf("Value of a : %s",a);
printf("\nValue of b : %s",b);
getch();
}
Output :
4) strcmp() in C :
This function is used to compare two strings. They are case sensitive.
Syntax :
strcmp(string1,string2);
If both the strings are equal return value will be 0, else non_zero value will be
returned.
Sample program :
str_cmp.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30],b[30];
int n;
printf("Enter string one : ");
scanf("%s",&a);
printf("Enter string two : ");
scanf("%s",&b);
n=strcmp(a,b);
if(n==0)
{
printf("Both the Strings are Equal");
}
else
{
printf("Strings are not Equal");
}
getch();
}
Output :
5) stricmp() in C :
This function is also used to compare two strings but they are not case sensitive.
Syntax :
stricmp(string1,string2);
Sample program :
stri_cmp.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30],b[30];
int n;
printf("Enter string one : ");
scanf("%s",&a);
printf("Enter string two : ");
scanf("%s",&b);
n=stricmp(a,b);
if(n==0)
{
printf("Both the Strings are Equal");
}
else
{
printf("Strings are not Equal");
}
getch();
}
Output :
6) strncmp() in C :
This function compares two strings only upto a specified length. They are case-
sensitive.
Syntax :
strncmp(string1,string2,length);
Sample program :
strn_cmp.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30],b[30];
int n;
printf("Enter string one : ");
scanf("%s",&a);
printf("Enter string two : ");
scanf("%s",&b);
n=strncmp(a,b,3);
if(n==0)
{
printf("Both the Strings are Equal upto the first 3 characters");
}
else
{
printf("Strings are not Equal");
}
getch();
}
Output :
7) strnicmp() in C :
This function also compares two strings upto a specified length, but not case-sensitive.
Syntax :
strnicmp(string1,stringn,length);
Sample program :
strni_cmp.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30],b[30];
int n;
printf("Enter string one : ");
scanf("%s",&a);
printf("Enter string two : ");
scanf("%s",&b);
n=strnicmp(a,b,3);
if(n==0)
{
printf("Both the Strings are Equal upto the first 3 characters");
}
else
{
printf("Strings are not Equal");
}
getch();
}
Output :
8) strlwr() in C :
This function converts uppercase characters to lowercase.
Syntax :
strlwr(string);
Sample program :
str_lwr.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30];
printf("Enter a string in uppercase : ");
scanf("%s",&a);
printf("RESULT : %s",strlwr(a));
getch();
}
Output :
9) strupr() in C :
This function converts lowercase characters to uppercase.
Syntax :
strupr(string);
Sample program :
str_upr.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30];
printf("Enter a string in lowercase : ");
scanf("%s",&a);
printf("RESULT : %s",strupr(a));
getch();
}
Program Algorithm / Explanation :
Output :
10) strdup() in C :
This function is used to duplicate a string to a pointer variable by allocating the
memory location of the string.
Syntax :
pointer=strdup(string);
Sample program :
str_dup.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30],*b;
printf("Enter a string : ");
scanf("%s",&a);
b=strdup(a);
printf("Entered String in a : %s",a);
printf("\nDuplicated string : %s",b);
getch();
}
Output :
11) strcat() in C :
This function is used to join (concatenate) two strings.
Syntax :
strcat(string1,string2);
Sample program :
str_cat.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30],b[30],c[30];
printf("Enter string one : ");
scanf("%s",&a);
printf("Enter string two : ");
scanf("%s",&b);
strcat(a,b);
getch();
}
Output :
13) strrev() in C :
This function is sued to reverse the characters in a given string.
Syntax :
strrev(string);
Sample program :
str_rev.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30];
printf("Enter string : ");
scanf("%s",&a);
strrev(a);
getch();
}
Output :
14) strset() in C :
This function replaces all the characters of a string with a given symbol or character.
Syntax :
strset(string,symbol);
Sample program :
str_set.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30];
char b;
strset(a,b);
printf("After strset : %s",a);
getch();
}
Output :
15) strnset() in C :
This function also replaces the characters of a string with a given symbol but only to a
specified length.
strnset(string,symbol,n);
Sample program :
strn_set.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30];
char b;
strnset(a,b,2);
getch();
}
Output :