从街道地址解析门牌号码表示为char []

时间:2015-04-15 03:21:47

标签: c arrays char

假设我将街道地址存储为char []。有效值的示例:

  • 1600 Pennsylvania Ave
  • 1无限循环
  • 221 Baker Street

如您所见,门牌号可以是任意长度。

在C中,将门牌号分成自己的int的有效方法是什么?我想我需要编写一个while循环来检查每个char c if isdigit(c),但我不知道我是否在实现方面走在正确的轨道上。

4 个答案:

答案 0 :(得分:0)

您可以使用strtok将字符串分解为标记,并使用isdigit()来确定该标记是否为数字。

#include <string.h>
#include <stdio.h>

int main()
{
   const char str1[80] = "1600 Pennsylvania Ave";
   int houseNumber = 0;
   const char s[2] = " ";
   char *token;

   /* get the first token */
   token = strtok(str, s);

   /* walk through other tokens */
   while( token != NULL ) {
      printf( " %s\n", token );
      if (isdigit(str1[0])) {
           houseNumber = atoi (token);
      }
      token = strtok(NULL, s);
   }

   return(0);
}

或者,您可以使用sscanf读取整个字符串并自动解析所有内容:

#include <string.h>
#include <stdio.h>

int main()
{
   const char str1[80] = "1600 Pennsylvania Ave";
   int houseNumber = 0;
   char streetName[50];
   char streetExt[20]; 

   sscanf (str1,"%d %s %s", &houseNumber, streetName, streetExt);

   return(0);
}

最后一种方法取决于字符串的格式在所有情况下都完全相同,这意味着它始终是一个数字后跟2个字符串。如果有更多其他东西,strtok方法将更容错。

答案 1 :(得分:0)

考虑以下解决方案:

#include <stdio.h>
#include <string.h>
int main(void)
{
    char address[255];
    char * ptr;
    int number;
    char strnumber[100];
    char strstreet[255];
    // ask adress
    printf("Please, enter the addres: ");
    // put end of string replacing newline
    fgets(address, 255, stdin);
    ptr = strchr(address, '\n');
    if(ptr)
    {
        *ptr = '\0';
    }
    else
    {
        address[254] = '\0';
    }

    // try to read whole number from the beggining of the string
    if( 1 == sscanf(address, "%d", &number) )
    {
        // make number as a string (if it is needed)
        sprintf(strnumber, "%d", number);
        // take streat to separate string
        ptr = strchr(address + strlen(strnumber), ' '); // find the firs space
        if( ptr )
        {
            strcpy(strstreet, ptr + 1); // +1 just to skip the found space
        }
    }
    else
    {   // if no number at the beginning of address string
        number = 0;
        strnumber[0] = '\0';
        strstreet[0] = '\0';
    }
    // show the results
    printf("You have entered a string:\n%s\n", address);
    printf("The found number is:\n%d\n", number);
    printf("The found number as a string:\n%s\n", strnumber);
    printf("The address without number is:\n%s\n", strstreet);
}

答案 2 :(得分:0)

我会像这样使用strstr和atoi

char someAddress[80] = "1600 Pennsylvania Ave";
char* p = strstr(someAddress, " ");
if (p)
{
    *p = 0;    // Terminate string, i.e. cheat for a moment
    int number = atoi(someAddress);
    *p = " ";  // Restore someAddress
}
else
{
     // Handle illegal format in someAddress
}

只有当您知道在短时间内修改someAddress是安全的时,才能使用此方法。

答案 3 :(得分:0)

这是数字优先的一种情况,只需使用简单的指针和if语句来解析该行就更容易了:

#include <stdio.h>

#define ADDL 64

int main (void) {

    char address[ADDL] = {0};
    char street[16] = {0};

    while (fgets (address, ADDL-1, stdin) != NULL) 
    {
        char *ap = address;
        char *sp = street;

        while (*ap >= '0' && *ap <= '9')  /* while the char is a number */
        {
            *sp = *ap++;                  /* copy to street number      */
            sp++;
        }
        *sp = 0;                          /* null-terminate             */

        printf (" Address: %s Number : %s\n\n", address, street);
    }

    return 0;
}

<强>输出

$ ./bin/split_address < dat/houses.txt
 Address: 1600 Pennsylvania Ave
 Number : 1600

 Address: 1 Infinite Loop
 Number : 1

 Address: 221 Baker Street
 Number : 221

注意:上面的printf语句用于newline末尾的嵌入式address,而不是像通常那样剥离它。同样注意如果您希望将号码设为integerlongunsigned,只需拨打atoistrtolstrtoul上的street