Strings:-


functions on strings-:
-:strlen()
-:strcpy()
-:strcmp()
-:strcat()

use of these functions along with the examples-:

how to program a string-:


#include< stdio.h>
#include< conio.h>
void main()
{
char a[15];
clrscr();
printf("enter 15 characters");
scanf("%s",a);
getch();
}



finding length of string using strlen function-:


#include< stdio.h>
#include< conio.h>
#include< string.h>
void main()
{
char a[15];
int x;
clrscr();
printf("enter a string");
scanf("%s",&a);
x=strlen(a);
printf("the length of your string is %d",x);
}



copying string into another using strcpy function:


#include< stdio.h>
#include< conio.h>
#include< string.h>
void main()
{
char a[15],b[15];
clrscr();
printf("enter a string");
scanf("%s",&a);
strcpy(b,a);
printf("the copied string is %s",b);
getch();
}



finding differnce of first unmatched character usi ng strcmp function-:


#include< stdio.h>
#include< conio.h>
#include< string.h>
void main()
{
char a[15],b[15];
int x;
clrscr();
printf("enter a string");
scanf("%s",&a);
printf("enter another string");
scanf("%s",b);
x=strcmp(a,b);
printf("the difference of string is %d",x);
getch();
}



concatenating two strings using strcat function-:


#include< stdio.h>
#include< conio.h>
#include< string.h>
void main()
{
char a[15],b[15];
clrscr();
printf("enter a string");
scanf("%s",&a);
printf("enter another string");
scanf("%s",b);
strcat(a,b);
printf("the formed string is %s",a);
getch();
}



finding the length of string without using strlen f unction-:


#include< stdio.h>
#include< conio.h>
#include< string.h>
void main()
{
char a[15];
int c=0;
printf("enter a string");
scanf("%d",a);
for(i=0;a[i]!='\0';i++)
{
c=c+1;
}
printf("the length of string is %d",c);
getch();
}



copy the string into another string without using s trcpy-:


#include< stdio.h>
#include< conio.h>
#include< string.h>
void main()
{
char a[15],b[15];
printf("enter a string");
scanf("%d",a);
for(i=0;a[i]!='\0';i++)
{
b[i]=a[i];
}
b[i]='\0';
printf("the copied string is %c",b);
getch();
}



compare two strings without using strcmp-:

< br> #include< stdio.h>
#include< conio.h>
#include< string.h>
void main()
{
char a[15],b[15];
int diff;
printf("enter a string");
scanf("%d",a);
printf("enter a string");
scanf("%d",b);
for(i=0;a[i]!='\0';i++)
{
if(a[i]!=b[i])
diff=a[i]-b[i];
}
printf("the difference of string is %d",diff);
getch();
}



concatenate two strings without using strcat function-:


#include< stdio.h>
#include< conio.h>
#include< string.h>
void main()
{
char a[15],b[15];
int x,y,k=0;
printf("enter a string");
scanf("%d",a);
printf("enter a string");
scanf("%d",b);
x=strlen(a);
y=strlen(b);
for(i=x;i<=x+y;i++)
{
a[i]=b[k];
k++;
}
a[i]='\0';
printf("the concatenated string is %c",a[i]);
getch();
}
program to insert a string between a string-:


#include< stdio.h>
#include< conio.h>
#include< string.h>
void main()
{
char a[15],b[15];
int i,j,x,y,k=1,pos;
clrscr();
printf("enter a string");
scanf("%d",&a);
printf("enter another string");
scanf("%d",&b);
printf("enter the position at which you want to ins er the string");
scanf("%d",&pos);
x=strlen(a);
y=strlen(b);
for(j=1;j<=y;j++)
{
for(i=x+1;i>=pos-1;i--)
{
a[i+1]=a[i];v }
}
for(i=pos-1;i<=y;i++)
{
a[i]=b[k];
k++;
}
printf("the new string formed is %s",a);
getch();
}



total number of vowels present in entered string-:< br>

#include< stdio.h>
#include< conio.h>
#include< string.h>
void main()
{
char a[15];
int x,i,c=0;
clrscr();
printf("enter a string");
scanf("%d",&a);
x=strlen(a);
for(i=0;i<=x;i++)
{
if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i] =='u')
{
c=c+1;
}
}
printf("the number of vowels are%d",c);
getch();
}



print the name in a histogram manner-:


#include< stdio.h>
#include< conio.h>
#include< string.h>
void main()
{
char a[15];
int x,i,j;
clrscr();
printf("enter a string");
scanf("%d",&a);
for(i=0;i!='\0';i++)
{
for(j=0;j<=i;j++)
{
printf("%c",a[j]);
}
printf("\n");
}
getch();
}



print the ASCII equivalent of a string-:


#include< stdio.h>
#include< conio.h>
#include< string.h>
void main()
{
int i;
char a[15];
printf("enter the string");
scanf("%s",a);
for(i=0;i!='\0';i++)
{
printf("the ASCII equivalent of %c is %d",a[i],a[i] );
}
getch();
}



program to search for the entered string-:

< br> #include< stdio.h>
#include< conio.h>
#include< string.h>
void main()
{
int i,j=0,x,c=0,y;
char a[15],b[15];
clrscr();
printf("enter the first string");
scanf("%s",a);
printf("enter the string to be searched");
scanf("%s",a);
x=strlen(a);
y=strlen(b);
for(i=0;i {
if(a[i]==b[j])
{
j=j+1;
if(c==y)
{
c=c+1;
j=0;
continue;
}
}
}
printf("%d",c);
getch();
}



program to extract a string from an entered string -:


#include< stdio.h>
#include< conio.h>
void main()
{
char a[15],b[15];
int i,j=0,pos1,pos2;
clrscr();
printf("enter the string");
scanf("%s",a);
printf("enter the position from where you want to e xtract the string");
scanf("%d",pos1);
printf("enter the position till where you want to e xtract the string");
scanf("%d",pos2);
for(i=pos1-1;i<=pos2-1;i++)
{
b[j]=a[i];
j++;
}
str2[j]='\0';
printf("%s",b);
getch();
}



program to sort the string-:


#include< stdio.h>
#include< conio.h>
void main()
{
char a[15];
int i,j,x;
clrscr();
printf("enter the string");
scanf("%s",a);
x=strlen(a);
for(i=0;a[i]!='\0';i++)
{
for(j=i+1;a[j]='\0';j++)
{
if(a[j] {
t=str[j];
str[j]=str[i];
str[i]=t;
}
}
}
printf("%s",a);
getch();
}