[英]upper case to lowercase
I just started learning programming, and I started with C, and I am just goofing around and trying to make a function that changes a letters in a string from uppercase to all lowercase, and then return it in an array of lowercase letters...我刚开始学习编程,我从 C 开始,我只是在胡闹,试图制作一个 function,将字符串中的字母从大写变为全小写,然后以小写字母数组返回...
Obviously my code doesn't work.显然我的代码不起作用。 And I'm tired of googling.
我厌倦了谷歌搜索。 can somebody please help me please?
有人可以帮我吗?
Here is what I have up until now:这是我到目前为止所拥有的:
#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>
string lowercase(char inlower[]);
int main(void)
{
string word = get_string("Type in a word: ");
char inlower[strlen(word)];
printf("You typed: %s\n", word);
}
string lowercase(string word)
{
for (int i = 0, len = strlen(word); i < len; i++)
{
inlower[i] = tolower(word[i]);
// printf("%c", inlower[i]);
}
return inlower[];
}
You need to work on word
and return word
, not word[]
.您需要处理
word
并返回word
,而不是word[]
。 inlower
is local to main
and can't be used in lowercase
, unless you pass it along as a parameter along with word
. inlower
是本地的main
并且不能用于lowercase
,除非您将它作为参数与word
一起传递。
Also note that you should cast the char
in your char[]
( string
) to unsigned char
before using it with tolower
.另请注意,您应该将
char
char[]
( string
) 中的 char 转换为unsigned char
,然后再将其与tolower
一起使用。 If char
is signed and the char[]
contains negative values, calling tolower
will cause undefined behavior.如果
char
有符号并且char[]
包含负值,则调用tolower
将导致未定义的行为。
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
string lowercase(string word)
{
for (unsigned i = 0, len = strlen(word); i < len; i++)
{
word[i] = tolower((unsigned char) word[i]);
}
return word;
}
int main(void)
{
string word = get_string("Type in a word: ");
printf("You typed: %s\n", lowercase(word));
}
If you do want to put the lowercase word in inlower
that you've declared in main
, you also need to make it big enough for what you have in word
.如果您确实想将您在
main
中声明的小写单词放入inlower
中,您还需要使其足够大以容纳您在word
中的内容。 strlen(word)
is one char
short since every string must be terminated with a \0
char
. strlen(word)
短一个char
,因为每个字符串都必须以\0
char
结尾。
string lowercase(string inlower, string word)
{
unsigned i = 0;
for (unsigned len = strlen(word); i < len; i++)
{
inlower[i] = tolower((unsigned char) word[i]);
}
inlower[i] = '\0';
return inlower;
}
int main(void)
{
string word = get_string("Type in a word: ");
char inlower[strlen(word) + 1]; // correct size
lowercase(inlower, word); // pass `inlower` in to `lowercase` too
printf("You typed: %s\n"
"In lowercase: %s\n", word, inlower);
}
Alternative version without doing strlen
inside lowercase
too:替代版本也没有在
lowercase
内做strlen
:
string lowercase(string inlower, string word)
{
string dest = inlower;
for(;*word; ++word, ++dest)
{
*dest = tolower((unsigned char) *word);
}
*dest = '\0';
return inlower;
}
try loop by char, use char + 32 to lower.尝试按字符循环,使用 char + 32 降低。
in ASCII, AZ any +32 = az在 ASCII 中,AZ 任意 +32 = az
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.