You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言加解密程序问题:如何仅处理括号内字符串?

解决C语言程序仅加密/解密括号内内容的问题

问题根源

原代码存在两个核心问题:

  • 固定跳过8个字符(假设encrypt后有一个空格),但实际输入格式为encrypt(hi)(无空格),导致无法定位到左括号,跳过了括号处理逻辑,直接加密/解密了包含右括号及后续的所有内容。
  • 未适配命令与括号间有无空格的情况,容错性差。

解决方案

修正命令解析逻辑,动态定位括号位置,确保仅处理括号内的内容:

  1. 先定位到encrypt/decrypt命令的结尾,再跳过所有空白字符(适配有无空格的输入)。
  2. 找到左括号后,定位对应的右括号,将其替换为字符串结束符'\0',截断多余内容。
  3. 对无左括号、无右括号的情况做容错处理,避免错误输出。

修正后的完整代码

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

void encrypt(char text[]);
void decrypt(char text[]);

int main() {
    char input[1000];
    while (1) {
        printf("Enter command: ");
        fgets(input, sizeof(input), stdin);
        // 移除fgets读取的换行符
        input[strcspn(input, "\n")] = '\0';

        if (strncmp(input, "encrypt", 7) == 0) {
            char *cmdEnd = input + 7;
            // 跳过命令后的所有空白字符
            while (*cmdEnd != '\0' && isspace((unsigned char)*cmdEnd)) {
                cmdEnd++;
            }
            char *textToEncrypt = cmdEnd;

            if (*textToEncrypt == '(') {
                textToEncrypt++;
                char *closingParenthesis = strchr(textToEncrypt, ')');
                if (closingParenthesis) {
                    *closingParenthesis = '\0'; // 截断到右括号,仅保留括号内内容
                } else {
                    textToEncrypt[0] = '\0'; // 无右括号时清空内容
                }
            } else {
                textToEncrypt[0] = '\0'; // 无左括号时清空内容
            }

            encrypt(textToEncrypt);
        } else if (strncmp(input, "decrypt", 7) == 0) {
            char *cmdEnd = input + 7;
            while (*cmdEnd != '\0' && isspace((unsigned char)*cmdEnd)) {
                cmdEnd++;
            }
            char *textToDecrypt = cmdEnd;

            if (*textToDecrypt == '(') {
                textToDecrypt++;
                char *closingParenthesis = strchr(textToDecrypt, ')');
                if (closingParenthesis) {
                    *closingParenthesis = '\0';
                } else {
                    textToDecrypt[0] = '\0';
                }
            } else {
                textToDecrypt[0] = '\0';
            }

            decrypt(textToDecrypt);
        }
    }
    return 0;
}

void encrypt(char text[]) {
    char encryptedChar;
    char array[1000] = "";
    // 用text[i] != '\0'替代strlen,提升循环效率
    for (int i = 0; text[i] != '\0'; i++) {
        encryptedChar = text[i] + 5;
        strncat(array, &encryptedChar, 1);
    }
    printf("%s\n", array);
}

void decrypt(char text[]) {
    char decryptedChar;
    char array[1000] = "";
    for (int i = 0; text[i] != '\0'; i++) {
        decryptedChar = text[i] - 5;
        strncat(array, &decryptedChar, 1);
    }
    printf("%s\n", array);
}

关键修正点

  • 动态适配命令与括号间的空白字符,支持encrypt(hi)和encrypt (hi)两种输入格式。
  • 强制截断到右括号,确保仅处理括号内的目标内容。
  • 优化循环条件,避免重复调用strlen带来的性能损耗。
  • 增加容错处理,避免非法输入导致的错误输出。

内容的提问来源于stack exchange,提问作者O D E

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 10:15:20