Home > database >  Reversing a string using pointers and my own functions
Reversing a string using pointers and my own functions

Time:12-29

I have an assignment and I am new to C. I will attach the assignment and my code. Basically, the issue is that my program doesn't work and I have no idea what's wrong.

Solve the following problem:

  • with pointers (pointer arithmetics instead of array[i]), but using your own user-defined functions

Write a program that reverses a string (array of char) entered by the user.

#include <stdio.h>
#include <conio.h>
void reversed_string(char *s,int nr)
{
    int i;
    for(i=nr; i>=0; i--)
    printf("%c",*(s i));
}
int main()
{
    char *s;
    int nr;
    gets(s);
    nr=strlen(s);
    reversed_string(s,nr);
    return 0;
}

CodePudding user response:

  1. You read outside the string bounds.
  2. Use the correct type for sizes (size_t)
  3. Name functions non-confusing way. Your function is printing string in reverse order, not reversing the string.
  4. You use not initialized pointer
  5. Do not use gets
void print_reversed(const char *str, size_t nr)
{
    if(str && *str)
        do{
            putchar(*(str   nr -1));
        }while(nr--);
}

int main()
{
    char *s = "Hello World!";
    size_t nr;
    nr=strlen(s);
    print_reversed(s,nr);
    return 0;
}

If you want to reverse the string:

char *reverseString(char *str)
{
    char *end = str, *wrk = str;
    if(str && *str)
    {
        while(*(end   1)) end  ;
        while(end > wrk)
        {
            char temp = *wrk;
            *wrk   = *end;
            *end-- = temp;
        }
    }
    return str;
}



int main()
{
    char s[] = "Hello World!";

    printf("Reversed string: `%s`\n", reverseString(s));
}

CodePudding user response:

For starters the pointer s is uninitialized and has an indeterminate value

char *s;

So the call of the function gets that is unsafe (and the function is not supported by the C Standard)

gets(s);

invokes undefined behavior.

Instead you could write in main for example

char s[100];

fgets( s, sizeof( s ), stdin );

s[ strcspn( s, "\n" ) ] = '\0';

As for the function then take into account that to reverse a string does not mean to output a string in the reverse order. It means to change the string itself.

The function should be declared and defined the following way

char * reversed_string( char *s )
{
    if ( *s )
    {
        for ( char *first = s, *last = s   strlen( s ); first < --last;   first )
        {
            char c = *first;
            *first = *last;
            *last = c;
        }
    }

    return s;
}

And in main the function is called like

puts( reversed_string( s ) );

Here is a demonstration program.

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

char * reversed_string( char *s )
{
    if ( *s )
    {
        for (char *first = s, *last = s   strlen( s ); first < --last;   first)
        {
            char c = *first;
            *first = *last;
            *last = c;
        }
    }

    return s;
}

int main( void )
{
    enum { N = 100 };
    char s[N];

    printf( "Enter a string: " );

    fgets( s, sizeof( s ), stdin );

    s[strcspn( s, "\n" )] = '\0';

    puts( reversed_string( s ) );
}

If you may not use the standard string function strlen then the function reversed_string can look the following way

char * reversed_string( char *s )
{
    if ( *s )
    {
        char *last = s;

        while ( *last )   last;

        for (char *first = s; first < --last;   first)
        {
            char c = *first;
            *first = *last;
            *last = c;
        }
    }

    return s;
}
  • Related