Monday, November 29, 2010

//string handling operations

#include
#include
#include
void read(char[]);
void copy(char[],char[]);
void compare(char[],char[]);
void substring(char[],int);
void main()
{
int choice,pos;
do
{
printf("***************************");
printf("1.copy two strings");
printf("2.compare two strings");
printf("3.finding substring");
printf("4.exit");
printf("***************************");
printf("enter ur option:");
scanf("%d",choice);
switch(choice)
{
case 1 :
read(str1);
copy(str1,str2);
break;
case 2:
read(str1);
read(str2);
compare(str1,str2);
break;
case 3:
read(str1);
printf("enter the position");
scanf("%d",&pos);
substring(strl,pos);
break;
case 4:exit();
default:printf("invalid option");
break;
}
}
while(choice<=4);
getch();
}
void read(char string[])
{
printf("enter a string");
scanf("%s",string);
}
void copy(char s1[],char s2[])
{
int i,j;
for(i=0;s1[i]!='\0';i++)
s2[i]=s1[i];
}
void compare(char s1[],char s2[])
{
int i,j;
for(i=0,j=0;s1[i]&&s2[j];i++,j++)
{
if(s1[i]!=s2[j])
break;
}
if(s1[j]=='\0' && s2[j]=='\0')
printf("both are equal");
}
void substring( char s1[],int p)
{
int i;
for(i=p;s1[i]!='\0';i++)
printf("%c",s1[i]);
}

No comments:

Post a Comment