【问题标题】:Printing the digits present in the string打印字符串中存在的数字
【发布时间】:2018-09-24 18:09:15
【问题描述】:

打印字符串中的数字。

假设我有这样的输入

Input: {1,32,33,41,59}

输出应该是这样的

Output: 1 32 33 41 59

我的代码是

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char input[200],words[10][10];
int length=0,i=0,j=0,k=0,t;

fgets(input,200,stdin);


//counting the length of str
while(input[length] != '\0')
{
    length++;
}

for(i=1;i<=length;i++)
{
   if(input[i] == ',' || input[i] == '}')
        {
            words[j][k] = '\0';
            j++;
            k=0;
        }
    else
        {
            words[j][k] = input[i];
            k++;
        }
}
int temp[j];
for(i=0;i<j-1;i++)
{
    temp[i] = atoi(words[i]);
    printf("%d\n",temp[i]);
}

return 0;
}

我的代码只打印字符串中的第一个数字。我不知道为什么。

【问题讨论】:

  • 索引从 0 开始。
  • 我从1开始消除'{'
  • i&lt;j-1 在打印时应该是 i&lt;j
  • "我的代码只打印字符串中的第一个数字。" Yout 语句不正确。它只是不打印输入的 last 编号。

标签: c string loops for-loop integer


【解决方案1】:

用 j 代替 j-1

#include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    int main()
    {
    char input[200],words[10][10];
    int length=0,i=0,j=0,k=0,t;

    fgets(input,200,stdin);


    //counting the length of str
    while(input[length] != '\0')
    {
        length++;
    }

    for(i=1;i<=length;i++)
    {
       if(input[i] == ',' || input[i] == '}')
            {
                words[j][k] = '\0';
                j++;
                k=0;
            }
        else
            {
                words[j][k] = input[i];
                k++;
            }
    }
    int temp[j];
    for(i=0;i<j;i++)
    {
        temp[i] = atoi(words[i]);
        printf("%d\n",temp[i]);
    }

    return 0;
    }

【讨论】:

    【解决方案2】:

    我对您的代码进行了一些编辑,并相信我可以按照您想要的方式运行它。我评论了这些变化,请看下面。

     #include<stdio.h>
     #include<string.h>
     #include<stdlib.h>
     int main()
     {
         char input[200],words[10][10];
         int length=0,i=0,j=0,k=0,t;
    
         fgets(input,200,stdin);
    
    
         while(input[length] != '\0')
         {   
             length++;
         }   
    
         for(i=1;i<=length;i++)
         {   
             if(input[i] == ',' || input[i] == '}')
             {
                 words[j][k] = '\0';
                 j++;
                 k=0;
             }
             else
             {
                 words[j][k] = input[i];
                 k++;
             }
         }   
         int temp[j];
         //Iterate through all elements in the array
         //0 ---> j-1 is j elements 
         for(i=0;i < j ;i++)
         {   
             temp[i] = atoi(words[i]);
             //print on the same line
             printf("%d ",temp[i]);
         }   
         //newline here
         printf("\n");
    
         return 0;
     }
    

    【讨论】:

      【解决方案3】:

      您的方法过于复杂,因此存在错误。

      没有必要定义一个字符二维数组来输出存储在字符串中的数字。从一开始就使用标准函数就足够了,例如strtoull

      你来了。

      #include <stdio.h>
      #include <stdlib.h>
      #include <ctype.h>
      
      int main(void) 
      {
          enum { N = 200 };
          char input[N];
      
          while ( 1 )
          {
              printf( "Input: " );
      
              if ( !fgets( input, sizeof( input ), stdin ) || input[0] == '\n') break;
      
              for ( char *p = input; *p; )
              {
                  if ( isdigit( ( unsigned char )*p ) )
                  {
                      char *endptr;
      
                      unsigned long long int num = strtoull( p, &endptr, 10 );
                      printf( "%llu\n", num );
      
                      p = endptr;
                  }
                  else
                  {
                      ++p;
                  }
              }
          }
      
          return 0;
      }
      

      程序输出可能看起来像

      Input: {1,32,33,41,59}
      1
      32
      33
      41
      59
      Input: 
      

      【讨论】:

      • @SyedAli 例如在 SO.:)
      • 什么? ;) 我找不到你
      • @SyedAli SO 是 StackOverflow。
      • 你在开玩笑 ;) 有没有办法像谷歌视频群聊一样联系你?所以我可以用c提出我的疑问
      • @SyedAli 你可以在这里提问。有很多合格的程序员。
      【解决方案4】:

      我建议你这样做:

      #include<stdio.h>
      #include<string.h>
      #include<stdlib.h>
      const char *const s = ",";
      int main()
      {
          char input[200], *ptri, *ptr;
      
          fgets(input, 200, stdin);
      
          ptr = input;
          while (*ptr != '{' && *ptr != '\0')
          ptr++;
          if (*ptr == '\0') {
          return -1;      /* not found */
          }
          ptr++;
          ptri = ptr;
          while (*ptr != '}' && *ptr != '\0')
          ptr++;
          if (*ptr == '\0') {
          return -1;      /* not found */
          }
          *ptr = '\0';
      
          ptr = strtok(ptri, s);
      
          while (ptr != NULL) {
          printf("%s ", ptr);
          ptr = strtok(NULL, s);
          }
          printf("\n");
      
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2020-03-13
        • 2018-10-16
        • 2021-06-08
        • 2019-07-16
        • 2021-12-27
        • 1970-01-01
        • 2021-09-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多