Working with a char array

I am working with a character array containing an equal number of vowels and consonants organized randomly. My goal is to reorganize them so that they alternate consonant, vowel, consonant, vowel... while still retaining their sequential order in the original array.

For this assignment I am only allowed to use one array (the input one) and I am not allowed to use any string library functions.

My program is (excluding the libraries and namespace std...):

char inputstring[9] = { 'i','a','m','s','a','e','t','b' };

char *con1 = 0, *con2 = 0 , *con3 = 0, *con4 = 0; // these are pointers that am designating for consonants and then vowels, i plan on outputting them in an alternating order at the end of the program
char *vow1 = 0, *vow2 = 0, *vow3 = 0, *vow4 = 0;

for (int i = 0; i < 8; i++)
{

if ((inputstring[i] == 'a') || (inputstring[i] == 'e') || (inputstring[i] == 'i') || (inputstring[i] == 'o') || (inputstring[i] == 'u'))
{
if (vow1 ==0 )
{
vow1 = &inputstring[i];
}
else if (vow2 == 0)
{
vow2 = &inputstring[i];
}
else if (vow3 == 0)
{
vow3 = &inputstring[i];
}
else if (vow4 == 0)
{
vow4 = &inputstring[i];
}
}
else
{
//i have left this intentionally empty while i figure out how to implement this
}
}

return 0;

When this program is run and i output vow1, vow2, vow3, vow4 i obtain the inputstring[] from the vowels' position to the end of the array, so vow1 is "iamsaetb" while vow4 is "etb" instead of the intended simple character.

How come my pointers are outputting the entire array instead of the intended individual character?

Thanks for the help.
Just do not use *char but char instead for conX and vowX... remove the addresssign in front of the inputstring[i] in the loop, and tadaa... your programm works.

To the else part... You could just use the else statement and do another if, else if block (like you did with the vows).
If your character is eather a, e, i, o or u, put them in the next, free vow variable.
If it is not, put it in the next, free con variable.
Last edited on
It doesnt work like that because if I do that it will simply assign the last position of the array to each value vow1,vow2... and output inputstring[8] which is empty, when i want to put the specific value of the array at position [i]
Last edited on
oh I've been fiddling around with it and you are right it does work that way but I am required to implement my solution using pointers
Then just do the same you did above, but when you output the code, use *vow1 and it should work.
I had some time to play with this one today, thought I would share.

The constraints that you have been given make this a really expensive exercise. I will only give you my thought process because I don't want to risk typing it out and giving you something that doesn't work at all:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
keeping track of if I'm looking for a consonant or vowel,
for every character in array
  if the character is what I'm looking for
    continue
  
  create a new pointer assigned to the current address
  // char *ptr = ( arr + index )

  move the pointer forward while it is not what I'm looking for
  create a character with the value of the dereferenced pointer

  while the pointer does not point to the current index + 1
    copy the previous index to the current one and move the pointer back
  
  //while ( ptr != arr + index + 1 )
  //{
  //  *ptr = *( ptr - 1 );
  //  --ptr;
  //}

  assign the current character ( *( arr + index ) ) the value of the character that I was looking for
  switch whether I am looking for a vowel or a consonant 


The interations of the loop would go as follows:
1
2
3
4
5
6
7
8
9
10
11
baeioucdf // start
baeioucdf // we wanted the b
baeioucdf // we wanted the a
// find the c, copy it, and cycle back down to the e
  baeiouudf
  baeiooudf
  baeiioudf
  baeeioudf
// place the c
baceioudf
// rinse and repeat 


I tried for a fair amount of time to do this any better in-place, and couldn't get it working. I don't think it is possible to predict whether or not one will copy over unassigned data.

For example, I couldn't get this working in linear time:
aaabbb

In the end, I decided to make a copy of all the vowels and all of the consonants and just bang through them:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void alternate( std::string& str )
{
  // vectors are resizeable arrays.  We actually know what size we want already
  std::vector< char > vowels( str.length() / 2 );
  std::vector< char > consonants( str.length() / 2 );

  size_t v_idx = 0;
  size_t c_idx = 0;

  // for every character, if it is a vowel, put it with the vowels
  // otherwise, put it with the consonants
  for ( auto ch : str )
    isVowel( ch ) ? vowels[ v_idx++ ] = ch : consonants[ c_idx++ ] = ch ;
    // isVowel is a function I wrote

  str.clear();  // erase the contents of the string

  // put back the collected letters in order
  for ( size_t i = 0; i < consonants.size(); ++i )
  {
    str += consonants[i];
    str += vowels[i];
  }
}


It takes about half the time to do the alteration that it does to make the random string itself.
Last edited on
Topic archived. No new replies allowed.