Friday, June 26, 2020

Reverse a char array

Input Format:

Input consists of a string. 

Assume that the maximum length of the string is 50.

Output format:

Refer sample input and output for formatting specifications.

Sample input &output

Excellent

tnellecxE

SOLUTION:

#include <iostream>
using namespace std;
int main(){
char str[100], rev[100];
  cin.getline(str,100);
  int i, j = 0;
  while(str[j] != '\0'){
  j++;
  }
  j--;
  char temp;
  for(i = 0; i <= j; i++,j--){
    temp = str[i];
    str[i] = str[j];
    str[j] = temp;
  }
  cout<<str;
}

No comments:

Post a Comment

horizontal ads