01/10/2018, 09:57
Vòng lặp infinite sau khi sử dụng strtoke
Các bạn xem giúp mình code này không biết sao nó chạy tới vòng lặp cuối cùng lại đứng
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int is_digit(char);
int main ()
{
printf("Assignment #1-3, Huy Nguyen, huynguyen2412@gmail.com
");
int is_digit(char *a){
int isDigit = 0;
int i = 0;
while (i < strlen(a) && isDigit == 0){
if (a[i] >= '0' && a[i] <= '9')
isDigit = 0;
else
isDigit = 1;
i++;
}
return isDigit;
}
char str[256];
char *ptr;
char *ptr2;
char str2[256];
int count =0;
int index;
int j =0;
int num_space;
do{
count =0;
index = 0;
num_space = 0;
printf("> ");
fgets(str,66,stdin);
str[strlen(str)-1] = ' ';
ptr = str;
while (*ptr != ' '){
if (*ptr == ' '){
num_space++;
}
ptr++;
}
ptr = strtok(str," ");
while (ptr != NULL){
str2[index] = *ptr;
count++;
index++;
ptr = strtok(NULL," ");
}
if (count > 2 || num_space == 0){
printf("ERROR! Incorrect number of tokens found.
");
}
ptr = str;
}while(count > 2 || num_space == 0);
printf("Hello world %d
", index);
while (j < index){
printf("Hello!");
}
return (0);
}
Bài liên quan
Bạn code cái gì đấy @@@@@@@@@@
Chắc cụ này muốn xài hàm đấy mà