按字母顺序移动单词中的字母

发布于 2025-01-18 10:41:18 字数 240 浏览 1 评论 0原文

我目前正在进行一个项目,涉及移动单词中的字母并进一步研究所有可能的结果。

它应该像这样工作: 例如:

你好世界 ifmmpxpsme jgnnqyqtnf

...等等。 由于我是编程新手,并且对这个问题很困惑,有人可以给我一个关于如何成功管理这个问题的提示吗?我知道这不会那么困难,而且可能与 ASCII 字母表示有关,但是,我不知道如何执行此操作。预先感谢您的帮助。

I'm currently working a project involving shifting letters in a word and further working with all possible outcomes.

It should work like this:
e.g:

helloworld
ifmmpxpsme
jgnnqyqtnf

...and so on.
Since I'm new to programming and quite stuck on this matter, could anyone please give me a hint on how to successfully manage just that ? I know it can't be that difficult and it maybe has something to do with ASCII letter representation, however, I don't know, how to perform this. Thanks in advance for your kind help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

平生欢 2025-01-25 10:41:18

您应该尝试阅读这个网站,它非常清楚地显示了 ASCII 表。

在 ASCII 中,每个字符都有其自己的唯一值,例如 'T' 等于 84't' 等于 116。因此,如果我们将 +1 添加到它们中,它将分别变为 'S''s',并且 的逻辑相同 - 1..

代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

static char *shift(const char *word)
{
    if (!word)
        return NULL;
    char *ptr = calloc(strlen(word) + 1, sizeof(char));
    if(!ptr)
        return NULL;
    for (size_t i = 0; word[i]; i++)
    {
        if (!isalpha(word[i]))
            return NULL;
        ptr[i] = word[i] + 1;
    }
    return ptr;
}

int main(void)
{
    char *word = shift("helloworld");
    while (word)
    {
        puts(word);
        word = shift(word);
    }
    return EXIT_SUCCESS;
}

输出:

ifmmpxpsme
jgnnqyqtnf
khoorzruog
lipps{svph

You should try reading this site, it shows the ASCII table very clearly.

In ASCII very character was its own unique value for example 'T' is equal to 84 and 't' is equal to 116. So, if we add +1 to them it will become 'S' and 's' respectively and same logic goes for -1.

Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

static char *shift(const char *word)
{
    if (!word)
        return NULL;
    char *ptr = calloc(strlen(word) + 1, sizeof(char));
    if(!ptr)
        return NULL;
    for (size_t i = 0; word[i]; i++)
    {
        if (!isalpha(word[i]))
            return NULL;
        ptr[i] = word[i] + 1;
    }
    return ptr;
}

int main(void)
{
    char *word = shift("helloworld");
    while (word)
    {
        puts(word);
        word = shift(word);
    }
    return EXIT_SUCCESS;
}

Output:

ifmmpxpsme
jgnnqyqtnf
khoorzruog
lipps{svph
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文