1

I want to write a function void reverse_string(char* s) that reverses in place the contents of the null-terminated C string given to it as parameter.

So I got it to reverse the content with no parameter. But I want to know how to implement the parameter into this like from the command line.

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

void reverseString(char* str);
int main()
{
    char str [256];
    strcpy_s(str, "Hello World");
    printf(str);
    reverseString(str);
    return 0;
}
void reverse_String(char* str)
{
    int i, j;
    char temp;
    i=j=temp=0;

    j=strlen(str)-1;
    for (i=0; i<j; i++, j--)
    {
        temp=str[i];
        str[i]=str[j];
        str[j]=temp;
    }
    printf(str);
}

Any help is appreciated.

Stanislav Pankevich
  • 11,044
  • 8
  • 69
  • 129
shawn edward
  • 149
  • 7

2 Answers2

2

Do it like this:

int main ( int argc, char **argv ) {
     char str [256];
     if(argc > 1)
       strcpy(str, argv[1]);
     else
       printf("no cmd given\n");
     ...
     return 0;
}

However, your code shouldn't compile as you post it...Here is something to start with:

gsamaras@gsamaras:~$ gcc -Wall px.c 
px.c: In function ‘main’:
px.c:8:5: warning: implicit declaration of function ‘strcpy_s’ [-Wimplicit-function-declaration]
     strcpy_s(str, "Hello World");
     ^
px.c:9:5: warning: format not a string literal and no format arguments [-Wformat-security]
     printf(str);
     ^
px.c: In function ‘reverse_String’:
px.c:19:14: error: ‘str’ undeclared (first use in this function)
     j=strlen(str)-1;
              ^
px.c:19:14: note: each undeclared identifier is reported only once for each function it appears in
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • I fixed some of those errors in the original question. My parameter in the function were "char* s" changed it to "char* str" – shawn edward Mar 27 '16 at 18:45
  • OK good @shawnedward, check the other warnings too. As for the cmd handling, my answer should suffice. :) – gsamaras Mar 27 '16 at 18:46
  • Is `strcpy_s()` invoked as you show in your example? All the documetation I see on it shows it with three arguments (i.e. a limit in the middle.) I'm not familiar with this function which is why I'm asking. – cdlane Mar 27 '16 at 19:02
1
void reverse_String(char* string);

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

    if (argc > 1 && strlen(argv[1]) < 1024) {
        strcpy(string, argv[1]);
        printf("%s\n", string);
        reverse_String(string);
        printf("%s\n", string);
    } else {
        // appropriate error message to stderr and then:
        return 1;
    }

    return 0;
}

void reverse_String(char *string)
{
    // implement reversal but don't print it, leave that to main()
}
cdlane
  • 40,441
  • 5
  • 32
  • 81
  • One question. Why did you initial the function at the top and then at the bottom you have it again. Could you just have the function at the bottom without initializing it at the top? – shawn edward Mar 27 '16 at 18:53
  • The first invocation is a forward declaration to let `main()` know the signature (return type and parameters) of `reverse_String()`. You could leave it out, but then you'd need to reverse the order of the two functions so that `reverse_String()` is defined before `main()` references it. Your original code attempts to do this as well, but doesn't use a consistent function name. – cdlane Mar 27 '16 at 18:57