I'm trying to write a program that when given a string it prints it back whats.
here is my program.Note I'm a biginner in C. -
#include <stdio.h>
-
#include <string.h>
-
char input[];
-
main(){
-
//gets(input);
-
scanf("%s",input);
-
reverse(input);
-
}
-
-
void reverse(){
-
int length,i;
-
length = strlen(input);
-
char reverse[length];
-
for(i=0;i < length;i++){
-
reverse[i] = input[length - i - 1];
-
}
-
printf("%s\n",reverse);
-
}
-
When I run the above program on input:
I get as my result:
I do not want that 12 4557
There are many errors with that code. - reverse is not declared before it is used and is wrongly defined: it does not take 0 arguments. The correct declaration is
void reverse(char * input); and this should appear before main(). - Return type for main() is missing: "int main()" not "main()"
- line 13: you can not declare an array with a size that is not known at compile time. Use malloc() instead.
- line 3: char input[]; you cannot declare an array like this unless you initialize it when you declare it in which case the the compiler can fill in the size required, like char str[] = "hello";. Use char input[80]; or something like this instead if you don't expect the user to input strings longer than 80 characters.
I am amazed that your code could possibly compile with all those fundamental errors, I suggest you get a better compiler and enable all warnings, especially if you're learning because it's very important you don't develop bad habits. To clear up any confusion regarding arrays, I suggest you read this.
You should place terminating zero in the reversed array.
Thank you very much, now I know where I made errors.
There are many errors with that code.- reverse is not declared before it is used and is wrongly defined: it does not take 0 arguments. The correct declaration is
void reverse(char * input); and this should appear before main(). - Return type for main() is missing: "int main()" not "main()"
- line 13: you can not declare an array with a size that is not known at compile time. Use malloc() instead.
- line 3: char input[]; you cannot declare an array like this unless you initialize it when you declare it in which case the the compiler can fill in the size required, like char str[] = "hello";. Use char input[80]; or something like this instead if you don't expect the user to input strings longer than 80 characters.
I am amazed that your code could possibly compile with all those fundamental errors, I suggest you get a better compiler and enable all warnings, especially if you're learning because it's very important you don't develop bad habits. To clear up any confusion regarding arrays, I suggest you read this.
Thank you very much, now I know where I made errors.
There also is a logic error in your code because lines 14-15 don't reverse
a string, e g. the string "abcd" is turned into "dccd" and not what you wrote.
kind regards,
Jos
There also is a logic error in your code because lines 14-15 don't reverse
a string, e g. the string "abcd" is turned into "dccd" and not what you wrote.
Why? He is not reversing it 'in place', there is a separate destination string.
Why? He is not reversing it 'in place', there is a separate destination string.
My bad; I missed that one completely; I apologize for that remark.
kind regards,
Jos
The reason it gave you a random charachter in the end was because the last element of the array reverse that was given a value(in the for loop) was reverse[lengh-1].
And I don't see why you have to use another array called reverse to store the reverse of the string if all you want the function to do is print.
How do I place a terminating zero in the end of my string?
How about - myCString[stringLength] = '\0'
Is this solution corrent?It works -
#include <stdio.h>
-
#include <string.h>
-
#define max 100
-
main(){
-
char string[max];
-
scanf("%s",&string);
-
int length = strlen(string);
-
char reverse[100];
-
int i;
-
for(i=0;i<length;i++){
-
reverse[i] = string[length - 1 - i];
-
}
-
-
reverse[i] = '\0';
-
printf("%s\n",reverse);
-
}
-
That looks good, but here
main should return an int. Make it -
int main() {
-
//...
-
return 0;
-
}
-
And here
use max.
Looks good though.
Why should I make it return an integer, I often see this in other peoples programs? Whats the importance of returning an integer?
That looks good, but here
main should return an int. Make it -
int main() {
-
//...
-
return 0;
-
}
-
And here
use max.
Looks good though.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: google |
last post by:
I'm trying to implement something that would speed up data entry. I'd
like to be able to take a string, and increment ONLY the right-most
numerical characters by one. The type structure of the...
|
by: Sathyaish |
last post by:
This one question is asked modally in most Microsoft interviews. I
started to contemplate various implementations for it. This was what I
got.
#include <stdio.h>
#include <stdlib.h>
#include...
|
by: Henk |
last post by:
Hi Guys,
(see actual code below)
I wrote a little program that is supposed to read a file (input.txt)
into memory (using a stack) and then reverse the file and display it to
output. It works,...
|
by: rick |
last post by:
Why can't Python have a reverse() function/method like Ruby?
Python:
x = 'a_string'
# Reverse the string
print x
Ruby:
x = 'a_string'
# Reverse the string
|
by: samimmu |
last post by:
this is my code i made this code because to reverse the words and get the number or frequent characters.this my code below.
#include <iostream>
#include <cstring>
using namespace std;
...
|
by: APEJMAN |
last post by:
I know what I'm posting here is wired, but it's been 3 days I'm workin g on these codes, but I have no result
I post the code here
I dont wanne bother you, but if any one of you have time to...
|
by: sunnyluthra1 |
last post by:
Hi,
I was creating an Application in MS Access for Geocoding a particular Address from Google to get the Lat & Long.
I successfully able to did that.
Here is the code:...
|
by: 032386 |
last post by:
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
using namespace std;
void reverse(string& s)
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |