/*
perform string operations w/o using library functions
*/
#include<stdio.h>
#include<conio.h>
int strn_len(char *);
void strn_cpy(char *,char *);
char *strn_rev(char *);
int strn_cmp(char *,char *);
int palin(char *);
char* strn_cat(char *,char *);
int strn_sub(char *,char *);
void main()
{
int n;
char a[50],b[50],tmp[50],sub[10],y;
do
{
clrscr();
printf("Enter a String A: ");
fflush(stdin);
gets(a);
printf("Enter a String B: ");
gets(b);
printf("Select An Operation");
printf("\n1. Copy B to A\n2. Length\n3. Reverse String\n4. Palindrome");
printf("\n5. Concatenation\n6.Find Occurence of Substring\nChoice: ");
scanf("%d",&n);
switch(n)
{
case 1:
strn_cpy(a,b);
printf("B has been copied to A: %s",a);
break;
case 2:
printf("Which string to find length (a/b): ");
y=getche();
if(y=='a'||y=='A')
printf("\nLength of string is: %d",strn_len(a));
else if(y=='B'||y=='b')
printf("\nLength of string is: %d",strn_len(b));
else
printf("\nInvalid String Name");
break;
case 3:
printf("Which string to reserse (a/b): ");
y=getche();
if(y=='a'||y=='A')
printf("\nReversed string is: %s",strn_rev(a));
else if(y=='B'||y=='b')
printf("\nReserved string is: %s",strn_rev(b));
else
printf("\nInvalid String Name");
break;
case 4:
printf("Which string to check for palindrome (a/b): ");
y=getche();
if(y=='A'||y=='a')
{
if(palin(a)==1)
printf("\nString %c is a palindrome",y);
else
printf("\nString %c is NOT a palindrome",y);
}
else
{
if(y=='B'||y=='b')
{
if(palin(b)==1)
printf("\nString %c is a palindrome",y);
else
printf("\nString %c is NOT a palindrome",y);
}
}
break;
case 5:
printf("\nString A & B have been joined: %s",strn_cat(a,b));
break;
case 6:
printf("\nEnter substring To Search: ");
fflush(stdin);
gets(sub);
if(strn_sub(a,sub)==0)
{ if(strn_sub(b,sub)==0)
printf("\nSubstring is neither in A nor in B");
}
else
{
if(strn_sub(a,sub)==1)
printf("\nSubstring is Present in String A");
else
{
if(strn_sub(b,sub)==1)
printf("\nSubstring is Present in String B");
}
}
break;
}
printf("\n\nDo u Want to continue (y/n): ");
fflush(stdin);
}while(getche()=='y');
}
int strn_len(char *a)
{
int i;
for(i=0;a[i]!='\0';i++);
return i;
}
void strn_cpy(char *a,char *b)
{
int i;
for(i=0;i<strn_len(b);i++)
a[i]=b[i];
a[i]='\0';
}
char *strn_rev(char *a)
{
char tmp[50];
int i,b;
b=(strn_len(a)-1);
for(i=0;i<strn_len(a);i++,b--)
tmp[b]=a[i];
for(i=0;i<strn_len(a);i++)
a[i]=tmp[i];
return a;
}
int strn_cmp(char *a,char *b)
{
int i,flag;
flag=0;
if(strn_len(a)!=strn_len(b))
flag=1;
else if(strn_len(a)==strn_len(b))
{
for(i=0;i<strn_len(a);i++)
if(a[i]!=b[i])
flag=1;
}
else
flag= 0;
return flag;
}
int palin(char *a)
{ char tmp[50];
strn_cpy(tmp,a);
strn_rev(tmp);
if(strn_cmp(tmp,a)==0)
return 1;
else
return 0;
}
char* strn_cat(char *a,char *b)
{
int i,A;
A=strn_len(a);
for(i=0;i<=strlen(b);i++,A++)
a[A]=b[i];
return a;
}
int strn_sub(char *a,char *sub)
{
int i,j;
for(i=0;i<strn_len(sub);i++)
{
for(j=0;j<strn_len(a);j++)
{
if(sub[i]==a[j])
break;
}
if(j==strn_len(a))
break;
}
if(i==strn_len(sub))
return 1;
else
return 0;
}
/*
Case 1:
Enter a String A: hello
Enter a String B: howzthat
Select An Operation
1. Copy B to A
2. Length
3. Reverse String
4. Palindrome
5. Concatenation
6.Find Occurence of Substring
Choice: 1
B has been copied to A: howzthat
Do u Want to continue (y/n):y
Case 2
Enter a String A: Hello
Enter a String B: howzthat
Select An Operation
1. Copy B to A
2. Length
3. Reverse String
4. Palindrome
5. Concatenation
6.Find Occurence of Substring
Choice: 2
Which string to find length (a/b): a
Length of string is: 5
Do u Want to continue (y/n):y
Case 3:
Enter a String A: hello
Enter a String B: howzthat
Select An Operation
1. Copy B to A
2. Length
3. Reverse String
4. Palindrome
5. Concatenation
6.Find Occurence of Substring
Choice: 3
Which string to reserse (a/b): b
Reserved string is: tahtzwoh
Do u Want to continue (y/n):y
Case 4:
Enter a String A: madam
Enter a String B: hi
Select An Operation
1. Copy B to A
2. Length
3. Reverse String
4. Palindrome
5. Concatenation
6.Find Occurence of Substring
Choice: 4
Which string to check for palindrome (a/b): a
String a is a palindrome
Do u Want to continue (y/n):y
Case 5:
Enter a String A: hello
Enter a String B: howzthat
Select An Operation
1. Copy B to A
2. Length
3. Reverse String
4. Palindrome
5. Concatenation
6.Find Occurence of Substring
Choice: 6
Enter substring To Search: ell
Substring is Present in String A
Do u Want to continue (y/n):n
*/
perform string operations w/o using library functions
*/
#include<stdio.h>
#include<conio.h>
int strn_len(char *);
void strn_cpy(char *,char *);
char *strn_rev(char *);
int strn_cmp(char *,char *);
int palin(char *);
char* strn_cat(char *,char *);
int strn_sub(char *,char *);
void main()
{
int n;
char a[50],b[50],tmp[50],sub[10],y;
do
{
clrscr();
printf("Enter a String A: ");
fflush(stdin);
gets(a);
printf("Enter a String B: ");
gets(b);
printf("Select An Operation");
printf("\n1. Copy B to A\n2. Length\n3. Reverse String\n4. Palindrome");
printf("\n5. Concatenation\n6.Find Occurence of Substring\nChoice: ");
scanf("%d",&n);
switch(n)
{
case 1:
strn_cpy(a,b);
printf("B has been copied to A: %s",a);
break;
case 2:
printf("Which string to find length (a/b): ");
y=getche();
if(y=='a'||y=='A')
printf("\nLength of string is: %d",strn_len(a));
else if(y=='B'||y=='b')
printf("\nLength of string is: %d",strn_len(b));
else
printf("\nInvalid String Name");
break;
case 3:
printf("Which string to reserse (a/b): ");
y=getche();
if(y=='a'||y=='A')
printf("\nReversed string is: %s",strn_rev(a));
else if(y=='B'||y=='b')
printf("\nReserved string is: %s",strn_rev(b));
else
printf("\nInvalid String Name");
break;
case 4:
printf("Which string to check for palindrome (a/b): ");
y=getche();
if(y=='A'||y=='a')
{
if(palin(a)==1)
printf("\nString %c is a palindrome",y);
else
printf("\nString %c is NOT a palindrome",y);
}
else
{
if(y=='B'||y=='b')
{
if(palin(b)==1)
printf("\nString %c is a palindrome",y);
else
printf("\nString %c is NOT a palindrome",y);
}
}
break;
case 5:
printf("\nString A & B have been joined: %s",strn_cat(a,b));
break;
case 6:
printf("\nEnter substring To Search: ");
fflush(stdin);
gets(sub);
if(strn_sub(a,sub)==0)
{ if(strn_sub(b,sub)==0)
printf("\nSubstring is neither in A nor in B");
}
else
{
if(strn_sub(a,sub)==1)
printf("\nSubstring is Present in String A");
else
{
if(strn_sub(b,sub)==1)
printf("\nSubstring is Present in String B");
}
}
break;
}
printf("\n\nDo u Want to continue (y/n): ");
fflush(stdin);
}while(getche()=='y');
}
int strn_len(char *a)
{
int i;
for(i=0;a[i]!='\0';i++);
return i;
}
void strn_cpy(char *a,char *b)
{
int i;
for(i=0;i<strn_len(b);i++)
a[i]=b[i];
a[i]='\0';
}
char *strn_rev(char *a)
{
char tmp[50];
int i,b;
b=(strn_len(a)-1);
for(i=0;i<strn_len(a);i++,b--)
tmp[b]=a[i];
for(i=0;i<strn_len(a);i++)
a[i]=tmp[i];
return a;
}
int strn_cmp(char *a,char *b)
{
int i,flag;
flag=0;
if(strn_len(a)!=strn_len(b))
flag=1;
else if(strn_len(a)==strn_len(b))
{
for(i=0;i<strn_len(a);i++)
if(a[i]!=b[i])
flag=1;
}
else
flag= 0;
return flag;
}
int palin(char *a)
{ char tmp[50];
strn_cpy(tmp,a);
strn_rev(tmp);
if(strn_cmp(tmp,a)==0)
return 1;
else
return 0;
}
char* strn_cat(char *a,char *b)
{
int i,A;
A=strn_len(a);
for(i=0;i<=strlen(b);i++,A++)
a[A]=b[i];
return a;
}
int strn_sub(char *a,char *sub)
{
int i,j;
for(i=0;i<strn_len(sub);i++)
{
for(j=0;j<strn_len(a);j++)
{
if(sub[i]==a[j])
break;
}
if(j==strn_len(a))
break;
}
if(i==strn_len(sub))
return 1;
else
return 0;
}
/*
Case 1:
Enter a String A: hello
Enter a String B: howzthat
Select An Operation
1. Copy B to A
2. Length
3. Reverse String
4. Palindrome
5. Concatenation
6.Find Occurence of Substring
Choice: 1
B has been copied to A: howzthat
Do u Want to continue (y/n):y
Case 2
Enter a String A: Hello
Enter a String B: howzthat
Select An Operation
1. Copy B to A
2. Length
3. Reverse String
4. Palindrome
5. Concatenation
6.Find Occurence of Substring
Choice: 2
Which string to find length (a/b): a
Length of string is: 5
Do u Want to continue (y/n):y
Case 3:
Enter a String A: hello
Enter a String B: howzthat
Select An Operation
1. Copy B to A
2. Length
3. Reverse String
4. Palindrome
5. Concatenation
6.Find Occurence of Substring
Choice: 3
Which string to reserse (a/b): b
Reserved string is: tahtzwoh
Do u Want to continue (y/n):y
Case 4:
Enter a String A: madam
Enter a String B: hi
Select An Operation
1. Copy B to A
2. Length
3. Reverse String
4. Palindrome
5. Concatenation
6.Find Occurence of Substring
Choice: 4
Which string to check for palindrome (a/b): a
String a is a palindrome
Do u Want to continue (y/n):y
Case 5:
Enter a String A: hello
Enter a String B: howzthat
Select An Operation
1. Copy B to A
2. Length
3. Reverse String
4. Palindrome
5. Concatenation
6.Find Occurence of Substring
Choice: 6
Enter substring To Search: ell
Substring is Present in String A
Do u Want to continue (y/n):n
*/
0 comments:
Post a Comment