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

B

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:
 


B

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);
}
 
B

blackneos940

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);
}
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
 
B

blackneos940

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);
}
Ok..... :3 So, after doing some Code cleanup, I can confirm that it works!..... ^^ I thought it would!..... ^^ Thanks!!..... :3
 
R

rstanley

Guest
There is no Standard Library function for the reversal of a string. You will need to code this yourself.
Print_Reverse_String.c:12:4: warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration]
gets(arr);
^
And
Print_Reverse_String.c : ( .text+0x2e): warning: the `gets' function is dangerous and should not be used.
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--;
 }
}
 
B

blackneos940

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--;
}
}
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
 
B

blackneos940

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--;
}
}
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!..... ^^
 
R

rstanley

Guest
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.......??
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!
 
B

Bethlehem

Guest
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
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.
 
B

blackneos940

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.
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
 
B

blackneos940

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!
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
 
B

blackneos940

Guest
@blackneos940
Can't complain. Weather is good, water is warm, food is great!
I'll die fat and happy.
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!..... ^^
 

samK

New Member
Credits
200
  1. int main() { char s[100];
  2. printf("Enter a string to reverse\n"); gets(s);
  3. strrev(s);
  4. printf("Reverse of the string: %s\n", s);
  5. return 0; }
 

JasKinasis

Well-Known Member
Credits
1,193
  1. int main() { char s[100];
  2. printf("Enter a string to reverse\n"); gets(s);
  3. strrev(s);
  4. printf("Reverse of the string: %s\n", s);
  5. return 0; }
::slow clap::
Congratulations on another poor quality necropost on an already solved thread!

Once again, gets should NOT be used.
Also strrev is NOT a part of the C standard library. It’s actually part of the ancient Borland Turbo C library. It’s a non-standard extension that is in their string.h. It’s not available on Linux, so not only is it non standard, but it’s non-portable.
 


Members online


Top