-1
  for(int i=0,int y=19;i<=19,y>=0;i++,y--)
  {
      char k[y]=char x[i];
  }

I have declared char[20] for both k and x ,initialized x and now I'm trying to reverse the string. It is showing error expected ; before int and y was not declared in scope.

Zelldon
  • 4,988
  • 3
  • 31
  • 41
toudeno
  • 11
  • 1

2 Answers2

1

You can reverse the string by declaring it as an array, and then declaring an integer equal to the last letter's spot( first letter = 0).

int letter [6] = {l, e, t, t, e, r};
int x = 5;

for ( int x = 5; x>= 0; x--) {
cout<<letter[x];
}
AlbanianGamerYT
  • 298
  • 2
  • 16
  • So much wrong there... – Roddy Jun 14 '16 at 09:59
  • can you explain the error please – toudeno Jun 14 '16 at 10:06
  • Your answer is just wrong. `for (int i=0,j=100; i < 100; i++, j--)` is perfectly correct. Semicolons separate the three sections of the `for` construct, and commas separate multiple actions within each. Read the answer to the linked question – Roddy Jun 14 '16 at 10:15
  • Also the line `Int y = 19;` is incorrect (`Int` vs. `int`), and irrelevant, as you then try to define `y` in the loop. – Roddy Jun 14 '16 at 10:16
  • And finally, the line you copied from the question `char k[y]=char x[i]` is simply meaningless. – Roddy Jun 14 '16 at 10:17
  • i have made it work ,by removing int from for loop and declaring int before loop,,thanks everyone for nothing turns out stackovereflow is much less helpful than i anticipated ,,,thanks roddy for pointing in right direction – toudeno Jun 14 '16 at 11:04
1

this code reverses only one string

for(int i=0;i<length/2;i++)
{
    char temp= k[i];
´   k[i] = k[length-1-i];
    k[length-1-i] = temp;
}
Thomas
  • 1,672
  • 2
  • 17
  • 34
  • No, it doesn't. Try reversing the string "ab" (length=2) and see what happens. – Roddy Jun 14 '16 at 10:02
  • @Roddy if we insert "ab" the for loop will be executed once. temp gets the value "a", k[i] gets the value "b", k[length-1-i] so k[2-1-0] so k[1] gets the value "a" so at the end there is "ba" – Thomas May 03 '17 at 06:28