我正在制作程序,它提取给定字符串的某些部分。
但它在函数extct()
中的for循环中有一些问题; for循环不会由a<=b
终止。它只是通过它并终止于a!='\0'
。
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);
}
答案 0 :(得分:2)
如果您想在任何条件满足时终止循环,则使用&&
代替||
for(l=0;(a<=b) && (*a!='\0');a++,l++)