C语言题目:提取一个字符串中的所有数字字符(‘0’...‘9’)将其转换为一个整数输出。 C语言 将用户输入的字符串中的所有数字提取出来
C\u8bed\u8a00\u9898\u76ee:\u63d0\u53d6\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6570\u5b57\u5b57\u7b26\uff08\u20180\u2019...\u20189\u2019\uff09\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6574\u6570\u8f93\u51fa\u3002\u8bc4\u6d4b\u00b7\u7cfb\u7edf\u663e\u793a\u7684\u662fwrong answer\u5417\uff1f\u5982\u679c\u662fPE\uff0c\u8bf7\u4fee\u6539\u8f93\u51fa\u683c\u5f0f\u3002
\u8fd9\u91cc\u6211\u6307\u51fa\u4e00\u79cd\u60c5\u51b5\uff0c\u5c31\u662f\u6bd4\u5982\u6211\u8f93\u5165a0123a \u8fd9\u7ec4\u5b57\u7b26\u4e32\uff0c\u90a3\u6309\u7167\u9898\u76ee\u7684\u8981\u6c42\u5e94\u8be5\u8f93\u51fa\u7684\u662f123\uff0c\u800c\u4f60\u8f93\u51fa\u7684\u80af\u5b9a\u662f0123\u3002\u6211\u4e0d\u77e5\u9053\u8fd9\u662f\u4e0d\u662f\u4f60\u4e0d\u901a\u8fc7\u7684\u6d4b\u8bd5\u7528\u4f8b\uff0c\u6211\u4e5f\u6ca1\u6709\u4f60\u6d4b\u8bd5\u5e73\u53f0\u7684\u5730\u5740\uff0c\u6ca1\u529e\u6cd5\u9a8c\u8bc1\uff0c\u5c31\u5f53\u662f\u6211\u7ed9\u4f60\u4e00\u70b9\u5efa\u8bae\u5427\u3002
#include
#include
#include
intmain()
{
intz=0;
intt,i;
int*p;
intm=0;
ints=0;
chara[10];//\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\u5230a[6]
printf("\u8bf7\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\n");
scanf("%s",a);//\u8f93\u5165\u5b57\u7b26\u4e32
for(i=0;i<10;i++)//\u8ba1\u7b97a[6]\u4e2d\u7684\u6570\u5b57\u4e2a\u6570
{
if(a[i]>='0'&&a[i]<='9')
m++;
}
if(m==0)
printf("\u5b57\u7b26\u4e32\u4e2d\u6ca1\u6709\u6570\u5b57\n");
p=(int*)calloc(m,sizeof(int));//\u52a8\u6001\u6570\u7ec4
for(i=0;i<10;i++)
{
if(a[i]>='0'&&a[i]<='9')
{
*(p+s)=a[i]-48;
s++;
}
}
//\u7ec4\u6210\u65b0\u7684\u6574\u6570
for(t=0;t<m;t++)
{
z+=(*(p+t))*pow(10,m-(t+1));//\u7ec4\u6210\u65b0\u7684\u6574\u6570
}
printf("\u65b0\u7ec4\u6210\u6574\u6570\uff1a%d\n",z);
free(p);
}
kuo
\u6269\u5c55\u8d44\u6599
C\u8bed\u8a00\u5b9e\u73b0\u5c06\u5b57\u7b26\u4e32\u7684\u6570\u5b57\u63d0\u53d6\u4e3a\u6574\u6570
#include
intStringToInteger(char*p)
{
intvalue=0;
while(*p!='\0')
{
if((*p>='0')&&(*p<='9'))
{
value=value*10+*p-'0';
}
p++;
}
returnvalue;
}
intmain(void)
{
char*str="112.123.156.1";
intval=0;
val=StringToInteger(str);
printf("value=%d\n\r",val);
getchar();
return0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char s[20];
int i;
printf("输入包含数字的字符串:");
scanf("%s",s);
printf("其中整型数字位为:");
for(i=0;i<strlen(s);i++)
{
if(s[i]>=48 && s[i]<=57)
{
printf("%d",(int)s[i]-48);
}
}
return 0;
}
绛旓細include<stdio.h>#include<string.h>int main(){ char s[20]; int i; printf("杈撳叆鍖呭惈鏁板瓧鐨瀛楃涓诧細"); scanf("%s",s); printf("鍏朵腑鏁村瀷鏁板瓧浣嶄负锛"); for(i=0;i<strlen(s);i++) { if(s[i]>=48 && s[i]<=57) { printf("%d",(int)s[...
绛旓細鏌ユ壘瀛楃涓蹭腑鐨鏁板瓧骞惰繑鍥炲叾涓暟锛歩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鎸囧悜...
绛旓細string[] split = url.Split('.');string result = split[1];Label1.Text = result;
绛旓細1銆侀鍏堟墦寮visual studio杞欢锛屾柊寤涓涓狢璇█鏂囦欢锛屽涓嬪浘鎵绀恒2銆佹帴鐫鍦–璇█鏂囦欢鐨勯《閮ㄥ鍏ュ簱鍐呭锛屽涓嬪浘鎵绀恒3銆佹帴鐫杩愮敤scanf鍑芥暟鎺ユ敹鐢ㄦ埛杈撳叆鐨瀛楃涓銆4銆佺劧鍚庡埄鐢╬rintf鍑芥暟鎵撳嵃涓涓嬬敤鎴疯緭鍏ョ殑鍐呭銆5銆佽繍琛岀▼搴忎互鍚庡氨浼氬脊鍑哄涓嬪浘鎵绀虹殑CMD鐣岄潰锛岃緭鍏ュ唴瀹瑰氨浼氳嚜鍔ㄦ帴鏀跺埌锛岀劧鍚庡師鏍疯緭鍑哄唴瀹广6...
绛旓細h> int main() { char s[100];int i;scanf("%s",s);for(i = 0;s[i];i++) { if(a[i] >= 'a' && a[i] <= 'z') s[i] -= 'a' - 'A';else if(a[i] >= 'A' && a[i] <= 'Z') a[i] -= 'A' - 'a';} printf("%s\n",s);return 0;} ...
绛旓細intmain(){ intz=0;intt,i;int*p;intm=0;ints=0;chara[10];//杈撳叆涓涓瓧绗︿覆鍒癮[6]printf("璇疯緭鍏ヤ竴涓瓧绗︿覆\n");scanf("%s",a);//杈撳叆瀛楃涓 for(i=0;i<10;i++)//璁$畻a[6]涓殑鏁板瓧涓暟 { if(a[i]>='0'&&a[i]<='9')m++;} if(m==0)printf("瀛楃涓蹭腑娌℃湁...
绛旓細浣跨敤isdigit()鍑芥暟銆傚湪C璇█涓锛屽彲浠ヤ娇鐢ㄦ爣鍑嗗簱鍑芥暟isdigit()鏉ユ鏌涓涓瓧绗鏄惁涓烘暟瀛椼俰sdigit()鍑芥暟瀹氫箟鍦ㄥご鏂囦欢ctype.h涓紝鍘熷瀷鏄痠ntisdigit(intc)銆
绛旓細鐢 sscanf() ;渚嬪锛歝har str[80]="1101";int a[4];sscanf(str,"%1d%1d%1d%1d",&a[0],&a[1],&a[2],&a[3]);
绛旓細char s[]="ssssabedbewb";int len;char *p;s[2]; //绗竴绉嶆柟娉 printf("杈撳叆杈撳叆瀛楃涓茬殑闀垮害锛");scanf("%d",&len);printf("杈撳叆瀛楃涓诧細");p=malloc(len);scanf("%s",p);printf("%c",p[3]); //绗簩绉嶆柟娉 鏄剧劧绗簩绉嶆柟娉曟洿鐏垫椿锛屼笉杩囧氨鏄湁鐐圭儲 ...
绛旓細C涓殑瀛楃涓灏辨槸涓涓瓧绗鏁扮粍銆傚锛歝har s[10]="wo shi SB";char c;鍙栨渶宸﹁竟鐨勫瓧绗,灏辨槸c=s[0];