C 简单计数器故障的循环?

C 简单计数器故障的循环?,c,string,loops,if-statement,for-loop,C,String,Loops,If Statement,For Loop,我有一个程序,它接受一个字符数组并调用convert函数。该函数确定字符是字母还是数字。程序应该输出它在字符串中找到的第一个字母。以及它在字符串中找到的第一个数字。我的循环在发现一封信不起作用后停止寻找 有什么想法吗? 代码是使用Borland编译器用C编写的 #include <stdio.h> #include <math.h> #include <stdlib.h> #include <ctype.h> #include <string

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

有什么想法吗? 代码是使用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;
}
#包括
#包括
#包括
#包括
#包括
int转换(字符数组[],字符**);
int main()
{
int intval;
char数组[512],*charptr;
printf(“输入以一系列十进制数字开头的字符串:\n>”;
while(获取(数组)!=NULL){
intval=convert(数组和charptr);
printf(“Intval包含%d,Charptr包含“%s”\n”,Intval,Charptr);
}
系统(“暂停”);
返回0;
}
int转换(字符数组[],字符**charptr)
{
int i,x,c=0;
字符b[512];
对于(i=0;i=1){
*charptr=&数组[i];
C++;
}
其他的
打破
}
else if(isdigit(数组[i]))
x=10*x+阵列[i]-“0”;
}
返回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;
}
#包括
#包括
#包括
#包括
#包括
int转换(字符数组[],字符**charptr);
int main()
{
int intval;
char数组[512],*charptr;
printf(“输入以一系列十进制数字开头的字符串:\n>”;
while(获取(数组)!=NULL){
intval=convert(数组和charptr);
printf(“Intval包含%d,Charptr包含“%s”\n”,Intval,Charptr);
}
系统(“暂停”);
返回0;
}
int转换(字符数组[],字符**charptr)
{
int i,x,c;
字符b[512];
对于(i=0;数组[i]!=0;i++){
if(isdigit(数组[i]))
x=10*x+阵列[i]-“0”;
else if(isalpha(数组[i]))
{
C++;
如果(c>=1){
*charptr=&数组[i];
}  
}
}
返回x;
}

您有一个逻辑错误<代码>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;
}
int转换(字符数组[],字符**charptr)
{
int i,x,c=0;
字符b[512];
对于(i=0;i
int扫描字符串(char数组[],char*charptr)
{
int len=strlen(数组);
int digs=0;
int x=0;
*charptr=0;
对于(int i=0;i 0&&charptr!=0)
打破
}
返回x;
}

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

您是否使用调试器对代码进行了单步调试?提示:
如果(c>=1){
从来都不是真的。1.charptr应该在使用前进行分配2.这不好:对于(i=0;i)您可以使用
scanf()
更有效地编写此代码。永远不要使用
get()
@R Sahu我需要第二块代码来打印它找到的第一个数字。例如,如果用户输入它,它将打印前3个数字,然后打印第一个字母“A”。@R Sahu为您完成了这项工作?我尝试使用此代码,但在第一个字母后它仍然不会停止。它将在找到字母和数字后停止。如果您ant要在第一个字母后停止,无论您是否找到数字,该函数都需要修改。@R Sahu如何将其修改为停止,无论它是否找到数字?@R Sahu它仍然不工作。可能我的主函数中有错误?这需要在开始时设置“x”和“*charptr”以指示在字符串中找不到e相关项。因此,如果该项不在字符串中,调用方将知道该项未找到。如果我正在这样做,我将为4个可能的返回条件生成一个枚举,将另一个ptr传递到一个数字,然后按照DONE进行操作-我想这就是他乘以10的原因-以区分已找到和0=未找到
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;
}
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;
}