0

First time here on StackOverflow. While studying and coding for my final exams at programming, I found a problem that I can mentally resolve, but when I try to put it in C++, it doesn't really work.

So the problem is in my native language, romanian, but i will try to translate it. So the problem wants me to read a word from keyboard and then to show it on the screen, eliminating a vowel each time. For example, if i enter in my program the word "programming" it should show:

  1. progrmming - eliminating a
  2. programming - eliminating e
  3. programmng - eliminating i
  4. prgramming - eliminating o
  5. programming - eliminating u

My code looks like this:

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    char v[6]="aeiou",s1[21],s2[21];
    int i,j;
    cin.get(s1,20);
    for(i=0;i<strlen(v);i++)
    {
        strcpy(s2,s1);
        j=0;
        while(j<strlen(s2))
        {
            if(strchr(v[i],s2[j])!='\0')
                strcpy(s2+j,s2+j+1);
            else
                j++;
        }
        cout<<s2<<endl;
    }
    return 0;

I have a error at the "if" statement, but i can't seem to find a fix for it. It works after i remove the "[i]" from "v[i]", but i need to eliminate only one vowel at a time, not all of them. Hopefully, someone can help me here. Thank you!

EDIT: Problem solved, i will let my code here, because the problem is taken from subjects of romanian baccalaureate at programming

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    char v[6]="aeiou",s1[21],s2[21];
    int i,j,x;
    cin.get(s1,20);
    for(i=0;i<strlen(v);i++)
    {
        strcpy(s2,s1);
        j=0;
        while(j<strlen(s2))
        {
            if(s2[j]==v[i])
                strcpy(s2+j,s2+j+1);
            else
                j++;
        }
        cout<<endl;
        cout<<s2<<endl;
    }
    return 0;
}

Sorry for the misunderstanding, but here, we are not taught to use things like std::string::find_first_of, just basic funtions like strcmp and stchr.

1 Answers1

0

Ok, the big mistake of yours is that you are iterating over the vowels before iterating over the string.

You should iterate over the input string and, for each char, check whether it is a vowel or not. You can check if a char is a vowel by using strchr over v.

For each character in the string you have two cases:

  1. The character is a vowel
  2. The character is not a vowel

In the first case, you should erase that character from the string, which implies shifting the remaining content of the string to the left.

For example, if your string is programming and you jumped into the first o, you would have to move the remaining gramming to the left:

0 1 2 3 4 5 6 7 8 9 10
p r o g r a m m i n g
p r   g r a m m i n g
p r g r a m m i n g

In the second case nothing happens (unless you are copying the string char by char to a secondary storage string, which is valid, btw).

Check out the following solution, it uses [memmove][1] to shift the string to the left, but it can also be done with a simple for loop. Read it, study it, and understand it. If you need more help don't hesitate to ask.

void incremental_vowel_removal(const char *instr) {
  // the vowels
  static const char *vowels = "aeiou";

  // length of the input string
  int len = strlen(instr);

  // I will work on a copy of the string because it is easier
  char *ostr = (char*)malloc((len+1) * sizeof(char));
  strcpy(ostr, instr);

  // for each char in the string
  for (int i = 0; i < len; ++i) {
    // if it is a vowel
    if (strchr(vowels, ostr[i])) {
      // shift remaining string to the left
      // this will erase the currect character from the string
      memmove(ostr+i, ostr+i+1, len-i);
      // print this partial result
      printf("%s\n", ostr);
      // the length is decreased by one, so let's reflect it
      len -= 1;
    }
  }

  // free the allocated memory
  free(ostr);
} 

Update: I replaced calloc with malloc because there is no need to zero-initialize the string because strcpy is called afterwards.

ichramm
  • 6,437
  • 19
  • 30
  • Yes, I corrected my errors. You have a different kind of writing code (`printf` instead of `cout`, I think this is normal C, right?), but I managed to understand the problem just by reading your comments and making some translations :D – Felix Ifrim Nov 21 '17 at 17:34
  • Glad you could translate it! Yes, some stuff comes from the C world but the code was compiled using a C++ compiler. – ichramm Nov 21 '17 at 17:57