there is a dog 转换成 dog a is there

参考:https://blog.csdn.net/dcjhyn/article/details/78073310?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162079159916780269830573%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=162079159916780269830573&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v29-2-78073310.first_rank_v2_pc_rank_v29&utm_term=there+is+a+dog

#include <iostream>
#include <stack>
#include<stdlib.h>
#include<string.h>
using namespace std;

char* _reverse(char *str)
{

    stack<char> v1;
    stack<char> v2;
	
	//求字符串长度
    int length = strlen(str);
    char *temp = str;


	//遍历字符串
    for (int i = 0; i < length+1; i++)
    {
    	//不为空,不为空格则压栈
        if(temp[i] != ' ' && temp[i] != '\0')
            v1.push(temp[i]);
        //如果为空或者,空格
        
        else{
        	//如果v1栈里有元素,则出栈,放入v2
            while (v1.size())
            {
                char ch = v1.top();
                v1.pop();
                v2.push(ch);
            }
            //如果i没到末尾,则对v2压入一个空格
            if (i < length-1)
                v2.push(' ');
        }
    }
	//把栈v2出栈到str
    for (int i = 0; i < length; i++)
    {
        str[i] = v2.top();
        v2.pop();
    }
    return str;
}

int main()
{
    char str[] = "this is a pre job";
    _reverse(str);
    cout << str <<endl;
    return 0;
}

总结:v1栈类似寻路,切割字符串。
v1会把字符串逆序,故v2的再次入栈则能将字符串的顺序回正

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的 Java 代码实现,可以对纯英文字母构成的明文进行加密和解密操作: ```java public class EncryptDecryptTool { private static final int LETTER_COUNT = 26; /** * 加密方法 * * @param plaintext 明文 * @param n 偏移量 * @return 密文 */ public static String encrypt(String plaintext, int n) { StringBuilder ciphertext = new StringBuilder(); for (char c : plaintext.toCharArray()) { if (Character.isLetter(c)) { int base = Character.isUpperCase(c) ? 'A' : 'a'; int index = (c - base + n) % LETTER_COUNT; ciphertext.append((char) (base + index)); } else { ciphertext.append(c); } } return ciphertext.toString(); } /** * 解密方法 * * @param ciphertext 密文 * @param n 偏移量 * @return 明文 */ public static String decrypt(String ciphertext, int n) { StringBuilder plaintext = new StringBuilder(); for (char c : ciphertext.toCharArray()) { if (Character.isLetter(c)) { int base = Character.isUpperCase(c) ? 'A' : 'a'; int index = (c - base - n + LETTER_COUNT) % LETTER_COUNT; plaintext.append((char) (base + index)); } else { plaintext.append(c); } } return plaintext.toString(); } } ``` 这个工具类有两个方法,一个是 `encrypt()` 方法用于加密,另一个是 `decrypt()` 方法用于解密。这两个方法都接受两个参数,第一个参数是要加密或解密的字符串,第二个参数是偏移量,即对应的 n 值。 在加密方法中,我们首先遍历明文中的每个字符,如果这个字符是英文字母,就将其转换为对应的 ASCII 码值,然后将 ASCII 码值减去相应的基准值(A 或 a),再加上偏移量 n,最后将结果对 26 取模得到新的 ASCII 码值,并将其转换为对应的字符。如果这个字符不是英文字母,则直接将其添加到密文字符串中。 在解密方法中,我们也是遍历密文中的每个字符,如果这个字符是英文字母,就将其转换为对应的 ASCII 码值,然后将 ASCII 码值减去相应的基准值(A 或 a),再减去偏移量 n,最后将结果对 26 取模得到新的 ASCII 码值,并将其转换为对应的字符。如果这个字符不是英文字母,则直接将其添加到明文字符串中。 下面是一个示例代码,演示了如何使用这个工具类进行加密和解密操作: ```java public class Main { public static void main(String[] args) { String plaintext = "There is a dog."; int n = 1; String ciphertext = EncryptDecryptTool.encrypt(plaintext, n); System.out.println("加密后的密文:" + ciphertext); String decryptedText = EncryptDecryptTool.decrypt(ciphertext, n); System.out.println("解密后的明文:" + decryptedText); } } ``` 在这个示例中,我们将明文设置为 "There is a dog.",偏移量 n 设置为 1。运行这个示例,输出如下: ``` 加密后的密文:Uifsf jt b eph. 解密后的明文:There is a dog. ``` 可以看到,加密后的密文为 "Uifsf jt b eph.",解密后得到的明文与原始明文相同,即 "There is a dog."。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值