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

时间:2014-12-01 23:17:43

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

我有一个程序,它接受一个char数组并调用函数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;
}

更新:

#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;
}

2 个答案:

答案 0 :(得分:0)

您有逻辑错误。 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 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;
}

答案 1 :(得分:0)

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。