/***String operations with pointers****/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(void)
{
//declaration and initialization of variables
char str1[20]={'\0'},str2[20]={'\0'};
int choice=0,flag=0;
int len=0 , len1=0,len2=0;
//declaration of functions
void accept(char[]);
void display(char []);
int length(char *);
int copy(char *,char *);
int compare(char *,char *);
void reverse(char *);
void revert(char *,char *);
int substring(char *,char *);
void split(char *);
void concatenate(char *,char *);
int palindrome(char *);
clrscr();
do
{
printf("\n******Menu*******\n");
printf("\nThe operations that can be performed are:\n");
printf("1.Accept\n");
printf("2.Display\n");
printf("3.Length\n");
printf("4.Copy\n");
printf("5.Compare\n");
printf("6.Reverse\n");
printf("7.Revert\n");
printf("8.Substring\n");
printf("9.Split\n");
printf("10.Concatenate\n");
printf("11.Palindrome\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
// to accept string from user
accept(str1);
break;
case 2:
//to display string
display(str1);
break;
case 3:
//To find length
len = length(str1);
printf("THe length is: %d\n\n",len);
break;
case 4:
//to copy string
len = copy(str1,str2);
if(len==0)
{
printf("\nThe source string is: %s\n\n",str1);
printf("\nThe copied string is: %s\n\n",str2);
}
else
{
printf("\nThe function could not be performed\n\n");
}
break;
case 5:
//to compare 2 strings
printf("Enter the 2nd string\n");
scanf("%s",str2);
len1 = length(str1);
len2 = length(str2);
if(len1==len2)
{
len = compare(str1,str2);
if(len==0)
{
printf("Strings are equal\n\n");
}
else
printf("Strings are not equal\n\n");
}
else
{
printf("Strings are not equal\n\n");
}
break;
case 6:
//to reverse a string
reverse(str1);
break;
case 7:
//to revert a string
revert(str1,str2);
printf("The revert string is: %s\n\n",str2);
break;
case 8:
//to find substring
printf("Enter the string you want:\n\n");
scanf("%s",str2);
len=substring(str1,str2);
if(len==1)
{
printf("Substring is not present\n");
}
else
{
printf("Substring present\n");
}
break;
case 9:
//to split a string
split(str1);
break;
case 10:
//to concatenate two strings
printf("Enter the string to concatenate:\n\n");
scanf("%s",str2);
concatenate(str1,str2);
printf("The concatenated string is: %s\n\n",str1);
break;
case 11:
//to find whether palindrome
flag=palindrome(str1);
if(flag==0)
{
printf("String is palindrome\n\n");
}
else
printf("String is not palindrome\n");
break;
default:
printf("Entered wrong choice\n");
}
}while(choice>0&&choice<=11);
getch();
return 0;
}
//function to accept a string from the user
void accept(char str1[])
{
printf("Enter the main string to perform operations: \n");
scanf("%s",str1);
}
//function to display a string
void display(char str1[])
{
printf("The string entered is:\n");
printf("%s\n",str1);
}
//function to find the length of a string
int length(char *p)
{
int len = 0;
while(*p!='\0')
{
len++;
p++;
}
return len;
}
//Function to copy one string to another
int copy(char *p,char *q)
{
while(*p!='\0')
{
*q=*p;
p++;
q++;
}
*q++='\0';
return 0;
}
//function to compare 2 strings
int compare(char *p,char *q)
{
int flag=0;
while(*p!='\0')
{
if(*p==*q)
{
p++;q++;
continue;
}
else
{
flag=1;
break;
}
}
if(flag==1)
return 1;
else
return 0;
}
//function to reverse a string
void reverse(char *p)
{
while(*p!='\0')
{
p++;
}
p--;
while(*p!='\0')
{
printf("%c\n",*p);
p--;
}
}
//function to revert a string
void revert(char *p,char *q)
{
while(*p!='\0')
{
p++;
}
p--;
while(*p!='\0')
{
*q=*p;
q++;
p--;
}
*q++='\0';
}
//function to find whether a substring
int substring(char *p,char *q)
{
int flag=0;
while(*p!='\0')
{
if(*p==*q)
{
while(*q!='\0')
{
if(*p==*q)
{
p++;
q++;
}
else
{
flag=1;
break;
}
}
}
else
{
p++;
}
if(flag==1)
{
break;
}
}
return(flag);
}
//function to split a string
void split(char *str)
{
int i=0,j=0;
char str1[20]={'\0'},str2[20]={'\0'};
char *s = str1,*d=str2;
copy(str,d);
printf("\nEnter the position:\n");
scanf("%d",&i);
while(j<i)
{
d++;
str++;
j++;
}
*d++='\0';
while(*str!='\0')
{
*s=*str;
s++;
str++;
}
*s++='\0';
printf("THe strings are: %s and %s\n",str2,str1);
str[i]='\0';
}
//function to concatenate 2 string
void concatenate(char *str,char *str1)
{
while(*str!='\0')
{
str++;
}
while(*str1!='\0')
{
*str=*str1;
str++;
str1++;
}
*str++='\0';
}
//function to find whether palindrome
int palindrome(char *str)
{
int len=0,j=0,i,count=0;
len=strlen(str);
j=len-1;
for(i=0;i<len/2;i++)
{
if(*(&str[i])==*(&str[j]))
{
count=0;
}
else
{
count=1;
break;
}
j--;
}
return count;
}
No comments:
Post a Comment