Hello,
I'm quite new to c programming and I'm trying to solve this exercise
Write a function that reverses a string e.g world to dlrow
.
Use this prototyp
/* Returns a reversed copy of s. */
Code:
char *reverse(char *s);
The function should reverse a copy and not the original string. Use
strdup to to copy the given string.
Use
strlen to determine the length of the string
I am able to use strlen and also strdup separately but I have no clue how I should combine the two commands to help me with the task. I would be really grateful for any help which could point me into the right direction.
What I have so far:
Code:
#include <stdio.h>
#include <string.h>
char *reverse (char *s);
int main(int argc, char* argv[])
{
char word[999] = {0};
int result = 0;
printf("Please enter a word:");
scanf("%s", &word);
reverse (&word);
return 0;
}
char *reverse (char *s)
{
int i = 0;
int length = 0;
char *copy;
copy = NULL;
length = strlen (s);
copy = strdup(s);
printf ("%s\n", copy); /*test*/
}