C 字符串不';不能正常工作

C 字符串不';不能正常工作,c,string,token,strtok,strlen,C,String,Token,Strtok,Strlen,关于C,我还有一个问题:(我想用这个函数检查令牌中的数字,如果没有数字,我会将该令牌放入字符串中,稍后打印到文件中。我的函数如下所示: const char*tarpas=“” 我的结果文件: asdasd \ rytoj gimimo rytas ( 它应该是什么: suraitytas o rytoj gimimo rytas asdasdasd asdasd 感谢您的任何输入!!!您必须在while循环的每次迭代中重置找到的 此外,还必须退出循环 for(i = 0; i

关于C,我还有一个问题:(我想用这个函数检查令牌中的数字,如果没有数字,我会将该令牌放入字符串中,稍后打印到文件中。我的函数如下所示: const char*tarpas=“”

我的结果文件:

asdasd \
  rytoj gimimo rytas
 (
它应该是什么:

suraitytas o rytoj gimimo rytas
asdasdasd
asdasd

感谢您的任何输入!!!

您必须在while循环的每次迭代中重置找到的

此外,还必须退出循环

    for(i = 0; i < strlen(token); i++){
        if((token[i] >= '0') && (token[i] <= '9')) found++; //If found a number
    }   

在while循环之前或最初将数组B初始化为零之前,在循环中,您永远不会将
found
重置为零

while(token != NULL){
    int found = 0;
    /* ... */
由于您在第一个标记中找到一个数字,这意味着您永远不会执行
if(find==0)
code

这反过来意味着,当您打印
B
时,它仍然是未初始化的,并且您正在打印一些随机数据

您应该初始化
B

char B[255] = {0};

found = 0;
作为循环的第一行。
或者,由于在循环外部没有使用
found
,请将其移动到循环内部

while(token != NULL){
    int found = 0;
    /* ... */

您忘了初始化while循环中找到的变量。正如@BLUEPIXY所提到的,B数组需要以“\0”结尾。 所以代码如下

{
const char *space = " ";
char *token, B[255];
int i, length, found = 0, x = 0;
token = strtok(A, space);
while(token != NULL){
    found = 0;
    for(i = 0; i < strlen(token); i++){
        if((token[i] >= '0') && (token[i] <= '9')) found++; //If found a number
    }                                                       //increase found
    if(found == 0){                            //if found = 0 means the word is clean
        for(i = 0; i < strlen(token); i++){
            B[x] = token[i];
            x++;
        }
        B[x] = ' ';                //adds a space after the token is in string
        x++;          
    }
    rado = 0;
    token = strtok(NULL, tarpas); // get another token
}
B[x] = '\0';
print(B);
memset(B, 0, strlen(B));          //clear B string
}
{
常量字符*空格=”;
char*token,B[255];
int i,长度,found=0,x=0;
令牌=strtok(A,空间);
while(令牌!=NULL){
发现=0;
对于(i=0;i如果((令牌[i]>='0')&((令牌[i]使用“isdigit”而不是您的一对条件。使用(memset B,0,sizeof B)而不是扫描额外的时间-并在使用前初始化B。“rado”是什么意思是吗?我建议将strtok调用放入单个for循环中,而不是分隔它们。在不同的strtok调用中使用不同分隔符指针的原因是什么?@mpez0
isdigit(token[I])
是未定义的行为,如果
token[I]<0
而不是
EOF
(token[I]>='0')&(token[I]@chux my man pages说isdigit是为任何未签名字符或EOF定义的。“char”默认为已签名还是未签名取决于实现——我同意将B定义为未签名字符会更好。感谢您和@molbdnilo为我澄清:)我已经设法解决了部分问题,现在当我将字符串打印到屏幕上时,第二行和第三行被一个空格挤了出来,结果文件似乎仍然乱七八糟,有什么想法吗?@Dominykas Ber从您显示的代码中很难说还有什么问题。请尝试用更新的代码问一个新问题。非常感谢您我的朋友,你们都帮了大忙:)我已经解决了空白问题,现在剩下的就是printing@DominykasBer由于您的问题中没有函数打印的代码,因此我建议您提出一个与函数打印相关的新问题。但在提出问题之前,您应至少使用在控制台上输出其参数的函数puts检查字符串。
found = 0;
while(token != NULL){
    int found = 0;
    /* ... */
{
const char *space = " ";
char *token, B[255];
int i, length, found = 0, x = 0;
token = strtok(A, space);
while(token != NULL){
    found = 0;
    for(i = 0; i < strlen(token); i++){
        if((token[i] >= '0') && (token[i] <= '9')) found++; //If found a number
    }                                                       //increase found
    if(found == 0){                            //if found = 0 means the word is clean
        for(i = 0; i < strlen(token); i++){
            B[x] = token[i];
            x++;
        }
        B[x] = ' ';                //adds a space after the token is in string
        x++;          
    }
    rado = 0;
    token = strtok(NULL, tarpas); // get another token
}
B[x] = '\0';
print(B);
memset(B, 0, strlen(B));          //clear B string
}