c语言输出单词首写字母,C语言!! 用*号输出单词!!
时间: 2024-03-09 18:46:51 浏览: 19
可以使用以下代码实现:
```c
#include <stdio.h>
#include <ctype.h> // 包含 isalpha 函数
int main() {
char str[100];
printf("请输入一个字符串:");
fgets(str, 100, stdin); // 从标准输入读取字符串
int i = 0;
while (str[i] != '\0') { // 遍历字符串
if (isalpha(str[i])) { // 判断是否是字母
putchar(toupper(str[i])); // 输出大写字母
} else {
putchar('*'); // 输出 *
}
while (isalpha(str[i+1])) i++; // 跳过单词中的其他字母
i++;
}
return 0;
}
```
该程序可以输入一个字符串并输出单词的首字母,如果不是字母则用 * 替代。例如输入 "Hello world!",输出结果为 "H**** W****!"。
相关问题
c语言提取单词首字母
要提取单词的首字母,可以使用以下方法:
1. 读入一行文本。
2. 遍历每个字符,判断该字符是否为字母。
3. 如果是字母,则将该字符转换为大写或小写(根据需要),并输出。
4. 如果不是字母,则继续遍历下一个字符。
以下是一个示例代码,可以提取一行文本中每个单词的首字母:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char line[100];
printf("请输入一行文本:\n");
fgets(line, 100, stdin);
int i = 0;
while (line[i] != '\0') {
// 如果当前字符是字母,则输出其大写形式
if (isalpha(line[i])) {
putchar(toupper(line[i]));
// 跳过当前单词的其它字母
while (isalpha(line[i+1])) {
i++;
}
}
i++;
}
return 0;
}
```
注意,这个代码只能提取英文单词的首字母,如果要处理中文或其它语言的单词,需要使用不同的方法。
c语言单词首字母大写
以下是两种C语言单词首字母大写的例子:
1.在一行中输出已输入的字符,其中所有单词的首字母已改为大写。
```c
#include <stdio.h>
int main(int argc, char *argv[]) {
char ch;
ch=getchar();
int flag=1;//首字母大写标志
while(ch!='\n') {
if(ch>='a'&&ch<='z'&&flag==1)//小写字母并且有大写字母标志,则大写
{
ch=ch-32;
putchar(ch);
}
else {
putchar(ch);
}
if(ch==' ')//空格标志下一个字母要大写
flag=1;
else
flag=0;
ch=getchar();
}
return 0;
}
```
2.字符串首字母转换成大写简单实例
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
char input[] = "this is a book";
char output[256] = {'\0'};
int i = 0;
int j = 0;
int len = strlen(input);
for(i = 0; i < len; i++)
{
if(i == 0 || input[i - 1] == ' ')
{
output[j++] = toupper(input[i]);
}
else
{
output[j++] = input[i];
}
}
printf("%s\n", output);
return 0;
}
```