String functions :
string functions used in c are
1.string length ----- strlen()
2.string copy ----- strcpy()
3.string reverse ----- strrev()
4.string compare ----- strcmp()
5.string concatenate ----- strcat()
6.string upper ----- strupr()
7.string lower ----- strlwr()
Program 1: string length ----- strlen()
// program to illustrate string length ----strlen
#include<stdio.h>
#include<string.h>
#define pf printf
#define sf scanf
void main()
{
char name[20];
int l;
pf("\nenter any string : ");
sf("%[^\n]s",name);
l=strlen(name);
pf("\nentered name is : %s and its length is : %d",name,l);
}
output:
Program 2: string copy ----- strcpy()
// program to illustrate string copy ----strcpy
#include<stdio.h>
#include<string.h>
#define pf printf
#define sf scanf
void main()
{
char name1[20],name2[44];
pf("\nenter any string in name1 : ");
sf("%[^\n]s",name1);
strcpy(name2,name1);
pf("\ncopied string in name2 is : %s",name2);
}
output:
Program 3: string reverse ----- strrev()
// program to illustrate string reverse using strlen()
#include <stdio.h>
#include <string.h>
int main()
{
int i=0, j, temp;
char s[22];
printf("\n\n Enter any string :");
scanf("%s", s);
j=strlen(s) - 1;
printf("\n\n Entered string is : %s and its length is %d", s,j+1);
while(i < j)
{
temp = s[i];
s[i++] = s[j];
s[j--] = temp;
}
printf("\n\n Reverse string is: %s", s);
}
output:
Program 4: string compare ----- strcmp()
// program to illustrate string compare ----strcmp
#include<stdio.h>
#include<string.h>
#define pf printf
#define sf scanf
void main()
{
char name1[20],name2[44];
int r;
pf("\nenter any string in name1 : ");
sf("%[^\n]s",name1);
pf("\nenter any string in name2 : ");
sf("%s",name2);
r=strcmp(name1,name2);
if (r == 0)
pf("\nBoth the strings are equal");
else
pf("\nBoth the strings are not equal");
}
output:
Program 5: string concatenate ----- strcat()
// program to illustrate string concatenate ----strcat
#include<stdio.h>
#include<string.h>
#define pf printf
#define sf scanf
void main()
{
char name1[20],name2[44];
int r;
pf("\nenter any string in name1 : ");
sf("%[^\n]s",name1);
pf("\nenter any string in name2 : ");
sf("%s",name2);
strcat(name1,name2);
pf("\nconcatenated string is : %s",name1);
}
output:
Program 6: string upper ----- strupr()
// program to illustrate string upper ----strupr using functions
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void strupper ( char* );
void main()
{
char string[] = {"r k raju"};
printf( "%s\n", string );
strupper( string );
printf( "%s\n", string );
}
void strupper ( char *p )
{
while( *p )
{
*p=toupper( *p );
p++;
}
}
output:
Program 7: string lower ----- strlwr()
// program to illustrate string upper ----strupr using functions
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void strupper ( char* );
void main()
{
char string[] = {"r k raju"};
printf( "%s\n", string );
strupper( string );
printf( "%s\n", string );
}
void strupper ( char *p )
{
while( *p )
{
*p=tolower( *p );
p++;
}
}
output:
No comments:
Post a Comment