-4

I just want to reverse the string order by switching the place of each index in the string.

#include <stdio.h>
#include <string.h>

void FirstReverse(char str[]) {  
  int a = strlen(str);

  for(int i=0; i<strlen(str) ;i++){
    str[i] = str[a-1];
    a-=1;
  }
}

int main(void) {
  // keep this function call here
  FirstReverse(gets(stdin));
  return 0;    
}

Error: "signal: segmentation fault (core dumped)"

GSerg
  • 76,472
  • 17
  • 159
  • 346
Timmy102
  • 9
  • 2

3 Answers3

2

There are multiple errors in your code. The obvious ones are that gets is used wrong (and, to be honest, that it is used at all) and it does not output the result in any way. But let's apply some quick fixes with minimal changes to your logic:

#include <stdio.h>
#include <string.h>

void FirstReverse(char str[]) {
  int a = strlen(str);

  for(int i=0; i<strlen(str) ;i++){
    str[i] = str[a-1];
    a-=1;
  }
}

int main(void) {
  char string[100];  // This is going to be our working field where the changes happen
  fgets(string, 100, stdin);  // read a line of up to 100 characters into "string"
  FirstReverse(string);  // do the work (this could be chained, like it was originally)
  puts(string);  // output the result
  return 0;
}

Now it compiles and executes without failing but the result is wrong:

In: My favourite string

Out: gnirts ette string

What went wrong? Let's follow what happens step by step:

i                  a
↓                  ↓
My favourite string
(program writes the last character [see point 3 below] in place of `M`)

 ↓                ↓
 y favourite string
(program writes `g` in place of `y`)

  ↓              ↓
 g favourite string
(program writes `n` in place of the space)

   ↓            ↓
 gnfavourite string
(program writes `i` in place of `f`)

etc.
         ia
         ↓↓
 gnirts eite string
(program writes `t` in place of `i`)

         ai
         ↓↓
 gnirts ette string
(program writes `t` in place of `t`)

        a  i
        ↓  ↓
 gnirts ette string
(program writes `e` in place of `e`)
etc.

Three problems here:

  1. By rewriting a character from the start by another from the end, you're not doing swapping. You're just copying the data from the end to start (but surely, in reverse order). The original is lost.

  2. You are actually going twice through, because by the time i reaches half of the interval, a keeps decreasing and they cross. Now i still needs to finish the loop and a continues towards the beginning of the string where you've already been. If you actually did swapping, you'd swap 1 with 2 and then later 2 with 1 again, resulting in the original unchanged!

  3. (minor) A string returned by (f)gets ends with a newline character, so that becomes the beginning of your result. Probably not what you want, but has an easy fix of chopping that off before feeding the string to your function.

You'll need to deal with each of these, other answers by now contain some advice. But I thought it instructive to run your code and try to "think like the machine" in explaining why the computer misunderstands your intention. If you swap the letters by copying one in a temporary variable, then rewriting str[i], then writing back in str[a-1] from the temporary, and stop before i and a cross each other, you can see for yourself you'll take care of 1 and 2.

Community
  • 1
  • 1
The Vee
  • 11,420
  • 5
  • 27
  • 60
1
  1. run only till mid of the string
  2. keep in some var the overridden char in side the loop

#include<stdio.h>

#include <stdlib.h>

#include <string.h>

void FirstReverse(char str[]) {

    int a = strlen(str);

    for (int i = 0; i <= a; ++i, --a) {
        char c = str[i];
        str[i] = str[a - 1];
        str[a - 1] = c;
    }
}

int main(void) {
    // keep this function call here

    char s[100] = { 0 };
    scanf("%s",s);

    FirstReverse(s);

    printf("%s",s);

    return 0;
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
nivpeled
  • 1,810
  • 5
  • 17
  • Your code reads and reverses a word. You should at least mention this difference compared to the question code. – hyde Aug 01 '19 at 09:24
  • Thanks for showing me another way to solve the problem! Much appreciated! – Timmy102 Aug 02 '19 at 07:27
0

Simple way to do it:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char * reverse(char *str)
{
    size_t size = strlen(str);
    char *res = malloc(size * sizeof(char));
    for(size_t i=0; i<size; i++) {
        res[i] = str[size-i-1];
    }
    return res;
}

int main(void)
{
    printf("%s\n",reverse("Hello World"));
    return 0;
}

Output : dlroW olleH

Instead of switching each char, just create a new string in which you copy the last char, then the last-1 and so on. You'll also prevent yourself from switching the same char if the length of your string is uneven ('\0' put aside).

ooj-001
  • 180
  • 9