Cutting off repeating chars & placing chars into array

I am doing a project that requires me to take a keyword and not allow it to have any repeating characters. For example if it was HAPPINESS, it would have to become HAPINES, and then I have to put it into a 5x5 array such that ‘H’ would be {0,0}) and ‘E’ would be {1,0}.Of course, I don't expect anyone to do it for me, but if someone could please point me in the right direction. I have been trying various ‘for loops’ to try and cut off repeating characters and for putting it into the array, but have been thus far unsuccessful. I also don’t know how to delete an array element such that if it had 7 elements and you deleted the 4th, the 5th would become the 4th, the 6th would become the fifth and so forth. I know I’m asking for a lot and, again, I don’t expect it to all be answered.

Any help would be greatly appreciated!
You are right that you cannot change the size of an array. I would make another array of the same size, and fill it with non-alphanumeric characters (like tildes). Then, start putting the letters in that array one by one. Iterate through the new array and make sure the current letter isn't in it. If you come across it, go to the next letter in the original word, and start iterating through the new array again. If you come across a tilde, you know that you've reached the end of the word in the new array, and the new array does not contain your letter, so go ahead and add it, and then move to the next letter.

If you know what a std::string is, and you are allowed to use that, then use that instead of a char array. Start with a blank one, and iterate through it until you come to the end. If you get to the end and it isn't there, add the current character to the end. If you don't know what it is, or you aren't allowed to use it, ignore this paragraph.

I'll leave the part about fitting it into a 5x5 array up to you. If anything is unclear to you, let me know.
Yeah, the 5x5 array is a trick.

First, unique the string "HAPPINESS" (or whatever is input) in a normal, one-dimensional array (or a std::string, if you're allowed to use it).

Once you have that, you copy it to the 5x5 array.
(If it is an actual 5x5, 2D array type, then you can simply copy() [or strncpy()] it!)

1
2
3
4
5
6
7
char the_array[5][5] = {{0}};
char the_string[] = "HAPINES";

strncpy(
  @(the_array[0][0]),  // the destination is a 2D array, we're treating it like a 1D array
  the_string,            // the source is the string
  min<size_t>( strlen(the_string), 5*5 );  // stop copying at 5x5 elements or the length of the string, whichever comes first 

Of course, the purpose may be for you to use a loop. You'll need to instead have a double loop:

1
2
3
4
5
6
7
8
9
10
int n=0;  // index into the_string
for (int row=0; row < 5; row++)
for (int col=0; col < 5; col++)
{
  if (the_string[n] == '\0')
    break;
  else
    the_array[row][col] = the_string[n];
  n++;
}

Sorry, don't know how to explain that without just, sort of, giving... an answer.

Hope this helps.
Topic archived. No new replies allowed.