String operations with pointers
with pointers).
functions implemented:
(2) Palindrome
(3) Copy
(4) String comparison
(5) Searching a string(substring)
Source Code
1 |
#include <br/> #include <br/> #include <br/> #include <br/> /*function prototypes*/ <br/> void reverse(char *a); <br/> int palindrome(char *a); <br/> void copy(char *b,char *a); <br/> int compare(char *a,char *b); <br/> void search(char *a,char *b); <br/> void main() <br/> { char a[100],b[100]; <br/> int result,op; <br/> clrscr(); <br/> do <br/> { /* display the menu */ <br/> printf("\n1)Reverse the Given String"); <br/> printf("\n2)Check for palindrome"); <br/> printf("\n3)Copy"); <br/> printf("\n4)String Comparison"); <br/> printf("\n5)String Searching(substring)"); <br/> printf("\n6)Quit"); <br/> printf("\n\nEnter Your Choice:"); <br/> scanf("%d",&op); <br/> flushall(); <br/> switch(op) <br/> { <br/> case 1: printf("\n Enter a String:"); <br/> gets(a); <br/> reverse(a); <br/> printf("\n Result=%s",a); <br/> printf("\n\n press a Character !!!!!!"); <br/> getch(); <br/> break; <br/> case 2: printf("\n Enter a String:"); <br/> gets(a); <br/> result=palindrome(a); <br/> if(result==0) <br/> printf("\nNot a palindrome"); <br/> else <br/> printf("\nA palindrome"); <br/> printf("\n\n press a Character !!!!!!"); <br/> getch(); <br/> break; <br/> case 3: printf("\n Enter a String:"); <br/> gets(a); <br/> copy(b,a); <br/> printf("\nResult=%s",b); <br/> printf("\n\n press a Character !!!!!!"); <br/> getch(); <br/> break; <br/> case 4: printf("\n Enter 1st string:"); <br/> gets(a); <br/> printf("\n Enter 2nd string:"); <br/> gets(b); <br/> result=compare(a,b); <br/> if(result==0) <br/> printf("\nboth are same"); <br/> else <br/> if(result>0) <br/> printf("\n1st>2nd"); <br/> else <br/> printf("\n1st<2nd"); <br/> printf("\n\n press a Character !!!!!!"); <br/> getch(); <br/> break; <br/> case 5: printf("\n Enter 1st string:"); <br/> gets(a); <br/> printf("\n Enter 2nd string:"); <br/> gets(b); <br/> search(a,b); <br/> printf("\n\n press a Character !!!!!!"); <br/> getch();break; <br/> } <br/> }while(op!=6); <br/> } <br/> void reverse(char *a) <br/> { char *p,*q; <br/> char temp; <br/> /* Algorithm used is an inplace Algorithm <br/> 1. q is postioned on the last character <br/> 2. p is postioned on the first character <br/> 3. *p is interchanged with *q <br/> 4. p is increased by 1 and q is decremented by 1 <br/> 5. if p""" <br/> */ <br/> p=q=a; <br/> while(*q!='\0') <br/> q++; <br/> /* q is on the null character*/ <br/> q--; <br/> while(p /* Algorith used <br/> 1. q is positioned on the last character <br/> 2. p is positioned on the first character <br/> 3. if *p != *q then it is not a palindrome,return(0) <br/> 4. p is increased by 1 and q is decremented by 1 <br/> 5. if p""" <br/> 6. string is a palindrome , return(1) <br/> */ <br/> p=q=a; <br/> while(*q!='\0') <br/> q++; <br/> /* q is on the null character*/ <br/> q--;/*q is on the last character*/ <br/> while(p /* Algorithm <br/> 1.both the strings are compared,character by character <br/> from the beginning <br/> 2.on first point of mismatch: <br/> a.if(*a>*b) then a>b <br/> b.if(*a <br/> 3. if both the strings end together then they are eqaul <br/> */ <br/> while(*a!='\0') <br/> { <br/> if(*a > *b) <br/> return(1); <br/> if(*a < *b) <br/> return(-1); <br/> a++;b++; <br/> } <br/> return(0); <br/> } <br/> void search(char *a ,char *b) <br/> { int lena,lenb; <br/> char *i,*j,*k; <br/> /*Algorithm <br/> 1. lenb=length of string b[],lena=length of a[] <br/> string a[] is scanned using the pointer i from location <br/> 0 to length of a[]-lenb+1 <br/> 2. string b[] is matched in string a[] from the position i <br/> */ <br/> for(lena=0;*(a+lena)!='\0';lena++) <br/> ; <br/> for(lenb=0;*(b+lenb)!='\0';lenb++) <br/> ; <br/> /* searching */ <br/> for(i=a;i<=a+lena-lenb+1;i++) <br/> { k=i; <br/> for(j=b;*k==*j&& *j!='\0';j++,k++) <br/> ; <br/> if(*j=='\0') <br/> printf("\nstring found at location:%d",i-a+1); <br/> } <br/> } <br/> "" <br/> |
String operations with pointers Output
1)Reverse the Given String
2)Check for palindrome
3)Copy
4)String Comparison
5)String Searching(substring)
6)Quit
Enter Your Choice:1
Enter a String:India
Result=aidnI
press a Character !!!!!!
1)Reverse the Given String
2)Check for palindrome
3)Copy
4)String Comparison
5)String Searching(substring)
6)Quit
Enter Your Choice:2
Enter a String:India
Not a palindrome
press a Character !!!!!!
1)Reverse the Given String
2)Check for palindrome
3)Copy
4)String Comparison
5)String Searching(substring)
6)Quit
Enter Your Choice:2
Enter a String:madam
A palindrome
press a Character !!!!!!
1)Reverse the Given String
2)Check for palindrome
3)Copy
4)String Comparison
5)String Searching(substring)
6)Quit
Enter Your Choice:3
Enter a String:Asia
Result=Asia
press a Character !!!!!!
1)Reverse the Given String
2)Check for palindrome
3)Copy
4)String Comparison
5)String Searching(substring)
6)Quit
Enter Your Choice:4
Enter 1st string:India
Enter 2nd string:Asia
1st>2nd
press a Character !!!!!!
1)Reverse the Given String
2)Check for palindrome
3)Copy
4)String Comparison
5)String Searching(substring)
6)Quit
Enter Your Choice:5
Enter 1st string:Pacific
Enter 2nd string:c
string found at location:3
string found at location:7
press a Character !!!!!!
1)Reverse the Given String
2)Check for palindrome
3)Copy
4)String Comparison
5)String Searching(substring)
6)Quit
Enter Your Choice:6
Source projectgeek.com