/***String operations without pointers****/
#include<stdio.h>
#include<conio.h>
#include<string.h>
//main()
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();
void display(char []);
int length(char []);
int copy(char[],char[]);
int compare(char[],char[]);
void reverse(char []);
void revert(char [],char []);
void 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 a string
accept(str1);
break;
case 2:
//To display a string
display(str1);
break;
case 3:
//find length
len = length(str1);
printf("THe length is: %d",len);
break;
case 4:
//Copy one string to another
len = copy(str1,str2);
if(len==0)
{
printf("The source string is: %s",str1);
printf("\nThe copied string is: %s",str2);
}
else
{
printf("The function could not be performed\n");
}
break;
case 5:
//to compare 2 strings
printf("\nEnter the string to compare with:");
scanf("%s",str2);
len1 = length(str1);
len2 = length(str2);
if(len1==len2)
{
len = compare(str1,str2);
if(len==0)
{
printf("Strings are equal");
}
else
printf("Strings are not equal");
}
else
{
printf("Strings are not equal");
}
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",str2);
break;
case 8:
//To find whether string is substring
printf("Enter the substring : ");
scanf("%s",str2);
substring(str1,str2);
break;
case 9:
//To split a string
split(str1);
break;
case 10:
//To concatenate two string
printf("Enter the string to concatenate:");
scanf("%s",str2);
concatenate(str1,str2);
break;
case 11:
//To find whether a string is palindrome
flag=palindrome(str1);
if(flag==0)
{
printf("String is palindrome");
}
else
printf("String is not palindrome");
break;
default:
printf("Entered wrong choice");
}
}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: ");
scanf("%s",str1);
}
//function to display a string
void display(char str1[])
{
printf("The string entered is: ");
printf("%s",str1);
}
//function to find the length of a string
int length(char str[])
{
int len = 0,i = 0;
while(str[i]!='\0')
{
len++;
i++;
}
return len;
}
//Function to copy one string to another
int copy(char str1[],char str2[])
{
int i=0;
while(str1[i]!='\0')
{
str2[i]=str1[i];
i++;
}
str2[i]='\0';
return 0;
}
//function to compare 2 strings
int compare(char str1[],char str2[])
{
int i=0,j=0,flag=0;
i = length(str1);
while(j<=i)
{
if(str1[j]==str2[j])
{
j++;
continue;
}
else
{
flag=1;
j=i+1;
}
}
if(flag==1)
return 1;
else
return 0;
}
//function to reverse a string
void reverse(char str[])
{
int len=0;
len=length(str);
while(len>=0)
{
printf("%c",str[len]);
len--;
}
}
//function to revert a string
void revert(char str1[],char str2[])
{
int len=0,i=0;
len=length(str1);
len=len-1;
while(len>=0)
{
str2[i]=str1[len];
len--;
i++;
}
str2[i]='\0';
}
//function to find whether a substring
void substring(char a[],char b[])
{
int i=0,j=0,flag=0,cnt=0;
while(a[i]!='\0')
{
j=0;
if(a[i]==b[j])
{
flag=1;
while(b[j]!='\0')
{
if(a[i]!=b[j])
{
flag=0;
break;
}
i++;
j++;
}
if(flag==1)
{
cnt++;
}
}
else
i++;
}
if(flag==1)
{
printf("Substring present");
}
else
{
printf("Substring not present");
}
}
//function to split a string
void split(char str[])
{
int i=0,j=0,m=0;
int len=length(str);
char str1[20]={0},str2[20]={0};
copy(str,str2);
printf("\nEnter the position:");
scanf("%d",&i);
for(j=i;j<len;j++)
{
str1[m]=str2[j];
m++;
}
str2[i]='\0';
printf("THe strings are: %s and %s",str2,str1);
}
//function to concatenate 2 string
void concatenate(char str[],char str1[])
{
int len1=length(str);
int len2=length(str1);
int i=0;
while(i<len2)
{
str[len1]=str1[i];
len1++;
i++;
}
str[len1+1]='\0';
printf("the concat string is: %s",str);
}
//function to find whether palindrome
int palindrome(char str[])
{
char str1[20]={'\0'};
int flag=0;
revert(str,str1);
flag=compare(str,str1);
return flag;
}
No comments:
Post a Comment