Thursday 11 May 2017

All C Style Functions in One program

#include<iostream>
using namespace std;
char rev[100]={0};
char * strcopy(char * des,char * src);

int strlent(char* a);

int strncmpe(char * a,char * b,int n );

int strcompare(char* a,char* b);

char *stringnconcat(char *a,char *b,int n);

char *stringconcat(char *a,char *b);

char * stringnumbercopy(char *a,char * b,int n);

int strlent(char* a);

int stricmpe(char* a,char * b);

char *strtoupper(char* a);

char *strlower(char* a);

char *strchre(char * source,char a);

char *strrchre(char *a,char b);

char * reverse( char * a);


int main(){
int value=1;
while(value!=0){
cout<<"ENTER 1 to use strcpy function "<<endl;
cout<<"ENTER 2 to use strncpy function "<<endl;
cout<<"ENTER 3 to use strcat function "<<endl;
cout<<"ENTER 4 to use strncat function "<<endl;
cout<<"ENTER 5 to use strcmp function "<<endl;
cout<<"ENTER 6 to use strncmp function "<<endl;
cout<<"ENTER 7 to use strlen function "<<endl;
cout<<"ENTER 8 to use stricmp function "<<endl;
cout<<"ENTER 9 to use strupr function "<<endl;
cout<<"ENTER 10 to use strlwr function "<<endl;
cout<<"ENTER 11 to use strchr function "<<endl;
cout<<"ENTER 12 to use strrchr function "<<endl;
cout<<"ENTER 13 to use strrev function "<<endl;
cout<<endl<<"enter your choice "<<endl;
int choice;
cin>>choice;
switch(choice){
//case 1
case 1:
{
cout<<"Welcome to strcpy function "<<endl;
char as[120];
char asd[110];
cout<<"enter string in which you want data to be copied "<<endl;
cin>>as;
cout<<"enter string whose data is to be copied "<<endl;
cin>>asd;
strcopy(as,asd);
cout<<"after string 2 over rided string 1 =  "<<as<<endl;
break;
}
//case 2
case 2:
{
cout<<"Welcome to strncpy function "<<endl;
char as6[120];
char asd6[110];
int n;
cout<<"enter string in which you want data to be copied "<<endl;
cin>>as6;
cout<<"enter string whose data is to be copied "<<endl;
cin>>asd6;
cout<<"enter number of characters to be copied "<<endl;
cin>>n;
stringnumbercopy(as6,asd6,n);
cout<<"After n characters of string 2 over rided n characters of string 1 =  "<<as6<<endl;
break;
}
//case 3
case 3:
{
cout<<"Welcome to strcat function "<<endl;
char a[120];
char b[110];
cout<<"enter string in which you want data to be concated "<<endl;
cin>>a;
cout<<"enter string whose data is to be concated "<<endl;
cin>>b;
stringconcat(a,b);
cout<<"After string 1 and string 2 are concatenated =   "<<a<<endl;
break;
}
//case 4
case 4:
{ cout<<"Welcome to strncat function "<<endl;
char a5[120];
char b5[110];
int n;
cout<<"enter string in which you want data to be concated "<<endl;
cin>>a5;
cout<<"enter string whose data is to be concated "<<endl;
cin>>b5;
cout<<"enter number of letters to be concated "<<endl;
cin>>n;
stringnconcat(a5,b5,n);
cout<<"After string 1 is concatenated by n characters of string 2 =   "<<a5<<endl;
break;
}

//case 5
case 5:
{
cout<<"Welcome to strcmp function "<<endl;
char a1[120];
char b2[110];
cout<<"enter string 1 "<<endl;
cin>>a1;
cout<<"enter string 2  "<<endl;
cin>>b2;
char bi=strcompare(a1,b2);
if(bi==0){
cout<<"string equal "<<endl;
}
else{
cout<<"not equal "<<endl;
};
break;
}

//case 6
case 6:
{
cout<<"Welcome to strncmp function "<<endl;
char a3[120];
char b3[110];
cout<<"enter string 1 "<<endl;
cin>>a3;
cout<<"enter string 2  "<<endl;
cin>>b3;
cout<<"enter number of letters to be compaired "<<endl;
int n;
cin>>n;
int s=strncmpe(a3,b3,n);

if(s==0){
cout<<s<<endl;
cout<<"strings matched"<<endl;}
else{
cout<<s<<endl;
}
break;
}

//case 7

case 7:{
cout<<"Welcome to strlen function "<<endl;
char a3[120];

cout<<"enter string 1 "<<endl;
cin>>a3;
cout<<"The size of string 1 =  "<<strlent(a3)<<endl;
break;
  }

//case 8

case 8:
{
cout<<"Welcome to stricmp function "<<endl;
char a3[120];
char b3[110];
cout<<"enter string 1 "<<endl;
cin>>a3;
cout<<"enter string 2  "<<endl;
cin>>b3;
int gh=stricmpe(a3,b3);
if(gh==0){
cout<<"strings matched "<<endl;
}
else{
cout<<"strings not matched"<<endl;

}
break;
}

//case 9

case 9:{
char f[120];
cout<<"enter the string which you want to be converted to upper case "<<endl;
cin>>f;
cout<<"String in Uppercase letters =   "<<strtoupper(f)<<endl;
break;
  }

//case 10

case 10:
{
cout<<"Welcome to strlwr function "<<endl;
cout<<"enter the string which you want to be converted to lower case "<<endl;
char f[120];
cin>>f;
char* q=strlower(f);
cout<<"String in Lowercase letters =   "<<q<<endl;

break;
}

//case 11

case 11:
{
cout<<"Welcome to strchr function "<<endl;
char a[110];
cout<<"enter string for which you want something "<<endl;
cin>>a;
char b;
cout<<"enter word that you want to find "<<endl;
cin>>b;
char*q=strchre(a,b);
if(q=='\0'){
cout<<"item not found "<<endl;
}
else{
cout<<"The characters apperaing after "<<b<<" are =  "<<q<<endl;
}
break;
}

//case 12

case 12:{
cout<<"Welcome to strrchr function "<<endl;
char a[120];
cout<<"enter string for which you want something "<<endl;
cin>>a;
char b;
cout<<"enter word that you want to find "<<endl;
cin>>b;

char*p=strrchre(a,b);
p++;
cout<<"The characters apperaing after "<<b<<" from reverse are =  "<<p<<endl;
break;
}

//case 13

case 13:
{
cout<<"Welcome to strrev function "<<endl;
char a[120];
cout<<"enter the string that you want to reverse "<<endl;
cin>>a;
char*q=reverse(a);
cout<<"THE string you entered in reverse  =  "<<q<<endl;
break;
}


          }//switch bracket

cout<<"enter 0 to exit the program "<<endl;
cin>>value;

};//while bracket

 }//main bracket

