Purchase Linux CDs / DVDs / Flash Drives at OSDisc.com

Welcome to Our Community

While Linux.org has been around for a while, we recently changed management and had to purge most of the content (including users). If you signed up before April 23rd, 2017 please sign up again. Thanks!

How do I reverse a String in C.....? :<

Discussion in 'General Computing' started by blackneos940, May 30, 2016.

  1. blackneos940

    blackneos940 Guest

    I tried it this way, from a Website:

    Code:
    #include <stdio.h>
    
    #include <string.h>
    
    int main()
    
    {
       char arr[100];
    
       printf("Enter a string to reverse\n");
    
       gets(arr);
    
       strrev(arr);
    
       printf("Reverse of entered string is \n%s\n",arr);
    
       return 0;
    
    }
    But it just gives me these Errors:

    Code:
    Print_Reverse_String.c: In function ‘main’:
    Print_Reverse_String.c:12:4: warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration]
        gets(arr);
        ^
    Print_Reverse_String.c:14:4: warning: implicit declaration of function ‘strrev’ [-Wimplicit-function-declaration]
        strrev(arr);
        ^
    /tmp/ccirfxGT.o: In function `main':
    Print_Reverse_String.c:(.text+0x2e): warning: the `gets' function is dangerous and should not be used.
    Print_Reverse_String.c:(.text+0x3f): undefined reference to `strrev'
    collect2: error: ld returned 1 exit status
    I know about the whole "gets" thing being dangerous if used improperly, but why is "strrev" not working.....? :< I assume it's in the string.h Header, but.... Anyway, thanks for any advice guys...... Gee, learning to Program can be HARD on your own....... D:


    (Log in to hide this advertisement)

     
  2. Bethlehem

    Bethlehem Guest

    Code:
    strrev()
    Big problem is right there, that function is not available.

    Try this instead:
    Code:
    #include<stdio.h>
    #include<string.h>
    
    intmain(){
      charstr[100],temp;
      inti,j=0;
    
      printf("\nEnter the string :");
      gets(str);
    
      i=0;
      j=strlen(str)-1;
    
      while(i<j){
    temp=str[i];
    str[i]=str[j];
    str[j]=temp;
    i++;
    j--;
      }
    
      printf("\nReverse string is :%s",str);
      return(0);
    }
    
     
    1 person likes this.
  3. blackneos940

    blackneos940 Guest

    I'll try it, but..... I think you forgot to put a space between "char" and "str"..... Thank you for the help, good sir....... :3
     
  4. blackneos940

    blackneos940 Guest

    Ok..... :3 So, after doing some Code cleanup, I can confirm that it works!..... ^^ I thought it would!..... ^^ Thanks!!..... :3
     
  5. rstanley

    rstanley Guest

    There is no Standard Library function for the reversal of a string. You will need to code this yourself.
    And
    gets() vs fgets() is not just an opinion. The C99 C Standard depreciates gets(), and the C11 Standard removes gets() completely! Yes, this breaks older code recompiled using C99 & C11 Standards, but can be easily corrected, and should be! (Or, compile specifying using the C89/90 Standard, though not recommended! You are probably using a compiler that defaults to the C99 or C11 Standard!

    You should be aware that fgets() will bring in the newline char ('\n') along with the string. You will need extra code to delete it. (I would have designed fgets() to optionally discard the newline!) ;^)

    If you will be calling this code from more than one program, or more than one place in the same program, you might want to create a personal library and add this function to it.

    Try this corrected and expanded version of @Bethlehem's code:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>  /* For EXIT_SUCCESS & EXIT_FAILURE */
    
    #define DIM 100  /* Avoid "Magic Numbers" in your code! */
    
    void strrev(char *str, const size_t size);
    
    int main(void)
    {
       /*  I strongly recommend initializing all of your local variables! */
       /* strlen() and other functions return a size_t value not int.  
       size_t is an unsigned integer type, unsigned int, or unsigned long. */
       size_t len = 0;
       char buff[DIM] = "";
    
      printf("Enter the string : ");
    
      /* Always use fgets() rather than gets()! */
      fgets(buff, DIM, stdin);
    
      len = strlen(buff);
    
      /* Check for, and remove a newline char input by fgets() */
      if(buff[len - 1] == '\n')
      {
      buff[len - 1] = '\0';
      len--;
      }
    
      strrev(buff, len);
      printf("Reverse string is : %s\n", buff);
    
      return(EXIT_FAILURE);
    }
    
    void strrev(char *str, const size_t size)
    {
      char temp = '\0';
      size_t i = 0;
      size_t j = size - 1; /* Point to the last char in the string. */
    
      while(i < j){
      temp = str[i];
      str[i] = str[j];
      str[j] = temp;
      i++;
      j--;
     }
    }
     
    2 people like this.
  6. blackneos940

    blackneos940 Guest

    Thanks for the advice, good sir..... :3 I've been meaning to find out how to use "fgets" in this code..... :) Also, thank you for the Code, Mr. Penguin!....... :3 I'll compile it, and then run it..... Though I'm sure it'll come out fine..... :) But, while learning Programming, did you ever get intimidated by it.....? I ask, because I'm trying to avoid practicing Programming right now....... XD Seriously, what is WRONG with me.......?? :D
     
  7. blackneos940

    blackneos940 Guest

    Also, I didn't know that the more current C Standard(s) got RID of it..... :O I just thought you had to use it with caution....... :O Also, it works!..... ^^ Thank you, Senpai!..... ^^
     
  8. rstanley

    rstanley Guest

    In the answer to your first question, no, I jumped in with no hesitation! I eventually had to stop due to a change of direction in my business, but in recent years am trying to get back to it.

    As to your second question, there is nothing wrong with you! Any person's interest will change over time. I used to be a professional advertising photographer in Boston, and then in New York, before I switched over to computers!

    If your interests are drifting in a different direction, then go for it! Do whatever makes you happy!
     
    1 person likes this.
  9. Bethlehem

    Bethlehem Guest

    I agree with Happy Feet up there^ - there's nothing 'wrong' with you. Interests change.
    In my illustrious career I was a bouncer at a strip club, welder, robotics/laser programmer... Those were my 'professional interests' early on. Computers were fun - but this was before or during the very early stages of the big internet boom. Computers were just fun, learning *some* programming and or scripting was all but a necessity.
    Then I found I could make money do what was *fun* to me, worked for HP, Lexmark - moved to Michigan and landed a job at a big ISP, worked at SGI in Dearborn for bit (that was a fun job there)..... Got bored of the fishbowl/sysadmin work. Healthcare IT (HIPAA) was getting a foothold then so I started a small consulting business.... which grew to HIPAA MSP business. I LOVED working with people on this stuff! Human interaction was something missing from my previous IT work. Sure, open source project contribution was fun and all, Debconf, Linuxworld... stuff like that was a blast. But I really loved the daily interaction with various people. Now I'm older and am enjoying peace and quiet on a little island in the Pacific. Working in FreeBSD as a hobby, remotely maintaining Windows, Linux, and Unix servers professionally.
    That's life my friend.
     
    1 person likes this.
  10. blackneos940

    blackneos940 Guest

    Wow..... You've had quite the life..... :3 Thanks for the advice, good sir....... I don't think I'll ever grow bored of Programming, but I may go in a different direction with it..... For instance, I'm goin' to College for Server stuff!..... ^^ So, how's life out there in the Pacific, good sir.....? :3
     
  11. Bethlehem

    Bethlehem Guest

    @blackneos940
    Can't complain. Weather is good, water is warm, food is great!
    I'll die fat and happy.
     
    1 person likes this.
  12. blackneos940

    blackneos940 Guest

    Ah, so it was like me at first.....! :) I think I just need to overcome my fear of all those Keywords and Functions and whatnot..... :) Hey, do you have Boston Bagels where you are......? Man, I miss those....... :D
    Thanks for the advice, Mister Penguin....... :3
     
  13. blackneos940

    blackneos940 Guest

    Ah, food..... You and I think alike!..... Just make sure you go for plenty of walks, good sir!..... ^^ Man, if must be a BEAUTIFUL place to travel by foot..... :3 That's what I wanna do one day--- Travel the Country by foot!..... ^^
     
  14. blackneos940

    blackneos940 Guest

    Yeah, I LOVE Sushi..... :3
     

Share This Page