Home > Mobile >  trying to reverse string without string function strrev but not working in c
trying to reverse string without string function strrev but not working in c

Time:04-23

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

void main(void)
{
    char in[15], rev[15];

    printf("Enter a word (upto 15 letters): ");
    gets(in);
    
    for (int i = 0, j = 15; i < strlen(in); i  , j--)
    {
        rev[i] = in[j];
    }
    puts(rev);
}

Shows no error, just not working. What am I doing wrong?

Edit : no strrev

CodePudding user response:

For starters according to the C Standard the function main without parameters shall be declared like

int main( void )

The function gets is unsafe and is not supported by the C Standard. Instead use either scanf or fgets.

The function strlen is a standard C string function. So according to the requirement you may not use it.

You are not reversing a string. You are trying to copy a string in the reverse order into another string.

The program can look the following way

#include <stdio.h>

int main(void)
{
    enum { N = 15 };
    char in[N] = "", rev[N];

    printf("Enter a word (upto %d letters): ", N - 1 );
    scanf( " s", in );

    size_t n = 0;
    while ( in[n] )   n;

    rev[n] = '\0';

    for ( size_t i = 0; i < n; i   )
    {
        rev[n - i - 1] = in[i];
    }

    puts( rev );
}

If you actually need to reverse a string in place then the program can look the following way

#include <stdio.h>

int main(void)
{
    enum { N = 15 };
    char in[N] = "";

    printf("Enter a word (upto %d letters): ", N - 1 );
    scanf( " s", in );

    size_t n = 0;
    while ( in[n] )   n;

    for ( size_t i = 0; i < n / 2; i   )
    {
        char c = in[i];
        in[i] = in[n - i - 1]; 
        in[n - i - 1] = c;
    }

    puts( in );
}

CodePudding user response:

EDIT: getline is not standard C, and it is only recognized by POSIX systems. Another solution is to use fgets that works for both OSes. I provided both examples.

As others have already pointed out, you are making some mistakes:

  • Unsafe practice when getting input from the user.
  • Always starting from 15 even if the input string has less chars.

I have created a little example with dynamic allocation that works with more than 15 characters and fixes the afore-mentioned issues. Comments inline to key points.

Example: getline - POSIX

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

int main (int argc, char *argv[]) {

    // Idea from https://stackoverflow.com/questions/7709452/how-to-read-string-from-keyboard-using-c
    char *line = NULL;  /* forces getline to allocate with malloc */
    size_t len = 0;     /* ignored when line = NULL */
    ssize_t read;

    read = getline(&line, &len, stdin);

    if (read > 0)
    {
        printf ("\n  String from user: %s\n",  line);
    }else
    {
        printf ("Nothing read.. \n");
        return -1;
    }    

    // Now we need the same amount of byte to hold the reversed string
    char* rev_line = (char*)malloc(read);
    
    // "read-1" because we start counting from 0. 
    for (int i = 0, j = read-1; i < read; i  , j--)
    {
        rev_line[i] = line[j];
    }
    printf("%s\n",rev_line);

    free (line);  /* free memory allocated by getline */
    free(rev_line);

    return 0;
}

Example: fgets - C standard

fgets does not return the number of characters read, so it has to be chained with strlen to decide how many characters to allocate for the reversed string.

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

int main (int argc, char *argv[]) {

    char line[LINE_MAX];
    size_t len = 0;     /* ignored when line = NULL */
    ssize_t read;


    if (fgets(line, LINE_MAX, stdin) != NULL)
    {
        line[strcspn(line, "\n")] = '\0'; //fgets() reads the \n character (that's when you press Enter). 
        read = strlen(line);
        printf ("\n  String from user: %s\n",  line);
    }else
    {
        printf ("Nothing read.. \n");
        return -1;
    }    

    // Now we need the same amount of byte to hold the reversed string
    char* rev_line = (char*)malloc(read);

    for (int i = 0, j = read-1; i < read; i  , j--)
    {
        rev_line[i] = line[j];
    }
    printf("%s\n",rev_line);

    free(rev_line);

    return 0;
}
  • Related