在c程序中如何从字符串中分离字符和数字
例如,我想在给定字符串中将字符串“0013subdivision”分成0013(作为一个可以进行加法、减法等的整数,而不是字符)和细分(作为字符本身) 这是我目前的代码:在c程序中如何从字符串中分离字符和数字,c,C,例如,我想在给定字符串中将字符串“0013subdivision”分成0013(作为一个可以进行加法、减法等的整数,而不是字符)和细分(作为字符本身) 这是我目前的代码: #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 wil
#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;
}
#包括
#包括
字符位置[10];/*用于输入数字和字母*/
字符x;
国际大厦[10];/*用于将从字符串中分离的整数*/
int main()
{
printf(“输入:”);
scanf(“%s”和位置[x]);
x=0
对于(x=0;位置[x]!='\0';x++);
返回0;
}
根据我的研究,代码
atoi
用于将转换后的值转换回int(如果我没有弄错的话),但我不知道何时放置函数。代码中的主要问题是
scanf ("%s", &location[x]);
您没有对扫描施加任何限制。类似于0013subdivision
的输入将导致越界内存访问,从而导致
对于定义为的数组,始终使用长度修饰符限制输入大小,如
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.
然后,不需要整数数组来存储提取的整数。一个单一的变量就足够了
但是,我想提出一种更为稳健的方法:
- 使用以下命令读取用户输入
- 然后,使用适当的转换说明符(如
或类似的说明符)扫描输入%4d%9s
位置
是字符数组
,如果您作为字符串读取,则仅使用%s
和字符串名称,不需要索引
scanf(“%s”和位置[x])代码>-->scanf(“%s”,位置)代码>
仅从char数组中分离int
后,需要将一个int
值存储到house
中
inthouse[10]
-->inthouse
以下是从字符串中仅提取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;
}
char位置[10];/*用于输入数字和字母*/
int x;
int house=0;/*用于将从字符串中分离的整数*/
int main()
{
printf(“输入:”);
//scanf(“%s”和位置[x]);
扫描频率(“%s”,位置);
对于(x=0;位置[x]!='\0';x++){
如果(位置[x]>='0'&位置[x]
- 最正确的方法是使用stdlib.h中的
strto…
函数系列。例如:
printf("%ld\n", strtol(str, NULL, 10));
atoi
函数系列不应用于任何目的,因为它们破坏了错误处理,可以100%由strto…
函数替换
- 您可以使用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));
}
#包括
#包括
#包括
#包括
内部主(空)
{
常量字符str[]=“0013subdivision”;
字符编号\部件[sizeof(str)];
字符字母_部分[sizeof(str)];
尺寸i;
对于(i=0;str[i]!='\0'&&isdigit(str[i]);i++)//查找字母的起始位置
{}
memcpy(数字部分,&str[0],i);//复制数字部分
编号_part[i]='\0';//追加空终止符
memcpy(字母部分,&str[i],sizeof(str)-i);//复制字母部分+空项
puts(数字_部分);
puts(字母_部分);
printf(“%ld\n”,strtol(str,NULL,10));
}
如果字符串是运行时变量,则必须使用strlen(str)+1
而不是sizeof()
将字符串转换为数字,并返回它停止时的字符,即数字后的第一个字符
#包括
#包括
内部主(空){
常量字符*常量输入=“0013subdivision”;
常量字符*字符串;
const long number=strtol(输入和字符串,10);
printf(“编号:%ld字符串:“%s”\n”,编号,字符串);
//编号:13字符串:“细分”
返回0;
}
使用*scanf
有什么问题?!%d
用于十进制(?)整数…%s
用于字符串部分同样,不需要int
数组来读取int
。