且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

toUpper和指向字符串的指针

更新时间:2023-10-31 14:40:10

文章< 11 ** ********************@k79g2000hse.googlegroups .com&gt ;,
ba *************** @ gmail.com 说...

我的函数应该接受一个指向字符串的指针作为它的参数。并且

它应该将每个字符转换为大写字母。我知道我需要做什么,只是我的语法完全没有问题。


void strUpper(char * myStr){



这可能与你看到的问题无关,但是这个名字是

(以及任何其他以''str''开头的'保留供

实现使用。


int i;


strcpy( * myStr的);



这可能是你最大的问题。如果你打算调用strcpy,

你需要提供两个参数。幸运的是,你根本不需要

完全使用strcpy,所以你可以完全删除它。


for( i = 0; myStr [i]; i ++){

myStr [i] = toupper(myStr [i]);



这通常有效,但可以给出未定义的行为。你通常需要这样的东西:

myStr [i] = toupper((unsigned char)myStr [i]);


-

后来,

杰瑞。


宇宙是自己想象的虚构。


9月22日上午11:59,Jerry Coffin< jcof ... @ taeus.comwrote:

文章< 1190478693.852512.115 ... @ k79g2000hse.googlegroups .com&gt ;,

baronessfrostb ... @ gmail.com说...

for(i = 0; myStr [i]; i ++){

myStr [i] = toupper(myStr [i]);



这通常有效,但可以提供未定义的行为。你通常需要这样的东西:

myStr [i] = toupper((unsigned char)myStr [i]);



我真的不明白这句话。 toupper()是一个函数

采用int参数。演员如何改变任何东西,更不用说
一个未签名的字符?


gaga schrieb:

我的函数应该接受一个指向字符串的指针作为它的参数。并且

它应该将每个字符转换为大写字母。我知道我需要做什么,只是我的语法完全没有问题。


void strUpper(char * myStr){

int i;


strcpy(* myStr);


for(i = 0; myStr [i]; i ++) {

myStr [i] = toupper(myStr [i]);

}

}


帮忙?



你好,


这就是我写这个函数的方式:


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?

:)

In article <11**********************@k79g2000hse.googlegroups .com>,
ba***************@gmail.com says...
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) {

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.

int i;

strcpy (*myStr);

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.

for(i=0; myStr[i]; i++) {
myStr[i] = toupper(myStr[i]);

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.


On Sep 22, 11:59 am, Jerry Coffin <jcof...@taeus.comwrote:
In article <1190478693.852512.115...@k79g2000hse.googlegroups .com>,
baronessfrostb...@gmail.com says...
for(i=0; myStr[i]; i++) {
myStr[i] = toupper(myStr[i]);


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?


gaga schrieb:
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?

Hello,

this is the way I would write this function:

void strUpper(char *str)
{
while (*str) {
*str = toupper(*(str++));
}
}

Cya,
Daniel Kay