Reputation: 71
I have been asked to write a code to print only consonants using a dynamic array. I wrote a code to do that but it prints exactly the input without canceling out the vowels.
#include <iostream>
#include<string>
using namespace std;
int main()
{
int len,i;
cin>>len;
char* pArray=new char[len];
char ch[len];
for(i=0;i<len;i++)
cin>>ch[i];
for(i =0;i<len;i++){
if(ch[i]=='a' && ch[i]=='e' && ch[i]=='i' && ch[i]=='o' && ch[i]=='u')
break;
else
pArray[i]=ch[i];
}
for( i=0;i<len;i++)
cout<<(pArray[i]);
return 0;
}
If I write the input ample
, it should print only mpl
.
Upvotes: 0
Views: 3149
Reputation: 1062
Quickly fixing the code inline:
every warning(1) error(4) is commented inline.
#include <iostream>
#include<string>
using namespace std;
int main()
{
int len, i;
cin >> len;
char* pArray = new char[len];
char* ch = new char[len];
for (i = 0; i < len; i++)
cin >> ch[i];
//WARNING: please note that pArray NOR ch isn't NULL terminated
int j = 0; //ERROR 1: you need to count the inserted char
for (i = 0; i < len; i++) {
//ERROR 2: you need an OR in order to check for vowels, not an AND
if (ch[i] == 'a' || ch[i] == 'e' || ch[i] == 'i' || ch[i] == 'o' || ch[i] == 'u')
//ERROR 3: if found, do not break, instead skip the vowels...
continue;
else
pArray[j++] = ch[i]; //ERROR 1bis: so you increment the j every time you insert a char
}
for (i = 0; i < j; i++) //ERROR 4: print out only the added lettere, indeed pArray is longer than needed (space for further improvement?)
cout << (pArray[i]);
return 0;
}
Hope it helps, Stefano
Upvotes: 0
Reputation: 57728
I believe there is a hole in your logic. IMHO, you can't have the same index for the ch
(source) array and the pArray
(target or destination) array.
Draw out the source and target arrays:
Source (example):
+---+---+---+---+---+
| H | e | l | l | o |
+---+---+---+---+---+
Target:
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
After ignoring the e
, your Source will be pointing at the first 'l' and your target will be pointing at the 3rd slot in pArray
:
Source (example):
+---+---+---+---+---+
| H | e | l | l | o |
+---+---+---+---+---+
0 1 2 3 4
Target:
+---+---+---+---+---+
| H | | l | | |
+---+---+---+---+---+
0 1 2 3 4
In the simple version you will need to have one index that represents the "next" available slot in the target array, and one index that represents the current letter in the source array.
(Or you could use another pointer to the target array and increment that)
char * p_target_slot = pArray;
int target_index = 0;
for(i =0;i<len;i++)
{
if( ch[i]=='a'
|| ch[i]=='e'
|| ch[i]=='i'
|| ch[i]=='o'
|| ch[i]=='u')
continue;
else
{
*p_target_slot++ = ch[i];
// Or you can use this:
pArray[target_index] = ch[i];
++target_index;
}
}
Upvotes: -1
Reputation: 32596
doing
for(i =0;i<len;i++){
if(ch[i]=='a' && ch[i]=='e' && ch[i]=='i' && ch[i]=='o' && ch[i]=='u')
break;
else
pArray[i]=ch[i];
}
the test ch[i]=='a' && ch[i]=='e' && ch[i]=='i' && ch[i]=='o' && ch[i]=='u'
is always false because a character cannot be several at the same time.
you also need a dedicated index to write in pArray and not print after based on len but that additional index.
Also check cin>>len;
success (and other use of >>
) else you do with len is 0 and none of the next read success because you do not clear the error flag nor bypass the invalid input.
Note also there are not only vowel and consonant, so when a character is not vowel that does not means it is a consonant, what must be done for other characters ?
Do not use variable length arrays as you did for ch.
Out of that, why are you using array of characters rather than std::string
, is it required by statement ?
based on your code minimal changes also managing uppercase character can be :
#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
int len;
if (!(cin>>len))
cerr << "len is not a number" << endl;
else if (len <= 0)
cerr << "len must be > 0" << endl;
else {
char* pArray = new char[len];
char* ch = new char[len];
int i;
for (i=0;i<len;i++){
if (!(cin>>ch[i])) {
cerr << "EOF, abort" << endl;
return -1;
}
if (!isalpha(ch[i])) {
cerr << "invalid character, abort" << endl;
return -1;
}
}
int j = 0;
for(i =0;i<len;i++) {
switch (ch[i]) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
case 'y': // is vowel no ?
case 'Y': // is vowel no ?
break;
default:
// suppose only consonant and vowel
pArray[j++]=ch[i];
}
}
for( i=0;i<j;i++)
cout<<(pArray[i]);
cout << endl;
}
return 0;
}
Compilation and execution :
pi@raspberrypi:/tmp $ g++ -Wall c.cc
pi@raspberrypi:/tmp $ ./a.out
5
ample
mpl
pi@raspberrypi:/tmp $
Upvotes: 1
Reputation: 87932
There's a few mistakes in your code. I'm feeling generous, see how you get on with this
int main()
{
// read the word
int length;
cin >> length;
char* word = new char[length];
for (int i = 0; i < length; ++i)
cin >> word[i];
// allocate enough space for the word without vowels
char* no_vowels = new char[length];
// copy the non-vowels
int no_vowels_length = 0;
for (int i = 0; i < length; ++i)
{
if (word[i] != 'a' && word[i] != 'e' && word[i] != 'i' && word[i] != 'o' && word[i] != 'u')
{
// found a non-vowel, copy it to the end of the no-vowel word
no_vowels[no_vowels_length] = word[i];
// and increase the length of the no vowel word
++no_vowels_length;
}
}
// write out the word without vowels
for (int i = 0; i < no_vowels_length; ++i)
cout << no_vowels[i];
}
I think the idea you are missing is that you need two variables for the lengths of your two different strings
Upvotes: 0
Reputation: 11
Your program isn't working because of logic error. You have used && operator between each logical expression. So, whenever the compiler come across a character it checks whether the character is 'a' and 'e' and 'i' and 'o' and 'u', which is obviously not possible simultaneously at the same time. Use "||" operator instead of "&&". Also for entering character in pArray you need to define another integer for its index .
int k=0;
for(i =0;i<len;i++){
if(ch[i]=='a' || ch[i]=='e' || ch[i]=='i' || ch[i]=='o' || ch[i]=='u')
break;
else
{pArray[k]=ch[i]; k++;}
Upvotes: 1