What is the output of the following function? .C void toUpper(char str[]){ while(*str) if(*str >= 'A' && *str <= 'Z') (*str++)+= 32; }
时间: 2024-03-07 19:52:16 浏览: 9
这个函数的作用是将字符串中的大写字母转换成小写字母,具体实现是通过while循环遍历每一个字符,然后判断是否为大写字母(即ASCII码值在65~90之间),如果是,则将其ASCII码值加上32,即转换成对应的小写字母。因此,如果在此函数之后输出字符数组的内容,将会得到每个大写字母被转换为小写字母后的字符串。
相关问题
#include <ctype.h> #include <string.h> /********found********/ include <stdio.h> /********found********/ void upfst(char p) { int k = 0; for (; *p; p++) if (k) { if (*p == ' ') k = 0; } else { if (*p != ' ') { k = 1; *p = toupper(*p); } } } void main() { char chrstr[81]; gets(chrstr); upfst(chrstr); printf("\n%s", chrstr); }
这段代码的功能是将输入的字符串中的每个单词的首字母转换成大写字母,并输出转换后的字符串。
具体实现方式是通过指针遍历字符串中的每一个字符,如果当前字符前面是空格且当前字符不是空格,则将其转换成大写字母。最终输出转换后的字符串。
需要注意的是,在第一行中缺少了两个头文件的引用,应该添加 `#include <stdio.h>` 和 `#include <string.h>`。另外,在第 3 行中的参数应该改为 `char *p`,否则会产生编译错误。
Write a C function void title(const char str[]) that takes a zero-terminated string as an input, and prints it such that every word of the string is printed with its first letter capitalized (upper-case). Here, a word is defined as a sequence of non-space character separated by one or more space characters. For instance, if the input is gone with the wind, the function should print: Gone With The Wind
Certainly! Here's the updated implementation of the `title` function in C:
```c
#include <stdio.h>
#include <ctype.h>
void title(const char str[]) {
int i = 0;
int capitalize = 1;
while (str[i] != '\0') {
// Skip leading spaces
while (str[i] == ' ') {
i++;
}
// Capitalize the first letter of the word
if (capitalize) {
printf("%c", toupper(str[i]));
capitalize = 0;
} else {
printf("%c", str[i]);
}
// Move to the next character
i++;
// Check if the current word has ended
if (str[i] == ' ' || str[i] == '\0') {
// Skip consecutive spaces
while (str[i] == ' ') {
i++;
}
// Move to the next line
printf(" ");
capitalize = 1;
}
}
}
```
You can use this updated `title` function to print a string such that every word is printed with its first letter capitalized. For example:
```c
int main() {
const char str[] = "gone with the wind";
title(str);
return 0;
}
```
Output:
```
Gone With The Wind
```
Please note that consecutive spaces are treated as a single separator between words.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)