I'm working through an example from Kochan C programming and stuck on this exercise.
The exercise has us writing a function that will display the n'th charater from a string, for an specific amount of characters after the n'th character. So i've built this and it works when in the for loop I don't have (string[i]='\0'), but as soon as I have that in the for loop, the remain of the string is return and not just the # of character asked for. The reason I added that (string[i]='\0'), is that if the number of characters asked for exceed the end of the string, only what is avalible is to be returned.
Code:
#include <stdio.h>
// Prototype Declaration
void sub(const char string[],int start, int count, char answer[]);
int main (void){
char string[]="character";
char answer[80]="'\0'";
int start=5;
int count=2;
sub(string,start,count,answer);
printf("%s",answer);
return 0;
}
void sub(const char string[],int start, int count, char answer[]){
int j= 0;
for(int i=start; ((i < (start+count)) || (string[i]!='\0')); ++i){
answer[j]=string[i];
j++;
}
}
Why does it not work, when I add (string[i]!='\0')?