Antonio Gvardijan
Antonio Gvardijan

Reputation: 73

Reversing a string in C using Visual Studio

I'm building a program for reversing a string in visual studio, and while I run the code and enter a word I want to reverse, the program crashes.

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

main(void) {
    char r[256];
    int i, d;
    printf("\nEnter the word you want to reverse : ");
    gets_s(" %s", r, sizeof(r));
    d = strlen(r);
    for (i=d;i!=0;i--) {
        printf("%s",i);
    }
    return 0;
}

Upvotes: 0

Views: 1462

Answers (3)

lost_in_the_source
lost_in_the_source

Reputation: 11237

void rev(char *s)
{
    char *start, *end;

    end = start + strlen(s) - 1;
    for (start = s; end > start; ++start, --end) {
        char tmp;

        tmp = *start;
        *start = *end;
        *end = tmp;
    }
}

Use the fgets function, and also put the reversing code in its own function, like I did. So the final code is

int main()
{
    char line[80];

    fgets(line, 80, stdin);
    /* don't allow empty string */
    if (*line == '\0') {
        fprintf(stderr, "Empty string is not a string\n");
        return 1;
    }
    /* remove the \n placed by fgets */
    remnl(line);
    rev(line);
    printf("%s\n", line);

    return 0;
}
void remnl(char *s) { s[strlen(s) - 1] = 0; }

Upvotes: 1

stjepano
stjepano

Reputation: 1152

Please note that I tried your program on Linux, so no MS Visual C++ and more specifically no conio.h and gets_s.

There are multiple problems with your program:

Your call to gets_s is incorrect, according to this and this, gets_s is defined as:

char *gets_s( 
  char *buffer,
  size_t sizeInCharacters
);

You are calling it with illegal arguments. Instead of gets_s(" %s", r, sizeof(r)); you need to call it like this:

gets_s(r, 256);

the first parameter is pointer to the string buffer where the gets_s function will store the line from input and the second is the size of the buffer, note that in char r[256] you can store 255 characters and terminating zero (\0).

Your for loop is incorrect instead of for (i=d;i!=0;i--) { you need to do it like this:

for (i=d-1;i>=0;i--) {

now the loop starts from last character instead of \0 and ends when the i < 0 ie. the last print will be when i=0.

And your final mistake is that you are using printf incorrectly instead of printf("%s",i); you need to do:

printf("%c",r[i]);

because you are printing characters: "%c" is for char output and r[i] is i-th character from string r (don't forget that we count from 0).

So, in total this is how the program should look like:

#include <stdio.h>
#include <conio.h> // does not exist on GCC (Linux)
#include <string.h>

main(void) {
    char r[256]; // 255 characters + \0
    int i, d;
    printf("\nEnter the word you want to reverse : ");
    gets_s(r, 256); // store at most 255 characters + \0 
                    // does not work on GCC (Linux) even with -std=C11
    d = strlen(r);

    // start from last character and include first
    for (i=d-1;i>=0;i--) {
        // %c - character, r[i] gets the i-th character from string r
        printf("%c",r[i]);
    }
    return 0;
}

Upvotes: 2

pierlo
pierlo

Reputation: 31

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

int main(void) {
    char r[256];
    int i, d;
    printf("\nEnter the word you want to reverse : ");
    gets_s(r, sizeof(r));
    d = strlen(r) - 1;
    for (i = d; i >= 0; i--) {
        printf("%c", r[i]);
    }
    _getch();
    return 0;
}

Upvotes: 0

Related Questions