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;
}

Recommended Answers

All 2 Replies

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.

#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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.