【问题标题】:Loop with simple counter malfunctioning?带有简单计数器故障的循环?
【发布时间】:2014-12-01 23:17:42
【问题描述】:

我有一个程序,它接受一个字符数组并调用函数转换。该函数确定字符是字母还是数字。该程序应该输出它在字符串中找到的第一个字母。以及它在字符串中找到的第一个数字。我的循环在发现一个字母不起作用后停止寻找字母。

有什么想法吗? 代码是使用 Borland 编译器用 C 语言编写的。

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int convert (char array[],char **);

int main()
{
    int intval; 
    char array[512], *charptr;

    printf("Input a string that starts with a series of decimal digits:\n>");
    while ( gets( array ) != NULL ){
        intval = convert(array, &charptr );
        printf ("Intval contains %d, Charptr contains '%s'\n", intval, charptr);
    }
    system("pause");

    return 0;


}
int convert (char array[],char ** charptr)
{
    int i, x, c = 0;
    char b[512];
    for (i=0;i<strlen(array);i++){

        if (isalpha(array[i]))
        {
            if(c >= 1){ 
                *charptr = &array[i];
                c++;
                }
            else 
                break;


        }
        else if ( isdigit(array[i]))
                x = 10*x + array[i] - '0';

     }

    return  x;
}

更新:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>


int convert (char array[],char ** charptr);



int main()
{
    int intval; 
    char array[512], *charptr;

    printf("Input a string that starts with a series of decimal digits:\n>");
    while ( gets( array ) != NULL ){
        intval = convert(array, &charptr );
        printf ("Intval contains %d, Charptr contains '%s'\n", intval, charptr);
    }
    system("pause");

    return 0;


}
int convert (char array[],char ** charptr)
{
    int i, x, c;
    char b[512];
   for (i=0;array[i] != 0;i++){
        if ( isdigit(array[i]))

                x = 10*x + array[i] - '0';
        else if (isalpha(array[i]))
        {
            c++;
            if(c >= 1){ 
                *charptr = &array[i];

            }  
        }

    }

    return  x;
}

【问题讨论】:

  • 您是否使用调试器单步执行了代码?
  • 提示:if(c &gt;= 1){ 绝不是真的。
  • 1. charptr 应该在使用前分配 2. 这很糟糕: for (i=0;i
  • 您可以使用scanf() 更有效地编写此代码。永远不要使用gets()

标签: c string loops if-statement for-loop


【解决方案1】:

你有一个逻辑错误。 c 被初始化为 0。有一行要增加 c,但它位于永远不会为真的 if 块内。

        if(c >= 1){ 
            *charptr = &array[i];
            c++;
            }

第 22 条渔获???

也许你打算使用:

int convert (char array[],char ** charptr)
{
    int i, x, c = 0;
    char b[512];
    for (i=0;i<strlen(array);i++){

        if (isalpha(array[i]))
        {
                // No need for checking the value of c
                // return as soon you find an alphabet.
                *charptr = &array[i];
                break;    
        }
        else if ( isdigit(array[i]))
                // If you are looking for an alphabet only,
                // why do you have this block of code???
                x = 10*x + array[i] - '0';
    }

    return  x;
}

更新

也许,这就是你要找的。​​p>

int convert (char array[], char ** charptr)
{
   size_t i;
   int x = 0;
   size_t len = strlen(array);

   // Set charptr to NULL in case there are no letters in the input.
   *charptr = NULL;
   for (i=0;i<len;i++){

      if ( isalpha(array[i]))
      {
         *charptr = &array[i];
         return x;
      }
      else if ( isdigit(array[i]))
      {
         x = 10*x + array[i] - '0';
      }
   }

   return x;
}

【讨论】:

  • @R Sahu 我需要第二个代码块来打印它找到的第一个数字。例如,用户输入 274ADAMS 它会打印前 3 个数字,然后是第一个字母“A”。
  • @R Sahu 为你做了这项工作吗?我尝试使用此代码,但在第一个字母之后它仍然没有停止。
  • 它会在找到一个字母和一个数字后停止。如果你想在第一个字母之后停止,不管你是否找到数字,需要修改函数。
  • @R Sahu 无论是否找到数字,我如何将其修改为停止?
  • @R Sahu 还是不行。也许我的主要功能有问题?
【解决方案2】:
int scanString(char array[],char * charptr)
{
    int len = strlen(array);
    int digs = 0;
    int x = 0;
    *charptr = 0; 
    for (int i=0;i<len;i++){

        if (charptr == 0 && isalpha(array[i]))
        {
                *charptr = array[i];
        }
        else if (digs == 0 && isdigit(array[i])){

                x = array[i] - '0';
               digs = 1;
       }
       if(digs > 0 && charptr != 0)
          break;
    }

    return  x;
}

规范说返回找到的第一个字符,因此更改了 charptr。

【讨论】:

  • 这需要在开头设置'x'和'*charptr'来表示在字符串中没有找到相关项。因此,如果它不在字符串中,调用者将知道该项目未找到。如果我这样做,我会为 4 个可能的返回条件生成一个枚举,将另一个 ptr 传递给一个数字,然后相应地继续
  • 完成 - 我猜这就是为什么他乘以 10 - 区分找到和 0 = 未找到
猜你喜欢
  • 2015-08-12
  • 2021-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-15
  • 2017-11-03
  • 1970-01-01
  • 2023-03-31
相关资源
最近更新 更多