#include <iostream>          
#include <string.h>          
#include <ctype.h>
using namespace std;
void Removespaces(char clear[], char cipher[],int x,int i);
void Removevowels(char clear[],char cipher[],int x,int i);
int main()
{
char clear[256];
char cipher[256];
int x,i;
int opt;

cout<<"Encryption (1) 0r Decryption (2):"<<endl;
cin>>opt;
cin.ignore();
if(opt==1)
{
    

cout<<" Enter a string(sentence):";
 cin.getline(clear,sizeof(clear)); // calling the string(sentence)

x = strlen(clear);

for(i=0;i<=x-1;i++)              // for loop for Encrypting string(sentence)
{
cipher[i] = clear[i]+3;
}
cipher[x] = '\0';

cout<<" Encrypted:"<< cipher << endl;
     

                
if(opt==2)
{
cout<<" Enter a string(sentence):";
 cin.getline(cipher,sizeof(cipher));
  

x = strlen(cipher);
for(i=0;i<=x-1;i++)                               //for loop for Decryption
                   {
                   clear [i] = cipher [i] -3;
                   }
clear[x] = '\0';
cout <<"Decrypted String:" << clear << endl;       // Decrypted string(sentence)
}
}



system("pause");
return 0;
}




void Removespaces(char clear[],char cipher[],int x,int i)
{
     if (isspace(clear[i]))
    {
        cipher[i] = clear[i];
    }
    else
    {
       cipher[i] = clear[i]+3;
    }
}
}

     for(i=0;i<=x-1;i++)

{
if(cipher[i]!=' ')
{
cout<<cipher[i]<< endl;         
}
}

     
void Removevowels(char clear[],char cipher[],int x,int i)
{
      for(i=0;i<=x-1;i++)
      {
                         
     if (cipher[i]=='a' || cipher[i]=='e' || cipher[i]=='i' || cipher[i]=='o' || cipher[i]=='u' || cipher[i]=='A' || cipher[i]=='E' || cipher[i]=='I' || cipher[i]=='O' || cipher[i]=='U') {
cipher[i]=' ';                                  
}
cout<<cipher[i];
}
}

hey guys i never used functions before and i'm new to C++, so any help and advice is very welcome. my program gives two options 1 encrypt a string and 2 decrypt. i'm trying to sort option one first it encrypts the string but i want the functions preserve the whitespaces and then remove and next function to remove vowels.cheers in advanced guys hope you can help.

Recommended Answers

All 2 Replies

You did not design your functions as well as you could have. That will make it hard. A function should be a single simple task, and it should do the entire task as expected. The function RemoveSpaces() then should probably take a string and... remove all its spaces. A sample implementation could be:

void RemoveSpaces(char *str)//char * is basically the same as char[]
{
    for (int i=0; str[i]; ++i)//loop until we hit the null character (str[i]==0)
    {
        if (str[i]==' ')//its a space!
            str[i]=' '+3;//or whatever you set it to
    }
}

Now if you want to remove spaces from a string you just call RemoveSpaces with the string as its argument. Here is an example:

int main()
{
    char myString[256];
    cout<<"Enter a string!"<<endl;
    cin.ignore();//I think this is needed to get rid of the endl... but I could be wrong
    cin.getline(myString,sizeof(myString));
    RemoveSpaces(myString);
    cout<<"You string without spaces looks like: <<myString<<endl;
    return 0;
}

Of course a better implementation of RemoveSpaces would remove the spaces entirely, leaving no trace that they were there at all. But that would require an extra array and complexity that you do not need right now. Similarly you could write a function like this:

void Encrypt(char *str)
{
    //for the sake of example, this encryption will remove all the spaces
    //  and add 1 to each ASCII value
    RemoveSpaces(str);
    for (int i=0; str[i]; ++i)
        str[i]+=1;
}

The decrypt function that could go along with that could look like this:

void Decrypt(char *str)
{
    for (int i=0; str[i]; ++i)
    {
        str[i]-=1;//undo the adding one
        if (str[i]==' '+3)//this means that a space was removed by our RemoveSpaces function
            str[i]=' ';
    }
}

The resulting code would be very neat and tidy in main due to the benefits of modularization. A possible main could be:

int main()
{
    char myString[256];
    cout<<"Enter a string!"<<endl;
    cin.ignore();//again... I think this is right
    cin.getline(myString,sizeof(myString));
    cout<<"Make a selection:"<<endl;
    cout<<"\t1) Encrypt"<<endl;
    cout<<"\t2) Decrypt"<<endl;
    int selection;
    cin>>selection;
    if (selection==1)
        Encrypt(myString);
    else//we assume that they want to decrypt... a better solution is possible though
        Decrypt(myString);
    cout<<"Your string is now:"<<myString<<endl;
    return 0;
}

You did not design your functions as well as you could have. That will make it hard. A function should be a single simple task, and it should do the entire task as expected. The function RemoveSpaces() then should probably take a string and... remove all its spaces. A sample implementation could be:

void RemoveSpaces(char *str)//char * is basically the same as char[]
{
    for (int i=0; str[i]; ++i)//loop until we hit the null character (str[i]==0)
    {
        if (str[i]==' ')//its a space!
            str[i]=' '+3;//or whatever you set it to
    }
}

Now if you want to remove spaces from a string you just call RemoveSpaces with the string as its argument. Here is an example:

int main()
{
    char myString[256];
    cout<<"Enter a string!"<<endl;
    cin.ignore();//I think this is needed to get rid of the endl... but I could be wrong
    cin.getline(myString,sizeof(myString));
    RemoveSpaces(myString);
    cout<<"You string without spaces looks like: <<myString<<endl;
    return 0;
}

Of course a better implementation of RemoveSpaces would remove the spaces entirely, leaving no trace that they were there at all. But that would require an extra array and complexity that you do not need right now. Similarly you could write a function like this:

void Encrypt(char *str)
{
    //for the sake of example, this encryption will remove all the spaces
    //  and add 1 to each ASCII value
    RemoveSpaces(str);
    for (int i=0; str[i]; ++i)
        str[i]+=1;
}

The decrypt function that could go along with that could look like this:

void Decrypt(char *str)
{
    for (int i=0; str[i]; ++i)
    {
        str[i]-=1;//undo the adding one
        if (str[i]==' '+3)//this means that a space was removed by our RemoveSpaces function
            str[i]=' ';
    }
}

The resulting code would be very neat and tidy in main due to the benefits of modularization. A possible main could be:

int main()
{
    char myString[256];
    cout<<"Enter a string!"<<endl;
    cin.ignore();//again... I think this is right
    cin.getline(myString,sizeof(myString));
    cout<<"Make a selection:"<<endl;
    cout<<"\t1) Encrypt"<<endl;
    cout<<"\t2) Decrypt"<<endl;
    int selection;
    cin>>selection;
    if (selection==1)
        Encrypt(myString);
    else//we assume that they want to decrypt... a better solution is possible though
        Decrypt(myString);
    cout<<"Your string is now:"<<myString<<endl;
    return 0;
}

void RemoveSpaces(char *str)

Thank for the input, i was really lost, the functions got me. you made it look so easy and clear. just one more question when declaring

void RemoveSpaces(char *str)

in the main program is it just the same as it is in the function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.