Home > Blockchain >  Skipping a word after comma in char array
Skipping a word after comma in char array

Time:06-01

I need to skip a word after a comma in char array. My code is next:

#include <iostream> 

int main()
{
    char r1[] = "Hello, listen to me. I want to ask, if you.";
    char del[] = ", ";
    int k = strlen(r1);
    int i = 0;
    while (i <= k) {
        if (r1[i] == del[0]) {//Checking if there is a comma
            i  ;
            if (r1[i] == del[1]) {//Checking if there is a space after co
                while (r1[i] != del[1]) {//Looking for a next space to skip a word
                      i;
                }
            }
        }
        else {//If no comma, just type a letter
            std::cout << r1[i];
              i;
        }
    }
}

But this does not work. I want to have an answer like: "Hello, to me. I want to ask, you." So it just should skip word after comma, not including space.

This code gives me just a code without commas: "Hello listen to me. I want to ask if you."

P.S. I need to do this as basic as i can because i am a beginner.

CodePudding user response:

#include <iostream> 

int main()
{
    char r1[] = "Hello, listen to me. I want to ask, if you. ,sda";
    char del[] = ", ";
    int k = strlen(r1);
    int i = 0;
    while (i <= k) {
        if (r1[i] == del[0]) {//Checking if there is a comma
            std::cout << r1[i];
              i;
            
            if (r1[i] == del[1] || r1[i] != del[1]) {//Checking if there is a space after comma
                  i;
                while (r1[i] != del[1]) {//Looking for a next space to skip a word
                      i;
                }
            }
        }
        else {//If no comma, just type a letter
            std::cout << r1[i];
              i;
        }
    }
}

Now this works. "Hello, to me. I want to ask, if you. ," - output

CodePudding user response:

The condition in while (r1[i] != del[1]) is always false right after the condition if (r1[i] == del[1]). You forgot i between them.

If you fix it, you get the next possible issue - spaces after commas are not printed.

  •  Tags:  
  • c
  • Related