char * strcopy(char * des,char * src){
while(*src!='\0'){
*des++=*src++;

}
return  des;
};
char * stringnumbercopy(char *a,char * b,int n){
for(int x=0;x<n;x++){
a[x]=b[x];
}
return a;
};
char *stringconcat(char *a,char *b){
while(*a!='\0'){
*a++;
}
do{
*a++=*b++;
}while(*b!='\0');
*a='\0';
return a;
};
char *stringnconcat(char *a,char *b,int n){
while(*a!='\0'){
*a++;
}
do{
*a++=*b++;
n--;


}while((*b!='\0')&&(n!=0));
*a='\0';
return a;
};
int strcompare(char* a,char* b){
while(*a==*b){
if(*a=='\0'||*b=='\0'){
break;
}
a++;
b++;
}
if(*a=='\0'&&*b=='\0'){
return 0;
}
else{
return -1;
}
};
int strncmpe(char * a,char * b,int n ){
int asd=n;
for(int x=0;x<asd;x++){
if(a[x]==b[x]){

n--;
}
}
if(n==0){

return 0;
}
else{

return 1;
}
};
int strlent(char* a){
int count=0;
while(*a!='\0'){
*a++;
count++;
}
return count;
};
int stricmpe(char* a,char * b){
int count=0;
for(int i=0;a[i]!='\0';i++){
if((a[i]-b[i]==32)||(a[i]-b[i]==-32)||(a[i]-b[i]==0)){

}
else {
count=-1;
break;
}


}
if(count<0){
return -1;
}
else{
return 0;
}
};
char *strtoupper(char* a){
for(int i=0;a[i]!='\0';i++){
if(a[i]>=97&&a[i]<=122){
a[i]=a[i]-32;
}
}
return a;
};
char *strlower(char* a){
for(int i=0;a[i]!='\0';i++){
if(a[i]>=65&&a[i]<97){
a[i]=a[i]+32;
}
}
return a;
};
char *strchre(char * source,char a){
int count=0;

while(source!='\0'){
if(source[count]==a){
break;
}

*source++;
}

return source+1;
};
char *strrchre(char *a,char b){
int count=0;
while(*a!='\0'){
*a++;
count++;


}

for(int x=0;x<count;x++){

if(*a==b){

break;
}
*a--;

}

return a;
};
char * reverse( char * a){
int j=0;
while(*a!='\0'){
j++;

*a++;
}
*a--;
for(int i=0;i<=j;i++){

rev[i]=*a--;
}
rev[j]='\0';
return rev;
}



No comments:

Post a Comment