Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Reversing array values
Message
Information générale
Forum:
Visual C++
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
01039402
Message ID:
01041297
Vues:
23
Herman,
You are very correct. Breaking out of the loop after you find \0 makes a lot of sense. I would also probably check the Index variable after the loop to make sure it got assigned a value (just in case there was no \n in the char array).

Einar
>Hi Einar & Ty Comer,
>
>Sorry for jumping in :)
>
>I just want to add some more correction. The thing is, if the Input char has more than one null char (up to the very end of the length), the Index will be equal to X. The error logic is in the first loop. It must break the loop when it found the null char for the first time. Actually it has more than one logic error but I think you pointed out that mistake already.
>
>Regards
>
>
>>Ty,
>>Try this instead:
>>
>>void ReverseArray(char Input[], char Reversed[])
>>{
>>  int i;
>>  int x;
>>  int Index;
>>
>>  // Get the index of the first null character
>>  for(x = 0; x < 50; x++)
>>  {
>>    if(Input[x] == '\0')
>>    {
>>      Index = x;
>>    }
>>  }
>>
>>  // Assign values to new array in reverse
>>  for(i = 0; i < Index ; ++i)
>>  {
>>    Reversed[i] = Input[Index - 1 - i];
>>  }
>>}
>>
>>
>>If you compare the code above to the code you posted you will notice that I use Index instead of x because x will always equal 49. I also removed the line where you decremented the x (which should have been the Index variable) variable.
>>
>>Hope my code works.
>>
>>Einar
>>
>>
>>>I am having a problem reversing the values in an array. The program I'm writing asks the user to enter a word, then I call the ReverseArray() function to reverse the values and cout the word backwards. When printing the reversed array to the screen, I get a bunch of odd sequencial characters. This is the syntax for my ReverseArray() function.
>>>
>>>=======================================================
>>>void ReverseArray(char Input[], char Reversed[])
>>>{
>>>
>>> int i;
>>> int x;
>>> int Index;
>>>
>>> // Get the index of the first null character
>>> for(x = 0; x < 50; x++)
>>> {
>>> if(Input[x] == '\0')
>>> {
>>> Index = x;
>>> }
>>> }
>>>
>>> // Assign values to new array in reverse
>>> for(i = 0; i < x - 1; ++i)
>>> {
>>> x--;
>>> Reversed[i] = Input[x - i];
>>> }
>>>}
>>>
>>>=======================================================
>>>What am I doing wrong? From what I can see, it should work just fine. When outputting 'Reversed' to the screen it shows a sequence of '||' for each character entered.
Semper ubi sub ubi.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform