【问题标题】:strings don't function properly字符串不能正常工作
【发布时间】:2014-11-27 15:26:21
【问题描述】:

我有另一个关于 C 的问题 :( 我想用这个函数做的是检查令牌中的数字,如果没有,我把那个令牌放在一个字符串中,我稍后打印到文件中。我的函数看起来像这: const char *tarpas = " ";

{
    const char *space = " ";
    char *token, B[255];
    int i, length, found = 0, x = 0;
    token = strtok(A, space);
    while(token != NULL){
        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
    }
    print(B);
    memset(B, 0, strlen(B));          //clear B string
    }

我的数据文件:

ta5iip r345ytas suraitytas o rytoj gimimo rytas
asdasdasd
asdasd

我的结果文件:

asdasd \
  rytoj gimimo rytas
 (

应该是什么:

suraitytas o rytoj gimimo rytas
asdasdasd
asdasd

感谢您的任何意见!!!

【问题讨论】:

  • 使用“isdigit”代替你的一对条件。使用 (memset B, 0, sizeof B) 而不是扫描额外的时间 - 并在使用前初始化 B。 “拉多”有什么作用?我建议将 strtok 调用放入单个 for 循环中,而不是间隔它们。您在不同的 strtok 调用中使用不同的分隔符指针的原因是什么?
  • @mpez0 isdigit(token[i]) 如果token[i] &lt; 0 而不是EOF,则为未定义行为。 (token[i] &gt;= '0') &amp;&amp; (token[i] &lt;= '9')没有这个问题。
  • @chux 我的手册页说 isdigit 是为任何无符号字符或 EOF 定义的。 “char”默认为有符号还是无符号取决于实现——我同意将 B 定义为无符号字符会更好。

标签: c string token strtok strlen


【解决方案1】:

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

你也必须退出循环

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

如果在字符串中找到数字。

这个循环可以改写如下

size_t n = strlen(token);
i = 0;
while ( i < n && ( token[i] < '0' || token[i] > '9' ) ++i;
found = i != n;

您似乎还使用包含换行符的函数fgets 读取字符串。您应该从字符串中删除该字符。

并放置语句

memset(B, 0, strlen(B));          

在 while 循环之前或初始将数组 B 初始化为零

【讨论】:

  • 谢谢你和@molbdnilo 帮我解决问题 :) 我已经设法解决了部分问题,现在当我将字符串打印到屏幕上时,第二行和第三行被一个空格推动结果文件似乎仍然混乱,有什么想法吗?
  • @Dominykas Ber 从您展示的代码中很难说还有什么问题。尝试使用更新后的代码提出新问题。
  • 非常感谢我的朋友,你们都帮了大忙 :) 我已经解决了空白问题,现在剩下的就是打印了
  • @Dominykas Ber 因为您的问题中没有函数打印代码,所以我建议您提出一个与函数打印相关的新问题。但在问这个问题之前,您至少应该使用在控制台上输出其参数的函数 puts 来检查字符串。
【解决方案2】:

您永远不会在循环内将found 重置为零。

由于您在第一个标记中找到了一个数字,这意味着您永远不会执行if(found == 0) 代码。

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

你应该初始化B:

char B[255] = {0};

并添加

found = 0;

作为循环的第一行。
或者,由于您在循环外没有使用found,因此将其移到循环内。

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

【讨论】:

    【解决方案3】:

    您忘记在 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
    }
    

    【讨论】:

      猜你喜欢
      • 2016-07-14
      • 1970-01-01
      • 2020-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      相关资源
      最近更新 更多