写一段代码将a_b_ _c_ _ _d_转换成_ _ _ _ _ _ _abcd;
int main(int argc, const char * argv[])
{
char str[20];
int count=0,tcount=0; //count是下划线计数器
//tcount是非下划线字符计数器
while (scanf("%s",str)!=EOF) {
int lenth=(int)strlen(str); //lenth是输入字符串长度
for (int j=lenth-1; j>=0;j--) {
if (str[j]=='_') { //从字符串尾部判断,如果是下划线
count++; //那么count加一
}else{ //如果是非下划线字符,”
tcount++; //那么tcount加一,
str[lenth-tcount]=str[j]; //并且用当前字符加入“栈
}
}
for (int i=0; i<count; i++) { //对前面写入下划线
str[i]='_';
}
printf("%s",str);
count =0;
tcount=0;
}
return 0;
}
本文介绍了一段C代码,用于将包含下划线的字符串转换为纯字母串,通过计数下划线并相应地调整字符位置实现转换。
2万+

被折叠的 条评论
为什么被折叠?



