如何用c语言分离字符串中的字母和数字并分别输出
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用c语言分离字符串中的字母和数字并分别输出相关的知识,希望对你有一定的参考价值。
给出代码:#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
using namespace std;
int main()
int num,snum;
num=0;
snum=0;
char s[100];
char zi[100];
char fu[100];
scanf("%s",s);
for(int i=0;i<strlen(s);i++)
if(s[i]<='9'&&s[i]>='0')
zi[num]=s[i];
num++;
else if(s[i]<='z'&&s[i]>='a'||s[i]<='Z'&&s[i]>='A')
fu[snum]=s[i];
snum++;
for(int i=0;i<=num;i++)
printf("%c",zi[i]);
cout<<endl;
for(int j=0;j<=snum;j++)
printf("%c",fu[j]);
return 0;
参考技术A 先定义两个字符串数组变量a,b。。。。然后把原来的字符串进行遍历,判断每个字符的ACSSII码。。在数字区间的存在a中。。。字母的存在b中。。。然后再把a,b输出。 参考技术B 回答
您好,很高兴为您解答。
把字符串存放到数组里面,一个一个比对(循环)后输出
判断数字:
for (i=0;i if ((p[i]>=\'0\') && (p[i]<=\'9\')) printf(); 判断字母: for (i=0;i 如何用c语言分离字符串中的字母和数字并分别输出? 定义两个数的加减乘除的四则运算,通过switch语句选择运算,输出对应的结果 给出代码: #include #include #include #include using namespace std;int main() int num,snum; num=0; snum=0; char s[100]; char zi[100]; char fu[100]; scanf("%s",s); for(int i=0;i 代码确定没错的 ? 运行不起 请截图 代码是正确的喔
#include<string>
using namespace std;
int main()
string s;
char *s1=new char[256],*s2=new char[256];
cout<<"请输入字符串:\n";
cin>>s;
int j1=0,j2=0;
for(int i=0;s[i];i++)
if(s[i]<='9'&&s[i]>='0')
s1[j1]=s[i];
j1++;
if(s[i]<='z'&&s[i]>='a'||s[i]<='Z'&&s[i]>='A')
s2[j2]=s[i];
j2++;
s1[j1]=0,s2[j2]=0;
cout<<"输入字符串中包含的数字字符串为:";
cout<<s1<<endl;
cout<<"输入字符串中包含的字母字符串为:";
cout<<s2<<endl;
return 0;
本回答被提问者采纳 参考技术D 班门弄斧一下吧;大师们勿怪
char c[]="123asdf564afg" ,c1[100] ,c2[100];
int len=sizeof(c)/sizeof(char);
int i ,j =0,k=0;
for(i=0 ;i<len ;i++)
if(c[i]<'9'&&c[i]>'0')
c1[j++]=c[i];
else if(c[i]<'z'&&c[i]>'A')
c2[k++]=c[i];
c1[j]='\0';
c2[k]='\0';
puts[c1];
puts[c2];
主程序完善一下就行了
如何用C语言将输入的数字转化成英语
数字为1-10000之间,由于我刚开始学C,我用的一种比较传统的方法比较复杂要写很多语句,所以想请教一下高手有没有什么简单的方法,谢谢!
是将数字转化成英文啊 如;11:ELEVEN,135:ONE HUNDRED FIVE
和你的相比,不知是否复杂。
此程序的计算范围:0<=num<1000。如果还想要计算更大的数,可以在最后面加判断语句,方法类似。
#include<stdio.h>
void main()
char *Eng1[20]="zero","one","two","three","four","five","six","seven",
"eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen",
"sixteen","seventeen","eighteen","nineteen";
char *Eng2[8]="twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety";
int num;
printf("请输入数字: ");
scanf("%d",&num);
printf("对应的英文为: ");
if(num>=0&&num<=19)
printf("%s\n",Eng1[num]);
else if(num<100)
int s,y;
s=num/10;
y=num%10;
printf("%s %s\n",Eng2[s-2],Eng1[y]);
else if(num<1000)
int b,s,y;
b=num/100;
y=num%100;
if(y>9)
s=(num%100)/10;
y=(num%100)%10;
if(y==0)
printf("%s hundred and %s\n",Eng1[b],Eng2[s-2]);
else
printf("%s hundred and %s %s\n",Eng1[b],Eng2[s-2],Eng1[y]);
else
printf("%s hundred and %s\n",Eng1[b],Eng1[y]);
参考技术A 只有按照ASCII码进行对应转换,把输入的数字转换为相应的英文字母,没有其他方法了 参考技术B #include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[])
char a;
while('\n'!=a)
a=getchar();
switch (a)
case '0':printf("zero ");break;
case '1':printf("one ");break;
case '2':printf("two ");break;
case '3':printf("three ");break;
case '4':printf("four ");break;
case '5':printf("five ");break;
case '6':printf("six ");break;
case '7':printf("seven ");break;
case '8':printf("eight ");break;
case '9':printf("nine ");break;
return 0;
参考技术C #include<stdio.h>
char *Eng1[20] = "zero", "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen" ;
char *Eng2[8] = "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" ;
int num;
void fun(int num)
if (num >= 0 && num <= 19)
printf("%s\n", Eng1[num]);
else if (num<100)
int s, y;
s = num / 10;
y = num % 10;
printf("%s %s\n", Eng2[s - 2], Eng1[y]);
else if (num<1000)
int b, s, y;
b = num / 100;
y = num % 100;
if (y>9)
s = (num % 100) / 10;
y = (num % 100) % 10;
if (y == 0)
printf("%s hundred %s ", Eng1[b], Eng2[s - 2]);
else
printf("%s hundred %s %s", Eng1[b], Eng2[s - 2], Eng1[y]);
else
printf("%s hundred %s", Eng1[b], Eng1[y]);
void main()
scanf("%d", &num);
if (num > 0)
if (num > 1000)
if (num > 1000000)
if (num > 1000000000)
int numw = num / 1000000000;
num = num % 1000000000;
fun(numw);
printf(" billion ");
int numx = num / 1000000;
num = num % 1000000;
fun(numx);
printf(" million ");
int numy = num / 1000;
num = num % 1000;
fun(numy);
printf(" thousand ");
int numz = num;
fun(num);
根据我的题目中间不用and,要改可以自调,数据范围一直到100billon,在此谢谢mhy8946(不会是清洁工吧。。。同行么)的算法,要不然我也搞不出来hh 参考技术D 没有
以上是关于如何用c语言分离字符串中的字母和数字并分别输出的主要内容,如果未能解决你的问题,请参考以下文章