与えられた文字列の一部を抽出するプログラムを作っています。 しかし、それは機能のforループで何らかの問題を抱えていますextct()
; forループはa<=b
で終了していません。それだけでそれを通過し、a!='\0'
で終了します。forループはCで終了しません
int main()
{
void extct();
char my_string[50];
printf("Enter anything: ");
scanf("%[^\n]s",my_string);
extct(my_string);
getch();
return 0;
}
void extct(char *a)
{
int i,j,l;
char new_string[50];
printf("Enter location from you want to extract string: ");
scanf("%d",&i);
printf("Now enter no. of letters you want to extract from previous entered location: ");
scanf("%d",&j);
a=a+(i-1); //sets address to entered location
int b=a+j; //sets limit for the for loop
for(l=0;(a<=b)||(*a!='\0');a++,l++)
{
new_string[l]=*a;
}
new_string[l]='\0';
printf("\n%s",new_string);
}
デバッガの
&&
を使用し、その後、ループを終了したい場合は、使用するツールです。 –'true || falseは 'trueに評価されます –