Example : Find the length of given string in C using string function [strlen()].
#include <stdio.h>
#include <string.h>
int main()
{
char str[30] = "Codershelpline";
int length;
length=strlen(str);
printf("Length of given string is = %d", length);
return 0;
}
Output :
Length of given string is = 14
--------- OR ----------
#include <stdio.h>
#include <string.h>
int main()
{
char str[30] = "Codershelpline";
printf("Length of given string is = %d", strlen(str));
return 0;
}
Output :
Length of given string is = 14
--------- OR ----------
#include <stdio.h>
int main()
{
char str[30] = "Codershelpline";
int length;
length=sizeof(str);
printf("Length of given string is = %d", length);
return 0;
}
NB : sizeof() gives the length of memory occupied by the variable.
Output :
Length of given string is = 30
--------- OR ----------
#include <stdio.h>
int main()
{
char str[30] = "Codershelpline";
printf("Length of given string is = %d", sizeof(str));
return 0;
}
Output :
Length of given string is = 30
Example : Find the length of given string in C using string function [sprintf()].
#include<stdio.h>
int main()
{
char x[20];
int n=sprintf(x,"codershelpline");
printf("The length of the String is = %d",n);
return 0;
}
Output :
14
Example : How to copy one string contents into another empty string in C using string function [strcpy()].
#include <stdio.h>
#include <string.h>
int main()
{
char str1[30] = "Codershelpline";
char str2[30] ;
strcpy(str2,str1);
//strcpy(str2,"Codershelpline");
printf("String after Copying is = %s", str2);
return 0;
}
Output :
String after Copying is = Codershelpline
--------- OR ----------
#include <stdio.h>
#include <string.h>
int main()
{
char str1[30] = "Codershelpline";
char str2[30] = "Hello India";
strcpy(str2,str1);
//strcpy(str2,"Codershelpline");
printf("String after Copying is = %s", str2);
return 0;
}
Output :
String after Copying is = Codershelpline
Example : How to copy one string contents up to desired length into another empty string in C using string function [strncpy()].
#include <stdio.h>
#include <string.h>
int main()
{
char str1[30] = "Codershelpline";
char str2[30] ;
strncpy(str2,str1,5);
printf("String after Copying is = %s", str2);
return 0;
}
Output :
String after Copying is = Coder #
--------- OR ----------
#include <stdio.h>
#include <string.h>
int main()
{
char str1[30] = "Codershelpline";
char str2[30] ;
strncpy(str2,str1,17);
printf("String after Copying is = %s", str2);
return 0;
}
Output :
String after Copying is = Codershelpline\0\0\0
NB : \0 does not appear in the output.
Example : How to duplicate the given string in C using string function [strdup()].
#include <stdio.h>
#include <string.h>
int main()
{
char *ptr1 = "Codershelpline";
char *ptr2;
ptr2 = strdup(ptr1);
printf("The Duplicate String is = %s", ptr2);
return 0;
}
Output :
The Duplicate String is = Codershelpline
Example : How to reverse the given string in C using string function [strrev()].
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[30];
printf("Enter String Value = ");
gets(str);
strrev(str);
printf("The Reversed String is = ");
puts(str);
getch();
}
Example : How to convert uppercase/capital letters into lowercase/small letters of the given string in C using string function [strlwr()].
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[30];
printf("Enter String Value = ");
gets(str);
strlwr(str);
printf("The Reversed String is = ");
puts(str);
getch();
}
------------------------------ OR ------------------------------
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char str[30];int i;
printf("Enter String Value upto 30 characters = ");
gets(str);
for(i=0; str[i]!='\0'; i++)
{
str[i] = tolower(str[i]);
}
printf("The Lowercase String is = ");
puts(str);
getch();
}
Example : How to convert lowercase/small letters into uppercase/capital letters of the given string in C using string function [strupr()].
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[30];
printf("Enter String Value = ");
gets(str);
strupr(str);
printf("The Reversed String is = ");
puts(str);
getch();
}
------------------------------ OR ------------------------------
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char str[30];int i;
printf("Enter String Value upto 30 characters = ");
gets(str);
for(i=0; str[i]!='\0'; i++)
{
str[i] = toupper(str[i]);
}
printf("The Uppercase String is = ");
puts(str);
getch();
}
NB : The strupr() function[string.h] converts all lowercase characters of a string to the uppercase character at a time whereas the toupper() function[ctype.h] works on character, not on a string. It works character wise i.e.only one character at a time .
Example : How to compare two given string in C using string function [strcmp()].
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "Codershelpline";
char str2[20] = "Codershelpline";
int value;
value = strcmp(str1, str2);
if (value==0)
{
printf("Both Strings are Equal");
}
else
{
printf("Both String are Different");
}
return 0;
}
Output :
Both Strings are Equal
--------- OR ----------
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "Codershelpline";
char str2[20] = "Codershelpline";
if (strcmp(str1, str2) ==0)
{
printf("Both Strings are Equal");
}
else
{
printf("Both String are Different");
}
return 0;
}
Output :
Both Strings are Equal
--------- OR ----------
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "Codershelpline";
char str2[20] = "codershelpline";
if (strcmp(str1, str2) ==0)
{
printf("Both Strings are Equal");
}
else
{
printf("Both String are Different");
}
return 0;
}
Output :
Both Strings are Different
--------- OR ----------
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "Welcome India";
char str2[20] = "Welcome";
int value;
value=strcmp(str1, str2);
if (value == 0)
{
printf("Both Strings are Equal");
}
else if(value>0)
{
printf("Both String are Different and First String has larger length");
}
else
{
printf("Both String are Different and Second String has larger length");
}
return 0;
}
Output :
Both String are Different and First String has larger length
--------- OR ----------
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "Welcome ";
char str2[20] = "Welcome India";
int value;
value=strcmp(str1, str2);
if (value == 0)
{
printf("Both Strings are Equal");
}
else if(value<0)
{
printf("Both String are Different and Second String has larger length");
}
else
{
printf("Both String are Different and First String has larger length");
}
return 0;
}
Output :
Both String are Different and Second String has larger length
Example : How to compare two given string ignoring case in C using string function [stricmp()].
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "Codershelpline";
char str2[20] = "codershelpline";
int value;
value=stricmp(str1, str2);
if (value == 0)
{
printf("Both Strings are Equal");
}
else
{
printf("Both String are Different ");
}
return 0;
}
Output :
Both Strings are Equal
Example : How to compare two string upto given length in C using string function [strncmp()].
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "Welcome ";
char str2[20] = "Welcome India";
int value;
value=strncmp(str1, str2, 7);
if (value == 0)
{
printf("Both Strings are Equal");
}
else
{
printf("Both String are Different");
}
return 0;
}
Output :
Both Strings are Equal
--------- OR ----------
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "Welcome ";
char str2[20] = "Welcome India";
int value;
value=strncmp(str1, str2, 9);
if (value == 0)
{
printf("Both Strings are Equal");
}
else
{
printf("Both String are Different");
}
return 0;
}
Output :
Both String are Different
Example : How to concatenate two given strings as one in C using string function [strcat()].
#include <stdio.h>
#include <string.h>
int main()
{
char str1[30] = "Coders";
char str2[30] = "helpline";
strcat(str1,str2);
printf("String after concatenation is = %s", str1);
return 0;
}
Output :
String after concatenation is = Codershelpline
Example : How to concatenate two given strings as one and store in another string in C using string function [strcat()].
#include <stdio.h>
#include <string.h>
int main()
{
char str1[30] = "Coders";
char str2[30] = "helpline";
char str3[30] ;
strcat(str1,str2);
strcpy(str3,str1);
printf("String after concatenation is = %s", str3);
return 0;
}
Output :
String after concatenation is = Codershelpline
Example : How to concatenate two given strings as one upto given length in C using string function [strncat()].
#include <stdio.h>
#include <string.h>
int main()
{
char str1[30] = "Coders";
char str2[30] = "helpline";
strncat(str1,str2,4);
printf("String after concatenation is = %s", str1);
return 0;
}
Output :
String after concatenation is = Codershelp
Example : How to display remaining string from a first occurrence of searching character in C using string function [strchr()].
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Codershelpline";
char ch = 'l';
char* n;
n = strchr(str, ch);
printf("String from last searching location of %c is : %s \n",ch, n);
return (0);
}
Output :
String from last searching location of l is : lpline
------ OR ------
#include <stdio.h>
#include <string.h>
int main ()
{
char str[55] ="This is Your Site, Please Use it Once.";
char *ptr;
char ch='Y';
ptr = strchr (str,ch);
printf ("Character %c is Found at Position %d\n",ch,ptr-(str+1));
printf ("First Occurrence of Character \"%c\" in \"%s\" is \"%s\" ",ch,str, ptr);
return 0;
}
Output :
Character Y is Found at Position 7.
First Occurrence of Character "Y" in "This is Your Site, Please Use it Once." is "Your Site, Please Use it Once."
Example : How to display remaining string from a last occurrence of character (in reverse order searching i.e. started from end) in C using string function [strrchr()].
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Codershelpline";
char ch = 'd';
char* n;
n = strrchr(str, ch);
printf("String from last searching location of %c is : %s \n",ch, n);
return (0);
}
Output :
String from last searching location of d is : dershelpline
--------- OR ----------
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Codershelpline";
char ch = 'l';
char* n;
n = strrchr(str, ch);
printf("String from last searching location of %c is : %s \n",ch, n);
return (0);
}
Output :
String from last searching location of l is : line
NB :
strrchr() function locates the last occurrence of a character in a string and returns as a pointer.
Example : How to search first occurrence of word in a string from a given string in C using string function [strstr()].
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[55] ="This is Your Choice Site";
char *ptr;
char str2[]="Choice";
ptr = strstr (str1,str2);
if(ptr)
{
printf("Searched String \"%s\" Found\n",str2 );
printf ("First Occurrence of Searched String \"%s\" in Sentence\"%s\" is \"%s\"",str2,str1,
ptr);
}
else
printf("Searched String Not Found\n" );
return 0;
}
Output :
Searched String "Choice" Found
First Occurrence of Searched String "Choice" in Sentence "This is Your Choice Site" is "Choice Site"
NB : Returns pointer to the first occurrence of the searched string in a given string.
Example : How to replace the string with given character using string function [strset()].
#include<stdio.h>
#include<string.h>
int main()
{
char str[20] = "Codershelpline";
printf("Original String is = %s\n", str);
printf("String after Replacing with given Character is %s",strset(str,'$'));
return 0;
}
Output :
Original String is = Codershelpline
String after Replacing with given Character is $$$$$$$$$$$$$$
Example : How to replace the string upto desired length with given character using string function [strnset()].
#include<stdio.h>
#include<string.h>
int main()
{
char str[20] = "Codershelpline";
printf("Original String is = %s\n", str);
printf("String after Replacing with given Character is %s",strnset(str,'$',6));
return 0;
}
Output :
Original String is = Codershelpline
String after Replacing with given Character is $$$$$$helpline
0 Comments