2017-09-26 85 views
-1

我正在制作提取给定字符串的某些部分的程序。 但它在功能extct() for循环中有一些问题; 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); 

} 
+1

调试器是使用工具。 –

+0

'true ||假'评估为'真' –

回答

2

,如果你要终止,如果任何条件满足的循环,然后使用&&代替||

for(l=0;(a<=b) && (*a!='\0');a++,l++) 
+0

这是一个参考德摩根定律的好地方。 – Barmar

+0

@Barmar为什么呢? –

+0

@SamIam因为它有助于理解当你改变条件的感觉时,为什么要从'||'切换到'&&'。 – Barmar