Programming Compiler
This assignment gives an opportunity to practice pointer and string manipulation. You should not call any existing string function from C++ string library. You need to implement three function defined as I do in copy function. //compare string s1 and s2, return 1 if s1>s2, return –1 if s1<s2,
//return 0 if s1 equal to s2
int strcmp(const char* s1,const char* s2);
//return the string length of string s
int length(const char* s);
//return the number of times character “c” in string “s”
int countChar(const char* s,const char c);
The main function is provided as: #include<iostream>
using namespace std;
int strcmp(const char*,const char*);
int length(const char*);
int countChar(const char*,const char);
void copy(char*, const char*); void menu();
void main()
{
char s1[20],s2[20];
bool done=false;
char c;
int choice;
while(!done){
menu();
cin>>choice;
switch(choice){
case 1:
cout<<"Please enter two strings\n";
cin>>s1>>s2;
if(strcmp(s1,s2)>0)
cout<<"\""<<s1<<"\" is greater than \""<<s2<<"\"\n";
else if(strcmp(s1,s2)<0)
cout<<"\""<<s1<<"\" is less than \""<<s2<<"\"\n";
else
cout<<"\""<<s1<<"\" is equal to \""<<s2<<"\"\n";
break;
case 2:
cout<<"Please enter a string\n";
cin>>s1;
cout<<"Length of \""<<s1<<"\" is "<<length(s1)<<endl;
break;
case 3:
cout<<"Please enter a string and a charater\n";
cin>>s1>>c;
cout<<c<<" appear in \""<<s1<<"\" "<<countChar(s1,c)
<<" times\n";
break;
case 4:
cout<<"Please enter a string\n"; cin>>s1; copy(s2,s1); cout<<"s2 now is "<<s2<<"\n"; break; case 5: done=true; break; default:
cout<<"Wrong option!\n";
}
}
}
void menu()
{
cout<<"Please enter one of the following\n"
<<"1. Compare two strings\n"
<<"2. Find string length\n"
<<"3. Count char in string\n"
<<"4. Quit"<<endl;
}
void copy(char* s1,const char* s2){ while(*s2!=0){ *s1=*s2;//copy a character from s2 to s1 s1++;//now s1 points to the next position s2++;//now s2 points to the next character } *s1='\0';//set end mark ; }
int strcmp(const char* s1,const char* s2)
{
/* add your code here*/
}
int length(const char* s)
{
/*add your code here*/
}
int countChar(const char*s,const char c)
{
/*add your code here*/
}