A program to reverse a string.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thatos
    New Member
    • Aug 2007
    • 105

    A program to reverse a string.

    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.
    Code:
    #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:
    Code:
    thato
    I get as my result:
    Code:
    otaht&
    I do not want that
    Code:
    &
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    #2
    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.

    Comment

    • newb16
      Contributor
      • Jul 2008
      • 687

      #3
      You should place terminating zero in the reversed array.

      Comment

      • thatos
        New Member
        • Aug 2007
        • 105

        #4
        Thank you very much, now I know where I made errors.
        Originally posted by arnaudk
        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.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by thatos
          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

          Comment

          • newb16
            Contributor
            • Jul 2008
            • 687

            #6
            Originally posted by JosAH
            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.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by newb16
              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

              Comment

              • curiously enough
                New Member
                • Aug 2008
                • 79

                #8
                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.

                Comment

                • thatos
                  New Member
                  • Aug 2007
                  • 105

                  #9
                  How do I place a terminating zero in the end of my string?

                  Comment

                  • boxfish
                    Recognized Expert Contributor
                    • Mar 2008
                    • 469

                    #10
                    How about
                    Code:
                    myCString[stringLength] = '\0'

                    Comment

                    • thatos
                      New Member
                      • Aug 2007
                      • 105

                      #11
                      Is this solution corrent?It works
                      Code:
                      #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);
                      }

                      Comment

                      • boxfish
                        Recognized Expert Contributor
                        • Mar 2008
                        • 469

                        #12
                        That looks good, but here
                        Code:
                        main(){
                        main should return an int. Make it
                        Code:
                        int main() {
                            //...
                            return 0;
                        }
                        And here
                        Code:
                        char reverse[100];
                        use max.
                        Code:
                        char reverse[max];
                        Looks good though.

                        Comment

                        • thatos
                          New Member
                          • Aug 2007
                          • 105

                          #13
                          Why should I make it return an integer, I often see this in other peoples programs? Whats the importance of returning an integer?
                          Originally posted by boxfish
                          That looks good, but here
                          Code:
                          main(){
                          main should return an int. Make it
                          Code:
                          int main() {
                              //...
                              return 0;
                          }
                          And here
                          Code:
                          char reverse[100];
                          use max.
                          Code:
                          char reverse[max];
                          Looks good though.

                          Comment

                          Working...