• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

String Operations with Pointers

June 26, 2011 by ProjectsGeek Leave a Comment

String operations with pointers

Program showing various operations on string(functions implemented
with pointers).
functions implemented:
(1) Reversing a string
(2) Palindrome
(3) Copy
(4) String comparison
(5) Searching a string(substring)

Source Code

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

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

Other Projects to Try:

  1. Database Operations without pointers source code
  2. String Operations in C Program
  3. Matrix Operations with Pointers
  4. string operations such as Copy, Length, Reversing, Palindrome, Concatenation
  5. String Operations in C Language

Filed Under: Fundamentals of Datastructure Tagged With: Datastructure Assignments

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy