如何在不使用数组表示法的情况下将字符串从小写转换为大写?

编程入门 行业动态 更新时间:2024-10-28 12:17:04
本文介绍了如何在不使用数组表示法的情况下将字符串从小写转换为大写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

任务是将字符串从小写转换为大写而不使用数组表示法? 我给出的提示是修改字符串指针本身,我以为你无法修改字符串指针。我不知道如何处理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++; }

更多推荐

如何在不使用数组表示法的情况下将字符串从小写转换为大写?

本文发布于:2023-10-25 22:34:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528313.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符串   转换为   情况下   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!