如何在c程序

时间:2018-01-15 11:57:46

标签: c

例如,我想分离字符串" 0013subdivision"进入0013(作为一个整数,可以做加法,减法等不是char)和细分(作为char本身)在给定的字符串中。

到目前为止,这是我的代码:

#include <stdio.h>
#include <stdlib.h>

char location[10]; /* for the input with number and letter */
char x;
int house[10]; /* for the integer that will be separated from the string */
int main()
{
    printf("Input: ");
    scanf ("%s", &location[x]);

    x=0
    for (x=0; location[x]!='\0'; x++ );
    return 0;
}

根据我的研究,代码atoi用于将转换后的值转换回int(如果我没有记错),但我不知道何时放置该函数。< / p>

4 个答案:

答案 0 :(得分:1)

代码中的主要问题是

   scanf ("%s", &location[x]);

您未对扫描施加任何限制。像0013subdivision这样的输入会导致超出范围的内存访问,导致undefined behavior

对于定义为

的数组,始终使用长度修饰符限制输入大小
 char location[10]

使用转换规范,如

 scanf ("%9s", location);   // (i) one element saved for terminating null
                            // (ii) the array name decays to the pointer to 1st element
                            //in case of an argument to a function call.

然后,您不需要整数数组来存储提取的整数。 单数变量就足够了。

但是,我想建议一个非常强大的方式:

  • 使用fgets()
  • 阅读用户输入
  • 然后,使用sscanf()和相应的转换说明符扫描输入,例如%4d%9s或类似。

答案 1 :(得分:1)

locationchar array,如果您只是字符串使用%s仅使用字符串名称,则不需要index

scanf ("%s", &location[x]); - &gt; scanf ("%s", location);

仅从int中分离char array后,您需要将一个int值存储到house

int house[10] - &gt; int house

以下是仅从字符串中提取int的代码:

char location[10]; /* for the input with number and letter */
int x;
int house = 0 ; /* for the integer that will be separated from the string */
int main()
{
        printf("Input: ");
        //scanf ("%s", &location[x]);
        scanf ("%s", location);

                for (x=0; location[x]!='\0'; x++ ) {
                        if(location[x]>='0' && location[x]<='9') {
                                house =(house * 10) + (location[x]-48);
                        }
                }
                        printf("int part = %d \n",house);


        return 0;
}

答案 2 :(得分:0)

  • 最正确的方法是使用stdlib.h中的strto...系列函数。例如:

    printf("%ld\n", strtol(str, NULL, 10));
    
  • atoi系列函数绝不能用于任何目的,因为它们已经破坏了错误处理,可以被strto...函数100%替换。

  • 你可以使用scanf系列函数,但它们不必要地慢,而且非常危险,所以我真的没有看到在这里使用它们的意义。

如果您有兴趣手动实施实际复制,出于学习目的,这是相当简单的:

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

int main (void)
{
  const char str[] = "0013subdivision";
  char number_part [sizeof(str)];
  char letter_part [sizeof(str)];
  size_t i;

  for(i=0; str[i]!='\0' && isdigit(str[i]); i++) // find where the letters start
  {}

  memcpy(number_part, &str[0], i);             // copy digit part
  number_part[i] = '\0';                       // append null terminator
  memcpy(letter_part, &str[i], sizeof(str)-i); // copy letter part + null term

  puts(number_part);
  puts(letter_part);
  printf("%ld\n", strtol(str, NULL, 10));
}

如果字符串是运行时变量,则必须使用strlen(str)+1而不是sizeof()

答案 3 :(得分:0)

strtol将字符串转换为数字,并且还会返回它停止的字符,即数字后面的第一个字符。

#include <stdio.h>
#include <stdlib.h>

int main(void) {
  const char* const input = "0013subdivision";
  const char* string;
  const long number = strtol(input, &string, 10);

  printf("Number: %ld String: '%s'\n", number, string);
  // Number: 13 String: 'subdivision'
  return 0;
}

https://repl.it/repls/SphericalImpracticalTinamou