473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

A program to reverse a string.

thatos
105 100+
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.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. char input[];
  4. main(){ 
  5.   //gets(input);
  6.   scanf("%s",input);
  7.   reverse(input);
  8. }
  9.  
  10. void reverse(){  
  11.   int length,i;
  12.   length = strlen(input);
  13.   char reverse[length];
  14.   for(i=0;i < length;i++){
  15.     reverse[i] = input[length - i - 1];
  16.   }
  17.   printf("%s\n",reverse);
  18. }
  19.  
When I run the above program on input:
Expand|Select|Wrap|Line Numbers
  1. thato
I get as my result:
Expand|Select|Wrap|Line Numbers
  1. otaht&
I do not want that
Expand|Select|Wrap|Line Numbers
  1. &
Sep 8 '08 #1
12 4557
arnaudk
424 256MB
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.
Sep 8 '08 #2
newb16
687 512MB
You should place terminating zero in the reversed array.
Sep 8 '08 #3
thatos
105 100+
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.
Sep 8 '08 #4
JosAH
11,448 Expert 8TB
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
Sep 8 '08 #5
newb16
687 512MB
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.
Sep 8 '08 #6
JosAH
11,448 Expert 8TB
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
Sep 9 '08 #7
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.
Sep 9 '08 #8
thatos
105 100+
How do I place a terminating zero in the end of my string?
Sep 26 '08 #9
boxfish
469 Expert 256MB
How about
Expand|Select|Wrap|Line Numbers
  1. myCString[stringLength] = '\0'
Sep 26 '08 #10
thatos
105 100+
Is this solution corrent?It works
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define max 100
  4. main(){
  5.   char string[max];
  6.   scanf("%s",&string);
  7.   int length = strlen(string);
  8.   char reverse[100];
  9.   int i;
  10.   for(i=0;i<length;i++){
  11.     reverse[i] = string[length - 1 - i];
  12.   }
  13.  
  14.   reverse[i] = '\0';
  15.   printf("%s\n",reverse);
  16. }
  17.  
Sep 26 '08 #11
boxfish
469 Expert 256MB
That looks good, but here
Expand|Select|Wrap|Line Numbers
  1. main(){
main should return an int. Make it
Expand|Select|Wrap|Line Numbers
  1. int main() {
  2.     //...
  3.     return 0;
  4. }
  5.  
And here
Expand|Select|Wrap|Line Numbers
  1. char reverse[100];
use max.
Expand|Select|Wrap|Line Numbers
  1. char reverse[max];
Looks good though.
Sep 26 '08 #12
thatos
105 100+
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
Expand|Select|Wrap|Line Numbers
  1. main(){
main should return an int. Make it
Expand|Select|Wrap|Line Numbers
  1. int main() {
  2.     //...
  3.     return 0;
  4. }
  5.  
And here
Expand|Select|Wrap|Line Numbers
  1. char reverse[100];
use max.
Expand|Select|Wrap|Line Numbers
  1. char reverse[max];
Looks good though.
Sep 27 '08 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

21
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...
24
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...
14
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,...
41
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
4
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; ...
6
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...
1
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:...
2
by: 032386 | last post by:
#include <iostream> #include <vector> #include <string> #include <iterator> #include <algorithm> #include <functional> using namespace std; void reverse(string& s)
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.