Replaces the contents of a string with the string reversed. - C String

C examples for String:String Function

Description

Replaces the contents of a string with the string reversed.

Demo Code

#include <stdio.h>

#define LIMIT 50// w  w  w  . j  a va  2s.  c  om

void reverse_string(char *string);
char * get(char *string, int n);

int main(void){
  char string[LIMIT];

  printf("Enter a string to reverse: ");
  get(string, LIMIT);

  while (string[0] != '\0'){
    reverse_string(string);
    printf("Your string reversed: %s\n", string);

    printf("Enter a string to reverse (empty line to quit): ");
    get(string, LIMIT);
  }
  puts("Bye");
  return 0;
}

void reverse_string(char *start){
  char *end = start;
  char tmp;

  // find location of the end of the string
  while (*(end + 1) != '\0')
    end++;

  // switch characters
  while (start < end){
    tmp = *start;
    *start = *end;
    *end = tmp;

    start++;
    end--;
  }
}
// wrapper for fgets that replaces first newline with null
char * get(char *string, int n){
  char *return_value = fgets(string, n, stdin);

  while (*string != '\0'){
    if (*string == '\n'){
      *string = '\0';
      break;
    }
    string++;
  }
  return return_value;
}

Result


Related Tutorials