#include <iostream>
using namespace std;
int main() {
string s;
cin>>s;
string arr[10]={"zero","one","two","three","four","five","six","seven","eight","nine"}; //string 数组
int sum=0;
for(int i=0;i<s.length();i++)
sum+=s[i]-'0';
s=to_string(sum);
for(int i=0;i<s.length();i++){
if(i==0)cout<<arr[s[i]-'0']; //printf不能输出字符串string
else cout<<" "<<arr[s[i]-'0'];
}
return 0;
}
补充细节:
C语言中,没有字符串类型,用字符数组处理字符串
输入输出方法:逐个字符输入输出:%c,整个字符串输入输出:%s
当以%s输入字符串的时候:输入字符串长度超过字符数组元素个数,不报错
但是以%s输出字符串的时候,输入字符串长度超过字符数组元素个数,会报错
printf(“%s”,地址值) 输出时遇到第一个’\0’为止 (空格可以输出),(以scanf输入的空格,在printf中可以输出)但遇到‘\0’就不能再输出;
若想输出字符数组:字符数组里要有内容,或初始化过,并要有字符串结束符,才好用%s输出 !!!
" "双引号字符串:
双引号在表示字符串常量时使用,可以表示0到多个字符组成的字符串。
char s1[] = “a”;
char s2[] = “a1A”;
char s3[] = “”; // 双引号中间可以没有任何字符,表示空字符串
’ '单引号 单个字符:
单引号在字符常量时使用,表示单个字符。
例如:
char c;
c = ‘a’;
c = ‘1’;
c = ‘A’;
当在单引号中出现两个及以上字符时或没有字符时,编译出错。
#include <iostream>
using namespace std;
int main() {
string s1="hello world\0";
char s2[100]="hello world\0 hello world";
printf("%s\n",s1);
printf("%s\n",s2);
return 0;
}