Hi i trying to implement a reverse array code but it doesnt seem to work and im really not sure why. The For loop just doesnt seem to work. I dont know why because the logic seems pretty right to me.
#include <stdio.h>
#include <string.h>
void reverse(char, int);
int main()
{
char a[100];
gets(a);
reverse(a, strlen(a)-1);
printf("%s\n",a);
getchar();
getchar();
getchar();
return 0;
}
void reverse(char ar[], int n)
{
char c;
int i = 0;
printf("n = %d" , n);
for ( i = 0; i >= n ; i++){
c = ar[i];
ar[i] = ar[n];
ar[n] = c;
printf("Processed");
n--;}
}
/*
if (begin >= n)
return;
c = *(x+begin);
*(x+begin) = *(x+n);
*(x+n) = c;
offs = x++;
printf("Begin = %d , n = %d, offs = %p \n", begin, n, offs);
reverse(x, ++begin, --n); */
void reverse(char, int); //declaration wrong
void reverse(char[], int);
^^^
Your loop
for ( i = 0; i >= n ; i++) // this fails i=0, n=some size
should be
for ( i = 0; i <= n ; i++)
Avoid using gets()
use fgets()
instead.