-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.

0

2 Answers 2

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]; } 
Sign up to request clarification or add additional context in comments.

6 Comments

So much wrong there...
can you explain the error please
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
Also the line Int y = 19; is incorrect (Int vs. int), and irrelevant, as you then try to define y in the loop.
And finally, the line you copied from the question char k[y]=char x[i] is simply meaningless.
|
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; } 

2 Comments

No, it doesn't. Try reversing the string "ab" (length=2) and see what happens.
@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"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.