Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char* reverseString(char* s)
- {
- /* Sample input: "Hello"
- Sample output: "olleh"
- */
- char * reversed_string;
- char temp[1];
- int length = 0;
- int i;
- if(s == NULL)
- return NULL;
- length = strlen(s);
- /* While string is not null, increment pointer */
- while(*s != NULL)
- {
- s = s + 1;
- }
- /* Allocate reversed string based off length of original string */
- reversed_string = malloc(length + 1);
- /* Traverse backwards for length of string */
- /* Copy each letter to temp */
- /* Concatenate each letter to reversed_string */
- for(i = 0; i < length; i++)
- {
- s = s - 1;
- strncpy(temp, s, 1);
- strcat(reversed_string, temp);
- }
- reversed_string[length] = '