string str;
    int k=0, count = 0,namelength, size,j=0;
    char name[80];
    char extract[80];
    char reversing[80];
    cout << "Enter word or enter 'Q' to quit." << endl;
    cout << ">>";
    cin.get(name,80);
    size =strlen(name);
    int q=0;
        for(int i=0;i<=size;i++){
                if(isalnum(name[i])){
                           name[q]=name[i];
                           q++;       
                    }else{
                           name[i]= '\0';

                          }
                cout << name[i];
        /*line start here when removing the null spaces*/
        if(name[i] == '\0'){
                   name[k]=name[i];
                   k++;
                   cout << name[k];
                   }
        }

But it doesn't work. So how?

What do you mean it doesn't? Could you give an example of the input/output?

If, you mean like: "Hello my name is phoce" would then become "hellomynameisphorce" then you can do: std::remove_if(str.begin(), str.end(), isspace) or str.erase(str.begin(), str.end(), isspace), str.end()); Hope this helps.

Edited 3 Years Ago by phorce

I tried using it in a char using this format.

for(int i=0; i<=size;i++)
std::remove_if(name[i].begin(), name[i].end(), isspace)

But it doesn't work.

Are you allowed to use strings? This would be so much simplier if you were. Just copy the char array that you have, into a std::string and the following should work..

#include <iostream>
#include <algorithm> 
#include <ctype.h>

using namespace std;

bool is_space (const char &c) { return isspace(c); } 

int main() {
    // your code goes here

    char myStr[] = "hello my name is";

    std::string blah(myStr);
    //std::cout << myStr << std::endl;

    blah.erase(std::remove_if(blah.begin(), blah.end(), is_space));

    std::cout << blah;
    return 0;
}

^^ Is just an example, where myStr is a char array and blah is the string storing the char array.

Let me know how this works for you :)

Hello I tried to use it. But it doesn't work. My program is crashing. It's a palindrome so i need to use char. >.<.

++++

I'm trying to replace the value if the value is null but I'm stuck on this conditions.

    for(int x=0;x<=size;x++){
            if(name[x] == '\0'){

                       }
            }

Edited 3 Years Ago by laklaker: additional

I solved it by adding this loop.

    for(int x=0;x<=q;x++){
            if(name[x] == '\0'){
                       name[x]= '\0';
                       }
    cout << name[x];
            }

Thank you guys! LOL.

This article has been dead for over six months. Start a new discussion instead.