C语言实现英文名称缩略语转换:将“world wide web”转换为“WWW”
以下是用C语言编写的实现://n//nc//n#include <stdio.h>//n#include <ctype.h>//n//nvoid abbr(char *s1, char *s2);//n//nint main() {//n char s1[] = ///'world wide web///';//n char s2[4]; // 由于缩略语只有3个字母,所以s2的长度为4,包括结尾的空字符'//0'//n//n abbr(s1, s2);//n printf(///'Abbreviation: %s//n/// ', s2);//n//n return 0;//n}//n//nvoid abbr(char *s1, char *s2) {//n int i = 0, j = 0;//n//n while (s1[i] != '//0') {//n if (isalpha(s1[i])) { // 判断字符是否为字母//n s2[j] = toupper(s1[i]); // 将字母转换为大写形式//n j++;//n }//n i++;//n }//n//n s2[j] = '//0'; // 在缩略语的末尾添加空字符//n}//n
//n//n这段代码首先定义了一个函数abbr
,用于将英文名称转换为缩略语。然后在main
函数中,初始化了字符串s1
为/'world wide web/',并声明了一个长度为4的字符串s2
用于存储缩略语。//n//n在abbr
函数中,使用了两个整型变量i
和j
来分别遍历s1
和s2
。通过一个循环,遍历s1
中的每个字符,如果字符是字母,则将其转换为大写形式,并存储到s2
中,同时j
自增。最后,在s2
的末尾添加空字符。//n//n运行以上代码,输出结果为://n//n//nAbbreviation: WWW//n
原文地址: http://www.cveoy.top/t/topic/pEEU 著作权归作者所有。请勿转载和采集!