C语言,从字符串中找出数字串 C语言中怎么提取字符串中的整数并求和?
\u7528c\u8bed\u8a00\u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32,\u5e76\u7edf\u8ba1\u4e2a\u6570\u4f8b\u5982\uff0casdfgasfgasf\uff0cas\uff0c\u5219\u8f93\u51fa3\u3002
\uff03include\uff1cstdio\uff0eh\uff1e
\uff03include\uff1cstdlib\uff0eh\uff1e
intmain\uff08\uff09
\uff5b
charS\uff3b100\uff3d\uff0cT\uff3b20\uff3d\uff1b
charch1\uff0cch2\uff1b
printf\uff08\uff02\u8bf7\u8f93\u5165\u4e3b\u5b57\u7b26\u4e32\uff1a\uff3cn\uff02\uff09\uff1b
ch1\uff1dgetchar\uff08\uff09\uff1b
inti\uff1d0\uff1b
while\uff08ch1\uff01\uff1d\uff07\uff3cn\uff07\uff09
\uff5b
S\uff3bi\uff3d\uff1dch1\uff1b
i\uff0b\uff0b\uff1b
ch1\uff1dgetchar\uff08\uff09\uff1b
\uff5d
printf\uff08\uff02\u8bf7\u8f93\u5165\u8981\u7b5b\u9009\u7684\u5b57\u7b26\u4e32\uff1a\uff3cn\uff02\uff09\uff1b
ch2\uff1dgetchar\uff08\uff09\uff1b
intj\uff1d0\uff1b
while\uff08ch2\uff01\uff1d\uff07\uff3cn\uff07\uff09
\uff5b
T\uff3bj\uff3d\uff1dch2\uff1b
j\uff0b\uff0b\uff1b
ch2\uff1dgetchar\uff08\uff09\uff1b
\uff5d
intm\uff0cn\uff1b\uff0f\uff0fm\u4e3aS\u7684\u4e0b\u6807\uff0cn\u4e3aT\u7684\u4e0b\u6807
m\uff1d0\uff1b
n\uff1d0\uff1b
intnum\uff1d0\uff1b\uff0f\uff0fnum\u7528\u4e8e\u8bb0\u5f55\u9009\u5b9a\u5355\u8bcd\u51fa\u73b0\u7684\u6b21\u6570
while\uff08m\uff1c\uff1di\uff06\uff06n\uff1c\uff1dj\uff09
\uff5b
if\uff08S\uff3bm\uff3d\uff1d\uff1dT\uff3bn\uff3d\uff09
\uff5b
m\uff0b\uff0b\uff1b
n\uff0b\uff0b\uff1b
\uff5d
else
\uff5b
m\uff1dm\uff0dn\uff0b1\uff1b
n\uff1d0\uff1b
\uff5d
if\uff08n\uff1d\uff1dj\uff09
\uff5b
num\uff0b\uff0b\uff1b
\uff5d
\uff5d
if\uff08m\uff1d\uff1di\uff0b1\uff09
\uff5b
printf\uff08\uff02\u51fa\u73b0\u7684\u6b21\u6570\u662f\uff05d\uff02\uff0cnum\uff09\uff1b
\uff5d
\uff5d
\u6269\u5c55\u8d44\u6599\uff1awhile\u8bed\u53e5\u82e5\u4e00\u76f4\u6ee1\u8db3\u6761\u4ef6\uff0c\u5219\u4f1a\u4e0d\u65ad\u7684\u91cd\u590d\u4e0b\u53bb\u3002\u4f46\u6709\u65f6\uff0c\u9700\u8981\u505c\u6b62\u5faa\u73af\uff0c\u5219\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u4e09\u79cd\u65b9\u5f0f\uff1a
\u4e00\u3001\u5728while\u8bed\u53e5\u4e2d\u8bbe\u5b9a\u6761\u4ef6\u8bed\u53e5\uff0c\u6761\u4ef6\u4e0d\u6ee1\u8db3\uff0c\u5219\u5faa\u73af\u81ea\u52a8\u505c\u6b62\u3002
\u5982\uff1a\u53ea\u8f93\u51fa3\u7684\u500d\u6570\u7684\u5faa\u73af\uff1b\u53ef\u4ee5\u8bbe\u7f6e\u8303\u56f4\u4e3a\uff1a0\u523020\u3002
\u4e8c\u3001\u5728\u5faa\u73af\u7ed3\u6784\u4e2d\u52a0\u5165\u6d41\u7a0b\u63a7\u5236\u8bed\u53e5\uff0c\u53ef\u4ee5\u4f7f\u7528\u6237\u9000\u51fa\u5faa\u73af\u3002
1\u3001break\u6d41\u7a0b\u63a7\u5236\uff1a\u5f3a\u5236\u4e2d\u65ad\u8be5\u8fd0\u884c\u533a\u5185\u7684\u8bed\u53e5\uff0c\u8df3\u51fa\u8be5\u8fd0\u884c\u533a\uff0c\u7ee7\u7eed\u8fd0\u884c\u533a\u57df\u5916\u7684\u8bed\u53e5\u3002
2\u3001continue\u6d41\u7a0b\u63a7\u5236\uff1a\u4e5f\u662f\u4e2d\u65ad\u5faa\u73af\u5185\u7684\u8fd0\u884c\u64cd\u4f5c\uff0c\u5e76\u4e14\u4ece\u5934\u5f00\u59cb\u8fd0\u884c\u3002
\u4e09\u3001\u5229\u7528\u6807\u8bc6\u6765\u63a7\u5236while\u8bed\u53e5\u7684\u7ed3\u675f\u65f6\u95f4\u3002
int main()
{
int sum =0;
int flag = 0;
char str[100];
cout << "\u8f93\u5165\u5b57\u7b26\u4e32:";
cin >> str;
int len = strlen(str);
cout << str << endl;
char temp[100];
memset(temp, 0, sizeof(temp));
int n=0;
for (int i=0; i<len; i++ )
{
if (str[i]>='0' && str[i] <= '9')
{
temp[n] = str[i];
n++;
flag =1;
}
else
{
if (flag == 1)
{
temp[n] ='\0';
cout << temp << endl;
sum += atoi(temp);
}
memset(temp, 0, sizeof(temp));
flag =0;
n=0;
}
}
cout << sum << endl;
return 0;
}
#include <stdlib.h>
void main(){
char str[400],a[30][30],*p;
int i=0,j=0,k,flag=0;
gets(str);
p=str;
for(;*p!='\0';p++){
if(*p>='0'&&*p<='9'){
a[i][j++]=*p;
flag=1;}
else if(flag==1){
i++;
j=0;
flag=0;}
}
for(k=0;k<=i;k++){
for(j=0;a[k][j]!='\0';j++)
printf("%c",a[k][j]);
printf("\n");}
}
绛旓細printf("%c",a[k][j]);printf("\n");} }
绛旓細浣跨敤isdigit()鍑芥暟銆傚湪C璇█涓紝鍙互浣跨敤鏍囧噯搴撳嚱鏁癷sdigit()鏉ユ鏌ヤ竴涓瀛楃鏄惁涓鏁板瓧銆俰sdigit()鍑芥暟瀹氫箟鍦ㄥご鏂囦欢ctype.h涓紝鍘熷瀷鏄痠ntisdigit(intc)銆
绛旓細1銆佺‘瀹氱▼搴忕粨鏋 绋嬪簭闇瑕佷袱灞傚惊鐜祵濂楋紝璇诲彇repeat鍚庤繘鍏ョ涓灞傚惊鐜紝绗竴娆″惊鐜敤浜庣‘瀹氳鍙栫殑瀛楃涓叉暟锛岀浜屽眰寰幆鐢ㄤ簬璇诲彇瀛楃鍜屼慨姝f暟鎹傜▼搴忔鍥惧涓嬪浘鎵绀:2銆佺‘璁ょ▼搴忎娇鐢ㄥ彉閲忓強绫诲瀷 鏍规嵁绋嬪簭闇姹傚彲浠ョ‘瀹氭墍闇鍙橀噺鍙婄被鍨嬶紙濡備笅鍥炬墍绀猴級锛屾澶勫簲娉ㄦ剰閫氳繃寰幆璇诲彇瀛楃骞跺悓姝ョ粺璁★紝鏁呬粎闇瑕佷娇鐢ㄤ竴涓猚h...
绛旓細1銆侀鍏堟墦寮visual studio杞欢锛屾柊寤轰竴涓C璇█鏂囦欢锛屽涓嬪浘鎵绀恒2銆佹帴鐫鍦–璇█鏂囦欢鐨勯《閮ㄥ鍏ュ簱鍐呭锛屽涓嬪浘鎵绀恒3銆佹帴鐫杩愮敤scanf鍑芥暟鎺ユ敹鐢ㄦ埛杈撳叆鐨瀛楃涓銆4銆佺劧鍚庡埄鐢╬rintf鍑芥暟鎵撳嵃涓涓嬬敤鎴疯緭鍏ョ殑鍐呭銆5銆佽繍琛岀▼搴忎互鍚庡氨浼氬脊鍑哄涓嬪浘鎵绀虹殑CMD鐣岄潰锛岃緭鍏ュ唴瀹瑰氨浼氳嚜鍔ㄦ帴鏀跺埌锛岀劧鍚庡師鏍疯緭鍑哄唴瀹广6...
绛旓細C璇█涓紝鍙互浣跨敤atoi鍑芥暟灏嗗瓧绗︿覆杞崲涓烘暟瀛锛屽atoi("123")鍙互寰楀埌鏁板瓧123銆 atoi (琛ㄧず ascii to integer)鏄妸瀛楃涓茶浆鎹㈡垚鏁村瀷鏁扮殑涓涓嚱鏁帮紝搴旂敤鍦ㄨ绠楁満绋嬪簭鍜屽姙鍏蒋浠朵腑銆俰nt atoi(const char *nptr) 鍑芥暟浼氭壂鎻忓弬鏁 nptr瀛楃涓诧紝浼氳烦杩囧墠闈㈢殑绌虹櫧瀛楃锛堜緥濡傜┖鏍硷紝tab缂╄繘锛夌瓑銆 濡傛灉 nptr涓...
绛旓細/*鎼滅储瀛楃涓蹭腑鏁板瓧鐨勪綅缃*/ int findNum(char *pStr,/*鍙楁瀛楃涓*/ int startPos/*妫绱㈠紑濮嬩綅缃*/ ){ int i=startPos;char ch;for(;(ch=pStr[i])!='\0';++i){ if(ch>='0'&&ch<='9')return i;} return -1;} int main(){ int pos=0;char *str="a1b2c3";while((pos...
绛旓細char s[maxn];char *p;int cmp(const void* a, const void* b) { //C 璇█姣旇緝鍑芥暟锛岀敤浜巕sort return *(int *)a - *(int *)b;} void func(char *s, int length) { //澶勭悊浣犵粰鍑虹殑瀛楃涓 ,length涓洪暱搴 static int arr[maxn];int cnt = 0;p=s;char sign;while ((p ...
绛旓細int main(){ char str[1000]; //瀛楃涓 memset(str,1000,0);scanf("%s",str);char ch;int pos1(0),pos2(0);char s1[500],s2[500];memset(s1,500,0);memset(s2,500,0);for(int i=0;i<strlen(str);i++){ ch=str[i];if(ch<='9'&& ch>='0'){ //缁熻鏁板瓧涓暟 s1[...
绛旓細include <stdio.h>void main(){char a[100],s[100];int i;for(i=0;i<=100;i++)a[i]=10;printf("璇疯緭鍏ュ皬浜100涓殑瀛楃\n"); for(i=0;i<100;i++) scanf("%c",&s[i]);for(i=0;s[i]!='\0';i++)a[s[i]-48]=s[i]-48;for(i=0;i<100;i++)if(a[i]!=10...
绛旓細include<stdio.h>main(){ char str[100]; int s[50]; int i=0,j=0,count=0,flag=0,t=0; puts("璇疯緭鍏瀛楃涓锛"); gets(str); do //鎻愬彇鏁存暟 { if('0'<=str[i]&&str[i]<='9') { flag=1; t=10*t+str[i]-'0'; } else { if(flag==1) {...