问题描述
我的函数应该接受一个指向字符串的指针作为它的参数。并且
它应该将每个字符转换为大写字母。我知道我需要做什么,只是我的语法完全没有问题。
void strUpper(char * myStr){
int i;
strcpy(* myStr);
for(i = 0; myStr [i]; i ++) {
myStr [i] = toupper(myStr [i]);
}
}
help?
:)
这可能与你看到的问题无关,但是这个名字是
(以及任何其他以''str''开头的'保留供
实现使用。
这可能是你最大的问题。如果你打算调用strcpy,
你需要提供两个参数。幸运的是,你根本不需要
完全使用strcpy,所以你可以完全删除它。
这通常有效,但可以给出未定义的行为。你通常需要这样的东西:
myStr [i] = toupper((unsigned char)myStr [i]);
-
后来,
杰瑞。
宇宙是自己想象的虚构。
这通常有效,但可以提供未定义的行为。你通常需要这样的东西:
myStr [i] = toupper((unsigned char)myStr [i]);
我真的不明白这句话。 toupper()是一个函数
采用int参数。演员如何改变任何东西,更不用说
一个未签名的字符?
你好,
这就是我写这个函数的方式:
void strUpper(char * str)
{
while(* str){
* str = toupper(*(str ++)) ;
}
}
Cya,
Daniel Kay
my function should accept a pointer to a string as its argument. and
it should convert each charater to an uppercase letter. I know what i
need to do, its just my syntax is all out of whack.
void strUpper (char *myStr) {
int i;
strcpy (*myStr);
for(i=0; myStr[i]; i++) {
myStr[i] = toupper(myStr[i]);
}
}
help?
:)
It''s probably not related to the problem you''re seeing, but this name
(and any other starting with ''str'') is reserved for use by the
implementation.
This is probably your biggest problem. If you''re going to call strcpy,
you need to supply two arguments. Fortunately, you don''t seem to need to
use strcpy at all, so you can just delete this entirely.
This will usually work, but can give undefined behavior. You normally
want something like:
myStr[i] = toupper((unsigned char)myStr[i]);
--
Later,
Jerry.
The universe is a figment of its own imagination.
This will usually work, but can give undefined behavior. You normally
want something like:
myStr[i] = toupper((unsigned char)myStr[i]);
I don''t really understand this statement. toupper() is a function
taking an int parameter. How would a cast change anything, much less
one to unsigned char?
Hello,
this is the way I would write this function:
void strUpper(char *str)
{
while (*str) {
*str = toupper(*(str++));
}
}
Cya,
Daniel Kay
这篇关于toUpper和指向字符串的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!