+ 1
Extra-Terrestrials task
https://code.sololearn.com/cppAL9w1bknG/?ref=app What is wrong with my code? Why canât I output a reversal word correctly without those strange characters before it?
11 Respostas
+ 2
#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;
}
+ 4
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: ")
+ 4
đ€đ€
+ 3
This is the answer in C
+ 3
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.
+ 2
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
+ 1
I will use the socratic method:
What is the value of chars[14], when you have only initialized chars[0]?
+ 1
You should first read the entire word ( scanf("%s", chars) ) and then reversing the array
+ 1
Angelo ohh, that makes all sense, thank you very much!
+ 1
Kalindu Wanasinghe excellent, bro, thank you!
0
Angelo sorry, still didnât get it