Harry
Harry

Reputation: 13

Reversing a String in C. Why is this code not working?

I wrote a simple code to reverse a string and its somehow not returning any result. Could someone please help me understand the problem.

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

void reverse_character(char *s){
    int i;
    int p = strlen(s);
    for(i=(p-1);i<0;i--){printf("%c",s[i]);}        
}

int main(){
    char name[20];
    printf("Enter a name");
    scanf("%s",name);
   reverse_character(name);

}

Upvotes: 1

Views: 41

Answers (1)

Niloct
Niloct

Reputation: 10005

i=(p-1);i<0;i--

Change it to

i=(p-1);i>=0;i--

Upvotes: 1

Related Questions