here is the main function in a program that i wrote in which i need to sort an array of characters, making the ones with an even ascii code at the beginning, and i wanna display how the array is sorted at every iteration.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main ()
{
int n, i,j;
char echange;
printf("array size : ");
scanf("%d", &n);
char t[n];
for (i=0; i<n; i++)
{
printf("enter array elements : ");
scanf(" %c", &t[i]);
}
for (j=0; j<n; j++)
for (i=0; i<n; i++)
{
if ((t[i] % 2!=0) && (t[i+1] % 2 ==0) && (i != n-1))
{
strcpy(echange, t[i]);
strcpy(t[i], t[i+1]);
strcpy(t[i+1], echange);
printf (" %c (%d)", t[i], t[i]);
}
else
printf(" %c (%d)", &t[i], t[i]);
}
}
this problem is normally compiled, but the output is so weird :
array size : 3
enter array elements : d
enter array elements : f
enter array elements : g
2 └ (100) ┴ (102) ┬ (103) └ (100) ┴ (102) ┬ (103) └ (100) ┴ (102) ┬ (103)
Process returned 0 (0x0) execution time : 4.063 s
Press any key to continue.
so what is wrong with my code? and why how to use strcpy with single characters? and by the way, i tried without the strcpy function :
echange = t[i];
t[i] = t[i+1];
t[i+1] = echange;
and it didn't work neither