0

need help with my program. it keeps repeating (NULL) instead of preinting hello backwards. if remove ca[5] i get bus error. any help please on how to fix it.

#include <stdio.h>

int main()
{
char ca[10];
ca[0] = 'H';
ca[1] = 'e'; 
ca[2] = 'l';
ca[3] = 'l';
ca[4] = 'o';
ca[5] = '\0';
printf("%s", ca[5],ca[4],ca[3],ca[2],ca[1],ca[0]);
getchar();
return 0;
}
3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Luckychap
0

ca[4] for example, is a single char, not a string.

So for each ca[x] you want to print, you need a %c in your format string.

0

#include <stdio.h>

int main()
{
char ca[10];
ca[0] = 'H';
ca[1] = 'e';
ca[2] = 'l';
ca[3] = 'l';
ca[4] = 'o';
ca[5] = '\0';

for(int i=4; i>= 0 ; i--)
printf("%c", ca);
//printf("%s", ca[5],ca[4],ca[3],ca[2],ca[1],ca[0]);
getchar();
return 0;
}

ca is a character type, you have to print them in reverse order by looping backward from ca[4] to ca[0].

need help with my program. it keeps repeating (NULL) instead of preinting hello backwards. if remove ca[5] i get bus error. any help please on how to fix it.

#include <stdio.h>

int main()
{
char ca[10];
ca[0] = 'H';
ca[1] = 'e'; 
ca[2] = 'l';
ca[3] = 'l';
ca[4] = 'o';
ca[5] = '\0';
printf("%s", ca[5],ca[4],ca[3],ca[2],ca[1],ca[0]);
getchar();
return 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.