2017-09-26 15 views
-1

与えられた文字列の一部を抽出するプログラムを作っています。 しかし、それは機能の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); 

} 
+1

デバッガの&&を使用し、その後、ループを終了したい場合は、使用するツールです。 –

+0

'true || falseは 'trueに評価されます –

答えて

2

あなたは条件を満たすのいずれかの場合には代わりに||

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

これはDe Morganの法則を参照するのに適した場所です。 – Barmar

+0

@Barmarそれはなぜですか? –

+0

@SamIam条件の感覚を変えているときに '?|'から '&&'に切り替える理由を理解するのに役立ちます。 – Barmar

関連する問題