如何在不使用数组表示法的情况下将字符串从小写转换为大写? [英] How do I convert a string from lowercase to uppercase without using array notation?

查看:119
本文介绍了如何在不使用数组表示法的情况下将字符串从小写转换为大写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任务是将字符串从小写转换为大写而不使用数组表示法?



我给出的提示是修改字符串指针本身,我以为你无法修改字符串指针。我不知道如何处理while循环。



任何帮助都会很棒。



我尝试过:



The task is to convert a string from lowercase to uppercase without using array notation?

The hint I was given was "modify the string pointer itself", I thought you couldn't modify a string pointer. I'm not sure how to work the while loop.

Any help would be great.

What I have tried:

int main(void)
{

  uppercase_no_array("reverse");

  return(0);
}

void uppercase_no_array(char* input)
{

  char* copy = malloc(sizeof(char) * strlen(input));
  strcpy(copy, input);

  printf("%s", copy);

  while(copy != '\0')
  {

   
  }
return;

}

推荐答案

分配内存时,复制指向空间中的第一个字符。然后使用它将输入复制到输出区域 - 根本不需要这样做,只需使用已有的两个指针: copy 输入

When you allocate the Memory, copy points at the first character in the space. You then use that to copy the input to an output area - you don't really need to do that at all, just use the two pointers you already have: copy and input
char* uppercase_no_array(char* input)
{
  char* mem = malloc(sizeof(char) * (strlen(input) + 1));
  char* copy = mem;
  char c;
  do
  {
  c = *input++;
  ... uppercase it here ...
  *copy++ = c;
  } while(c != '\0')
  return mem;
}


Quote:

我给出的提示是修改字符串指针本身

The hint I was given was "modify the string pointer itself"

从我的角度来看,这意味着你可以使用指针就地修改字符串:

From my point of view that means that you can modify the string in-place using a pointer:

char *p;
p = input_string;
while (*p)
{
    /* char_to_upper() has to be implemented */
    *p = char_to_upper(*p);
    p++;
}


这篇关于如何在不使用数组表示法的情况下将字符串从小写转换为大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