Computer Science, asked by devirajendran5560, 1 year ago

Please help Kiara to write the correct code to reverse a string.

Answers

Answered by MoChuisle
2

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:

Answered by anjaliom1122
0

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

Similar questions