Guys i really need some help here with this function, in my program i enter string to to encrypt. from my function i want to delete all spaces, i'm really having problems here hope someone can tell me were im going wrong. thanks again.

#include <iostream>          
#include <string.h>          
#include <ctype.h>
using namespace std;
void RemovedSpaces(char *str);


int main()
{
char clear[256];
char cipher[256];
char myString[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)); 

x = strlen(clear);

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

cout<<" Encrypted:"<< cipher << endl;
}
for(i=0; i<=x-1; i++)
{                                 
    if (isspace(clear[i]))
    {
        cipher[i] = clear[i];
    }
    else
    {
       cipher[i] = clear[i]+3;
    }
    
cout<<cipher[i]<<endl;
}
cin.get();
return 0;

}



void RemovedSpaces(char *str) {
     
     
     for (int i=0; cipher[i]; ++i)
    {                                     //  Need to remove spaces from cipher[i]
        if (cipher[i]!=' ')
          
         }
        cout<<cipher[i];
       }

Recommended Answers

All 10 Replies

here is one way to do it

void RemoveSpaces(char *str)7
{
   int i = 0;
   int length = strlen(str);
   while(i < length)
   {
      if( isspace(str[i]) )
      {
         memmove(&str[i], &str[i+1], length-1);
         --length;
      }
      else
          ++i;
   }

}

Thanks for the help, just one thing how would i go about outputting the result from the function that it shows in the main program. Thanks again for your help.

I don't understand your question. All you do is call that function from main() with the string that you want to have it work with. When RemoveSpaces() finishes the string in main() will not have any white spaces in it.

I'm sorry about the question, I just cant seem to be able to implement it that in int main() the white-spaces are removed. I'm pretty new to working with function and i'm still trying to understand them. thanks again for your repy

somewhere in main() RemoveSpaces(clear); Or replace variable clear with cipher.

Thanks for your help AD i got the program to execute put no joy with the spaces. thanks again.

#include <iostream>          
#include <string.h>          
#include <ctype.h>
using namespace std;
void RemoveSpaces(char *str);


int main()
{
   
char clear[256];
char cipher[256];
int x,i;
int opt;
RemoveSpaces(cipher);



  

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

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

x = strlen(clear);

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

cout<<" Encrypted:"<< cipher << endl;
;
}
for(i=0; i<=x-1; i++)
{                                 
    if (isspace(clear[i]))
    {
        cipher[i] = clear[i];
    }
    else
    {
       cipher[i] = clear[i]+3;
    }
    
cout<<cipher[i]<<endl;





}
 

cin.get();
return 0;

}




  void RemoveSpaces(char *str)
{
   int i = 0;
   int length = strlen(str);
   while(i < length)
   {
      if( isspace(str[i] ))
      {
         memmove(&str[i], &str[i+1], length-1);
         --length;
      }
      else
          ++i;
          
   
           
   }

 
}

you put line 15 in the wrong place. It has to go after line 30, after you enter the text to be encrypted. It can't remove spaces if there is nothing in the char array yet.

thanks for the tip, i put it in after line 30 still not working, i also tried other lines below this with no luck. it sure has me. Cheers AD.

Yes finally i got it working thanks again for your input AD, much appreciated.

Hey AD i have opened another function to remove all vowels from the string
it does not seem to work for me. if you could help me and show were i went wrong.
i woyuld appreciate it thank you.

void RemoveVowels(char *str) 
{      int i = 0;     int  lenght = strlen(str);      for(i=0;i<=-lenght;i++)     {              if (str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u' || str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]=='O' || str[i]=='U') {str[i]=' ';         }        }
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.