C语言问题:给定一个任意字符串,提取出其中所包含的整数。 c语言问题,如何将字符串中的数字提取
C\u8bed\u8a00:\u4e00\u4e2a\u7ed9\u5b9a\u7684\u5b57\u7b26\u4e32,\u63d0\u53d6\u5176\u4e2d\u7684\u6570\u5b57,\u8fde\u5728\u4e00\u8d77\u6784\u6210\u4e00\u4e2a\u6574\u6570,\u518d\u4e58\u4ee5513,\u5c31\u662f\u7ed3\u679c#include #define N 100int main() { int i; long r;//\u8981\u6ce8\u610f\u7ed3\u679c\u53ef\u80fd\u4f1a\u8d85\u51fa\u666e\u901a\u6574\u5f62\u8303\u56f4 char s[N]; scanf("%s",s); i=0; r=0; while(s[i]!='\0'){ if(s[i]>='0'&& s[i]<='9') r=r*10+(s[i]-'0'); ++i; } // r*=513; printf("\u8f93\u5165\u4e32:s=%s\n\u63d0\u53d6\u6570\u5b57:r=%ld\n\u7ed3\u679c:r*513=%ld\n",s,r,r*513); return 0;}
1\u3001\u9996\u5148\u6253\u5f00visual studio\u8f6f\u4ef6\uff0c\u65b0\u5efa\u4e00\u4e2aC\u8bed\u8a00\u6587\u4ef6\uff0c\u5982\u4e0b\u56fe\u6240\u793a\u3002
2\u3001\u63a5\u7740\u5728C\u8bed\u8a00\u6587\u4ef6\u7684\u9876\u90e8\u5bfc\u5165\u5e93\u5185\u5bb9\uff0c\u5982\u4e0b\u56fe\u6240\u793a\u3002
3\u3001\u63a5\u7740\u8fd0\u7528scanf\u51fd\u6570\u63a5\u6536\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u3002
4\u3001\u7136\u540e\u5229\u7528printf\u51fd\u6570\u6253\u5370\u4e00\u4e0b\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u3002
5\u3001\u8fd0\u884c\u7a0b\u5e8f\u4ee5\u540e\u5c31\u4f1a\u5f39\u51fa\u5982\u4e0b\u56fe\u6240\u793a\u7684CMD\u754c\u9762\uff0c\u8f93\u5165\u5185\u5bb9\u5c31\u4f1a\u81ea\u52a8\u63a5\u6536\u5230\uff0c\u7136\u540e\u539f\u6837\u8f93\u51fa\u5185\u5bb9\u3002
6\u3001\u6700\u540e\u5982\u679c\u63a5\u6536\u5176\u4ed6\u7684\u7c7b\u578b\uff0c\u6bd4\u5982\u6574\u6570\uff0c\u90a3\u4e48\u7c7b\u578b\u5c31\u9700\u8981\u6539\u53d8\u6210int\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5e76\u4e14scanf\u91cc\u9762\u7684\u63a5\u6536\u7c7b\u578b\u53d8\u4e3ad%\uff0c\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u3002
查找字符串中的数字并返回其个数:
#include <stdio.h>
int main(void)
{
char str[1000];
int i, count, a[100];
int p, q, r;
int tmp;
gets(str);
r = 0;
count = 0;
while(1)
{
while(str[r] && (str[r]<'0' || str[r]>'9'))
r++; //跳过非数字
if(str[r])
{
p = r; //p指向数字子串开头
q = r + 1; //q寻找数字串结尾
a[count] = str[r]-'0';
while(str[q]>='0' && str[q]<='9')
{
a[count] = 10*a[count]+(str[q]-'0');
q++;
}
r = q; //新起点
count++;
}
else
break;
}
printf("共计 %d 个整数:", count);
for(i=0; i<count; i++)
printf("%d ", a[i]);
printf("
");
return 0;
}
具体代码不给你写了,很简单,就是判断是否是整数,ASCII码大于'0'小于'9'均为整数,保存下来,如果下一个是整数,继续保存,如果下一个不是整数,用空格代替,并继续读取。最后输出保存的结果。
绛旓細鏌ユ壘瀛楃涓涓殑鏁板瓧骞惰繑鍥炲叾涓暟锛歩nclude <stdio.h>int main(void){char str[1000];int i, count, a[100];int p, q, r;int tmp;gets(str);r = 0;count = 0;while(1){while(str[r] && (str[r]<'0' || str[r]>'9'))r++;//璺宠繃闈炴暟瀛 if(str[r]){p = r;//p鎸囧悜...
绛旓細/*缂栫▼浠庨敭鐩樿緭鍏ヤ竴涓瓧绗︿覆锛岀粺璁¤瀛楃涓蹭腑浠'a'鍒'z'鍏26涓皬鍐欏瓧姣嶅悇鑷嚭鐜扮殑娆℃暟锛屽皢缁撴灉瀛樺叆鏁扮粍涓紝骞惰緭鍑*/ include<stdio.h> include<string.h> define N 100 main(){int i,j=0,c[26]={0};char str[N];printf("璇疯緭鍏涓涓瓧绗︿覆锛");gets(str);for(i=0;i<strlen(str);...
绛旓細include <stdio.h> include <string.h> void main(){ int a=0,b=0,i;char c[300];printf("璇疯緭鍏涓涓灏戜簬300瀛楃鐨瀛楃涓诧細");gets(c);for(i=0;i<300,c[i]!='\0';i++){ if(c[i]==' ')a++;else b++;} printf("瀛楃涓蹭腑绌烘牸瀛楃鏈%d涓紝闈炵┖鏍煎瓧绗︽湁%d涓猏n",a,b)...
绛旓細include <string.h> int main(){ char a[255] = "";char b[255] = "";printf("璇疯緭鍏瀛楃涓a:");scanf("%s", a);printf("璇疯緭鍏ュ瓧绗︿覆b:");scanf("%s", b);char* p = strstr(a, b);if (p != NULL)printf("%s绗竴娆″嚭鐜板湪%s鐨勪綅缃负:%d", b, a, p - a);else p...
绛旓細1 瀹氫箟涓や釜flag锛岀敤鏉ユ爣璁版槸鍚﹀惈鏈夊瓧姣嶅拰鏁板瓧锛屽垵濮嬪间负0锛2 杈撳叆瀛楃涓锛3 閬嶅巻瀛楃涓诧紝濡傛灉鍙戠幇瀛楁瘝锛屾爣璁板瓧姣峟lag锛屽鏋滃彂鐜版暟瀛楋紝鏍囪鏁板瓧flag锛4 閬嶅巻缁撴潫锛屾垨鑰呬袱涓猣lag鍧囪鏍囪锛岄鍑哄惊鐜紱5 杈撳嚭缁撴灉銆備唬鐮佸涓嬶細int main(){ char s[100]; int c,n, i; c = n = 0; get...
绛旓細閲囩撼鐜:52% 鏉ヨ嚜鍥㈤槦:C* 鎿呴暱: C/C++ VC++ VB 瀹夊叏杞欢 鏁版嵁搴揇B 鍏朵粬鍥炵瓟 瀹氫箟涓涓瓧绗︿覆鏁扮粍,鐒跺悗鐢╢or璇彞寰幆,鎵惧埌浣犺鍒犻櫎鐨勯偅涓瓧绗,鍒犻櫎鍚庡氨閫鍑哄惊鐜 menghuahewei | 鍙戝竷浜2011-11-01 涓炬姤| 璇勮 2 0 涓烘偍鎺ㄨ崘: c璇█绋嬪簭璁捐 c璇█ 瀛楃涓 c璇█闅愯棌瀛楃涓 c 瀛楃涓插垹闄ゆ寚瀹氬瓧绗...
绛旓細include<stdio.h> include<stdlib.h> void printfc(char s[],int i){ while(i!=0){ i--;printf("%c",s[i]);} printf("\n");} void main(){ int i=0;char a[100];printf("璇疯緭鍏瀛楃涓\n");gets(a);while(a[i]!='\0'){ i++;} printfc(a,i);} ...
绛旓細include <stdio.h>#include <string.h>void main(){ char ch[100]; char c; int index; memset(ch,NULL,100);//鍒濆鍖栨墍鏈夊厓绱犱负绌 puts("璇疯緭鍏ヤ竴涓插瓧绗︼細\n"); gets(ch); puts("璇疯緭鍏涓涓瓧绗︼細"); scanf("%c",&c); puts("璇疯緭鍏ヨ鎻掑叆鐨勪綅缃細"...
绛旓細int i = 0 ; /* 瀹氫箟涓涓暣鍨嬪彉閲 */ unsigned char my_string[MAX_STRLEN] ; /* 瀹氫箟涓涓瓧绗︿覆鏁扮粍 */ gets(my_string) ; /* 浠庨敭鐩樹笂杈撳叆浠绘剰涓涓瓧绗︿覆锛岄暱搴︿笉瑕佽秴杩 MAX_STRLEN */ for( i = 0 ; my_string[i] != '\0' ; i ++ ) /* 鍒ゆ柇褰撳墠瀛楃鏄惁涓哄瓧绗︿覆缁撴潫绗...
绛旓細include <stdio.h>#include <stdlib.h>int main(){ char as[1000]; gets(as); int m; scanf("%d",&m); char *p=as; printf("%c",*(p+m-1)); return 0;}