arrays - 关于C中字符串的字符操作问题
问题描述
好的,所以我有一个代码:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int count_characters(char str[]){
/* Your Code Here */
}
int count_words(char str[]){
/* Your Code Here */
}
void capitalize(char str[]){
/* Your Code Here */
}
void invert_capitalization(char str[]){
/* Your Code Here */
}
int main(){
char S1[] = "Raspberry";
char S2[] = " "; //Contains 0 words, 6 characters
char S3[] = "CSc 111 FaLL 2021";
char S4[] = " raspberry pear pineapple banana";
char S5[] = " <-- spaces at the beginning, spaces at the end -->
";
//Make a new array (initialized to an empty string) to use as temporary storage.
char W[1000] = "";
printf("S1: \"%s\"\n", S1 );
printf("Characters: %d\n", count_characters(S1) );
printf("Words: %d\n", count_words(S1) );
strcpy(W, S1);
capitalize(W);
printf("Capitalized: \"%s\"\n", W );
strcpy(W, S1);
invert_capitalization(W);
printf("Inverted Capitalization: \"%s\"\n", W );
printf("\n");
printf("S2: \"%s\"\n", S2 );
printf("Characters: %d\n", count_characters(S2) );
printf("Words: %d\n", count_words(S2) );
strcpy(W, S2);
capitalize(W);
printf("Capitalized: \"%s\"\n", W );
strcpy(W, S2);
invert_capitalization(W);
printf("Inverted Capitalization: \"%s\"\n", W );
printf("\n");
printf("S3: \"%s\"\n", S3);
printf("Characters: %d\n", count_characters(S3) );
printf("Words: %d\n", count_words(S3) );
strcpy(W, S3);
capitalize(W);
printf("Capitalized: \"%s\"\n", W );
strcpy(W, S3);
invert_capitalization(W);
printf("Inverted Capitalization: \"%s\"\n", W );
printf("\n");
printf("S4: \"%s\"\n", S4 );
printf("Characters: %d\n", count_characters(S4) );
printf("Words: %d\n", count_words(S4) );
strcpy(W, S4);
capitalize(W);
printf("Capitalized: \"%s\"\n", W );
strcpy(W, S4);
invert_capitalization(W);
printf("Inverted Capitalization: \"%s\"\n", W );
printf("\n");
printf("S5: \"%s\"\n", S5 );
printf("Characters: %d\n", count_characters(S5) );
printf("Words: %d\n", count_words(S5) );
strcpy(W, S5);
capitalize(W);
printf("Capitalized: \"%s\"\n", W );
strcpy(W, S5);
invert_capitalization(W);
printf("Inverted Capitalization: \"%s\"\n", W );
printf("\n");
return 0;
}
输出为
S1: "Raspberry"
Characters: 9
Words: 1
Capitalized: "Raspberry"
Inverted Capitalization: "rASPBERRY"
S2: " "
Characters: 6
Words: 0
Capitalized: " "
Inverted Capitalization: " "
S3: "CSc 111 FaLL 2021"
Characters: 17
Words: 4
Capitalized: "CSc 111 FaLL 2021"
Inverted Capitalization: "csC 111 fAll 2021"
S4: " raspberry pear pineapple banana"
Characters: 34
Words: 4
Capitalized: " Raspberry Pear Pineapple Banana"
Inverted Capitalization: " RASPBERRY PEAR PINEAPPLE BANANA"
S5: " <-- spaces at the beginning, spaces at the end --> "
Characters: 54
Words: 10
Capitalized: " <-- Spaces At The Beginning, Spaces At The End -
-> "
Inverted Capitalization: " <-- SPACES AT THE BEGINNING, SPACES
AT THE END --> "
我对本教程感到困惑,但老师没有为此提出解决方案,我想问别人我该怎么做?如果是这样,有人可以显示 /* Your Code Here */. 请注意,这不是家庭作业,这是一个我遇到问题的教程
解决方案
count_characters(S1)++
在 C 语言中没有意义。我什至不知道你想用它存档什么。
size_t count_characters(char str[])
{
size_t len;
for (len = 0; str[len] != '\0'; len++){}
return len;
}
int main(void)
{
printf("%zu\n", count_characters("123456"));
}
但正如我所见,您甚至不知道 C 语言的基础知识(for 循环也是无效的)。我建议您在尝试任何编程之前阅读 C 书籍并学习一些基本知识。
推荐阅读
- java - 如何通过Java获取MySQL表中一列不同值的ID?
- ios - Swift 4.1 JWT http 标头承载在服务器中返回 401
- python - TypeError: unhashable type: 'dict' 在 MongoDB 中更新数组时
- json - 从 JSON 中解析变量,然后使用 curl 的变量在几个 url 上循环
- c# - 带有 if else 条件和 && 逻辑运算符的复选框
- visual-studio - 如何在 Visual Studio 2017 中更改断点的位置
- docker - Zookeeper 服务发现和 Docker
- javascript - Laravel 中的 ajax 419 错误
- ruby-on-rails - 将类动态解析为全局范围
- android - 转到该片段后,底部导航栏仍处于选中状态