Please help Kiara to write the correct code to reverse a string.
Answers
Answer:
#include<iostream>
#include<string.h>
using namespace std;
int main ()
{
char str[50], temp;
int i, j;
gets(str);
j = strlen(str) - 1;
for (i = 0; i < j; i++,j--)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
cout << str;
return 0;
}
Explanation:
Answer:
For loops are used when you know you want to visit every character. For loops with strings usually start at 0 and use the string's length() for the ending condition to step through the string character by character
Explanation:
Python code to reverse a string using loop:
def reverse(s):
str = ""
for i in s:
str = i + str
return str
s = "Brainly"
print ("The original string is : ",end="")
print (s)
print ("The reversed string(using loops) is : ",end="")
print (reverse(s))
Output:
The original string is : Brainly
The reversed string(using loops) is : ylniarB