对“i love china”进行逆序输出,输出格式:“china love i”

int main(int argc, char const *argv[])
{
    char a[32] = "i love China";
    char t;
    char *p = a;
    char *q = a + strlen(a) - 1;
    char *m = NULL;
    //整体倒过来
    while (p < q)
    {
        t = *p;
        *p = *q;
        *q = t;
        p++;
        q--;
    }
    printf("%s\n", a);
    //把每个单词倒过来
    p = q = a;
    while (*p != '\0')
    {
        while (*p ==' ')
            p++;
        q = p; //找单词结尾
        while (*q != ' ' && *q != '\0')
            q++;
        m = q; //暂存空格和\0地址
        q--;
        while (p < q)
        {
            t = *p;
            *p = *q;
            *q = t;
            p++;
            q--;
        }
        p = m;
    }
    printf("%s\n", a);

    return 0;
}

约瑟夫问题(Josephus Problem)是一个著名的数学问题,涉及到一组人围成一圈,按照指定的步长跳过一定数量的人,然后从剩下的人中再次开始重复此过程,直到最后只剩下一个人。在加密和解密的上下文中,我们可以将这个过程稍作修改,将其应用到字符的重新排列上。 为了简化问题,我们假设给定的英语语句是按照空格分隔的单词序列,而加密和解密的n和m值分别代表单词数量和跳过的单词数。这里提供一个简化的处理方式,不考虑空格和标点符号,只对单词进行加密解密。 下面是一个C语言实现约瑟夫问题加密和解密的简单示例代码: ```c #include <stdio.h> #include <string.h> #include <stdlib.h> // 函数用于分割字符串为单词数组 char** splitWords(const char* str, int* wordCount) { int length = strlen(str); int start = 0, end = 0; int count = 0; char** words = (char**)malloc(sizeof(char*) * (length + 1)); for (int i = 0; i <= length; i++) { if (str[i] == ' ' || str[i] == '\0') { if (i > start) { end = i; words[count] = (char*)malloc(sizeof(char) * (end - start + 1)); strncpy(words[count], str + start, end - start); words[count][end - start] = '\0'; count++; start = i + 1; } } } *wordCount = count; return words; } // 函数用于约瑟夫问题加密 void josephusEncrypt(char** words, int wordCount, int n, int m, char* output) { int index = 0; for (int i = 0; i < n; i++) { index = (index + m - 1) % wordCount; // 逆序取单词 strcat(output, words[index]); if (i < n - 1) { strcat(output, " "); // 在单词之间添加空格 } } } // 函数用于约瑟夫问题解密 void josephusDecrypt(char* encrypted, int n, int m, char* output) { int wordCount = 0; char** words = splitWords(encrypted, &wordCount); int index = 0; for (int i = 0; i < n; i++) { index = (index + m - 1) % wordCount; // 逆序放回单词 strcat(output, words[index]); if (i < n - 1) { strcat(output, " "); // 在单词之间添加空格 } // 将已处理的单词从原数组中移除 free(words[index]); for (int j = index; j < wordCount - 1; j++) { words[j] = words[j + 1]; } wordCount--; } free(words); } int main() { const char* sentence = "I love China"; int n = 12, m = 4; // 加密 char encrypted[256] = {0}; char** words = splitWords(sentence, &n); josephusEncrypt(words, n, n, m, encrypted); printf("Encrypted: %s\n", encrypted); for (int i = 0; i < n; i++) { free(words[i]); } free(words); // 解密 char decrypted[256] = {0}; josephusDecrypt(encrypted, n, m, decrypted); printf("Decrypted: %s\n", decrypted); return 0; } ``` 这段代码首先定义了两个函数`splitWords`用于分割原始句子为单词数组,`josephusEncrypt`和`josephusDecrypt`用于执行加密和解密操作。最后在`main`函数中,我们对给定的句子进行加密和解密操作,并打印结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值