Encrypting problem

In the third function of my program, I am to reverse each word of the sentence so
IF BUILDERS BUILT BUILDINGS THE WAY PROGRAMMERS BUILT PROGRAMS THEN THE FIRST WOODPECKER THAT CAME ALONG WOULD DESTROY CIVILIZATION

translates to:

FI SREDLIUB TLIUB SGNIDLIUB.. and so on.

This is what code I have for this function so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void getReverseCipher(char clean[])
{
   char words[25][20]; // 2-d array to manipulate individual words
   char cipherThree[500];
   int k = 0;


   for (int i = 0; i < 25; i++)
      {
         for (int j = 0; j < 20; j++)
            {
               if ((clean[i] >= 'A')&&(clean[i] <= 'Z'))
                  {
                     words[i][j] = clean[k];
                     k++;
                  }

               else
                  break;

            }

      }

   for (int i = 0; i<25; i++)
     {
       for (int j = 0; j<20; j++)
          {
             cout << words[i][j];
          }
     }

}


Ideally, with the first for loop, I'd like to split the words in a 2d array so the words array is like words[words][lettersofwords].

I am trying to get it so if it is a letter between A and Z, store that into the array, but if its anything else (i.e a space or linebreak), just simply break out of the nested for and increment i.

The second for loop is just a test loop to see what is being stored in my array.
When the loop executes, however, there is a fair amount of junk, and I don't think that its breaking out of the nested loop if the character isnt from A-Z.

The reason I am doing it this way is so that when I have each word stored into the 2d array, I can flip each word and store it back into a 1D array to be written to a text file.

I really need help guys, I have to work soon and this is the last leg of my project before its due at midnight.
Suggestions?
Last edited on
Also, I am not to use any strings in this program. What you see is pretty much the extent of what I'm allowed to use.
Also, the array "clean" contains all the text to manipulate from above, spaces included
The reason you're getting a lot of junk is (partly) because you don't use a lot of the array spaces.
Since the extra spots aren't initialized, when you try to print out everything in the array, you get what you want (if you coded it correctly) plus all of the junk in the unused array slots.
So I'd suggest initializing all of the values of words to zero first before you do anything else.

Now, as for the rest of the code:
12
13
14
15
16
17
18
19
if ((clean[i] >= 'A')&&(clean[i] <= 'Z'))
   {
      words[i][j] = clean[k];
      k++;
   }

else
   break;

First, it should be if ((clean[k] >= 'A')&&(clean[k] <= 'Z')).
Now, even if the condition is false, you still need to increment k, or else k will get stuck on the position of the first space.

There's also going to be a point where the string you inputted into clean ends, but you might still have some extra array slots after that.
So, at the beginning of the j loop, you need to check for if clean[k] == '\0' (checking for the end of the string), and if that happens, you'll have to break out of both loops.

After you do all of that, you should have each word (assuming you don't have lowercase letters or other punctuation marks) split into its own words[i] spot.

Just keep in mind when reversing:
-- Each of the words[i]s is not necessarily going to have all of the j spots filled (if you know what I mean). This comes simply from the fact that not every word is going to have 20 letters.
If you initialized all of the values of words to zeros at the beginning like I mentioned above, you can be sure that each word will be either 20 letters long or will end with a null character ('\0').

-- Not even all of the words[i]s themselves are going to be filled (since there won't necessarily always be 25 words).
The unused spots will be filled with all null characters if you initialized words at the beginning.
Topic archived. No new replies allowed.