11 Answers
New Answer#include <stdio.h> int main() { char a[100],b[100]; int start,end,count=0; gets(a); while(a[count]!='\0') { count++; end=count-1; } for(start=0;start<count;start++) { b[start]=a[end]; end--; } b[start]='\0'; printf("%s",b); return 0; }
Here is my suggestion In C++ #include<iostream> #include<bits/stdc++.h> using namespace std; int main(){ string a,b; cout<<"Enter the string: "; cin>>a; b=a; reverse(b.begin(),b.end()); cout<<b; return 0; } (//Note:- when you type this code in sololearn,plz remove cout<<"Enter the string: ")
As shown above in my code,first you can take the string as array of characters and then you can print it.next you can declare a variable called end and you can use a for loop by initializing the end value as the last member of the array.
Ok, don't worry, this is what the code is doing: Input - > chars[0] chars[14] - > output Input - > chars[1] chars[13] - > output ... And so on But as you can see, at the time you read them, char[14], char[13] ecc. are not initialized yet So you are actually reading garbage data. When this garbage data goes in output you will get strange char and even sounds
I will use the socratic method: What is the value of chars[14], when you have only initialized chars[0]?
Sololearn Inc.
535 Mission Street, Suite 1591Send us a message