Sunday, November 6, 2011

String Programming in C language

This program covers almost every String operations available in C Language


#include<conio.h>
#include<stdio.h>

 void main()
 {
    char arr[10],ar[10],ar1[]="HiQwwerDr";
    int r;
    int cmp(char *,char *);
    void mid(char [],char [],int,int);
    void toup(char []);
    void tolow(char []);
    void toggle(char []);
    void reverse(char [],char []);
    clrscr();
    printf("Enter the word ");
    scanf("%s %s",arr,ar);
    /*r=cmp(arr,ar);
    if(r==0)
    printf("This two words are same");
    else
    printf("This two word are not same");
    printf("\nThe size of first word is %d\n",scount(arr));
    mid(arr,ar,2,5);
    printf("%s",ar);
    toup(arr);
    printf("\n%s",arr);
    tolow(arr);
    printf("\n%s",arr);
    toggle(ar1);
    printf("\n%s",ar1); */
    reverse(arr,ar);
    printf("\n%s",ar);
    getch();
 }
 int scount(char *arr)
 {
int i=0;
while(*arr!=NULL)
{
i++;
arr++;
}
return i;
 }
 int cmp(char *arr,char *ar)
 {
while(*arr!=NULL)
{
if(*arr==*ar)
{
arr++;
ar++;
}
else
return 1;
}
return 0;
 }
 void mid(char arr[],char ar[],int s,int e)
 {
int i,
j=0;
i=s-1;
while(arr[i]!=NULL && i<=(e-1))
{
ar[j]=arr[i];
i++;
j++;
}
ar[j]=NULL;
 }
 void toup(char arr[])
 {
int i=0;
char ch;
while(arr[i]!=NULL)
{
ch=arr[i];
if(ch>=97 && ch<=122)
ch-=32;
arr[i]=ch;
i++;
}
 }
 void tolow(char arr[])
 {
int i=0;
char ch;
while(arr[i]!=NULL)
{
ch=arr[i];
if(ch>=65 && ch<=90)
ch+=32;
arr[i]=ch;
i++;
}
 }
 void toggle(char arr[])
 {
int i=0;
char ch;
while(arr[i]!=NULL)
{
ch=arr[i];
if(ch>=65 && ch<=90)
ch+=32;
else if(ch>=97 && ch<=122)
ch-=32;
arr[i]=ch;
i++;
}
 }
 void reverse(char arr[],char ar[])
 {
int i,j=0,k;
i=scount(arr);
k=i-1;
while(j<=(i-1))
{
ar[j]=arr[k];
j++;
k--;
}
ar[j]=NULL;
 }

0 comments:

Post a Comment

You are most welcome for making comments here. I will consider your view and will reply you soon.