c语言问题,字符串?

如何实现任意关键词随机替换颜表情,关键词 = {"am", "b", "tdm", ...},颜表情 = {"˚*̥(∗*⁰͈꒨⁰͈)*̥哇哦~", "(♡ര‿ര)给我康康", ...},C/C++都可, (人 •͈ᴗ•͈) (୨୧•͈ᴗ•͈)◞ᵗʱᵃᵑᵏઽ*♡

该代码使用了STL中的vector、map等容器以及C++11中引入的random库,实现了任意关键词随机替换颜表情的功能。具体实现思路是先将关键词和颜表情建立映射关系,然后使用find函数在文本中查找关键词,找到之后将其替换为对应的颜表情。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2023-03-19
这个程序使用了一个自定义的函数 replaceKeywordsWithEmoticons,它会遍历输入字符串中的每个字符,并检查是否存在一个关键词。如果找到关键词,它会将其替换成一个随机的颜文字表情。如果没有找到关键词,它会将该字符复制到输出字符串中。程序使用 strncmp 函数来比较字符串,这比 strstr 函数更加高效。程序中还使用了宏定义和数组来简化代码。

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

#define NUM_KEYWORDS 6
#define MAX_KEYWORD_LEN 10
#define MAX_EMOTICON_LEN 10

char keywords[NUM_KEYWORDS][MAX_KEYWORD_LEN] = {"happy", "sad", "love", "angry", "excited", "tired"};
char emoticons[NUM_KEYWORDS][MAX_EMOTICON_LEN] = {":-)", ":-((", "<3", ">:(", ":-D", ":-|"};

void replaceKeywordsWithEmoticons(char *input, char *output) {
// Initialize the random number generator
srand(time(NULL));

int inputLen = strlen(input);
int outputLen = 0;
int i = 0;

while (i < inputLen) {
int keywordFound = 0;
int j = 0;

// Check if a keyword is found
for (j = 0; j < NUM_KEYWORDS; j++) {
if (strncmp(&input[i], keywords[j], strlen(keywords[j])) == 0) {
// A keyword is found, replace it with a random emoticon
int emoticonIndex = rand() % NUM_KEYWORDS;
strcpy(&output[outputLen], emoticons[emoticonIndex]);
outputLen += strlen(emoticons[emoticonIndex]);
i += strlen(keywords[j]);
keywordFound = 1;
break;
}
}

// If no keyword is found, copy the character as it is
if (!keywordFound) {
output[outputLen] = input[i];
outputLen++;
i++;
}
}

// Null terminate the output string
output[outputLen] = '\0';
}

int main() {
char input[1000];
char output[1000];

printf("Please enter your text: ");
fgets(input, sizeof(input), stdin);

replaceKeywordsWithEmoticons(input, output);

printf("Output: %s\n", output);

return 0;
}追问

很好啊,就是如果关键字里有另一个关键字,能否将长一些的替换呢シ

第2个回答  2023-03-20
你好!很高兴为你解答:字符串

c 语言没有专门用于存储字符串的变量类型,字符串都被存储在 char 类型的数组中。数组由连续的存储单元组成,字符串中的字符被存储在相邻的存储单元中,每个单元存储一个字符,c 语言用数组末尾的字符 \o 标记字符串的结束。
c 中的字符串一定是以空字符结束,这意味着数组的容量必须至少比待存储字符串的字符数多 1,以上图中有14个存储单元,却只能存储13个字符,剩下一个字节留给空字符。
字符串常量

用双引号括起来的内容称为字符串的字面量,也叫做字符串常量。双引号中的字符和编译器自动加入末尾的 \0 字符,都作为字符串存储在内存中,如:” hello world ! ” 、” good morning “ 、 “ i am a student ” 都是字符串字面量。另外注意一点,你的字符串字面量之间如果没有间隔,或用空白字符分隔,c 会自动将它们串联起来。
#include<stdio.h>
int main(void)
{
char a[]="hello ""world " "!";
char b[]="hello world !";

printf("a[]=%s b[]=%s\n",a,b);

return 0;

}

它们的输出是相同的:

字符串变量

我们字符串变量一般有两种方法创建,指针或者是数组
#include<stdio.h>
int main(void)
{
char a[]="hello world !"; //数组
char* b=" hello world !"; //指针

}
数组形式( a [ ] )在计算机的内存中分配为一个内含14个元素的数组,每个元素初始化为字符串字面量对应的字符。通常,字符串都作为可执行文件的一部分存储在数据段中。当把程序载入内存时,也载入了程序中的字符串。字符串存储在静态存储区中,但是,程序在开始运行时才会为该数组分配内存。此时才将字符串拷贝到数组中,此时字符串有两份,一个是在静态内存中的字符串字面量,另一个是存储在 a[ ] 数组中的字符串。
文字有点晦涩,我们结合图看看

指针形式( *b )也使得编译器为字符串在静态存储区预留14个元素的空间。一旦开始执行程序,它会把字符串地址存储在指针变量中。该变量指向该字符串的首字符。

其实不用关心太多,你只要知道,初始化数组把静态存储区的字符串拷贝到数组中,你的数组得到的只是它的副本,而初始化指针只把字符串的地址拷贝给指针,你得的是静态存储区字符串的地址。
数组和指针的选择:

那么我们用数组还是用指针呢?它们一个存着地址一个存着副本,所以肯定是有差别的对吧,我们精简的了解一下,因为书上真的很复杂写了一大堆的例子,我们提取一下精华:
字符串字面量是 const 的数据(常量),由于 *b 指向这个 const 数据,所以实际上 b 是 const char * b ,但是由于历史的原因,编译器接受不带 const 的写法,这意味这不能用 b 改变它指向的数据,但是仍然可以改变 b 的值,就是让 b 指向别的位置。如果把一个字符串字面量拷贝给一个数组,就可以随意改变数据,除非把数组声明为 const,数组元素是变量,但是数组名不是变量。
#include<stdio.h>
int main(void)
{
char a[]="hello world !"; //数组
a[0]='H';
printf("a[]=%s\n",a);

char* b="hello world !"; //指针
b[0]='H';
printf("*s=%s\n",b);

return 0;

}

我用的是 Dev -C++ ,在我电脑上编译它过了,但是这个指针形式并没有输出,这就很好的证明了上面说的对吧,你用指针你不能修改字符串字面量(字符串常量),因为你的指针是 const char *,所以你可以在把指针初始化为字符串字面量时使用 const 限定符:const char * b,书上推荐我们这样用,这也是比较安全的做法,看见 const 我们就知道不能去修改它。总之,初始化数组把静态存储区的字符串拷贝到数组中,而初始化指针只把字符串的地址拷贝给指针,如何选择取决于你想用程序做什么,如果你要去构造一个字符串数组,如果你要处理一个字符串指针。
补充

再说一下指针,就是当你用多个指针初始化的字符串字面量是一样的,它们得到的是同一个地址,编译器可以使用内存中的一个副本表示所以完全相同的字符串字面量。
#include<stdio.h>
int main(void)
{
char* a="hello world !";
char* b="hello world !";
char* c="hello world !";

printf("a= %p\n",a);
printf("b= %p\n",b);
printf("c= %p\n",a);

return 0;

}

你看它们的地址是不是一模一样

所以你有没有懂懂一点点这个(你初始化指针是 const ),如果允许你修改的话:
char* a="hello world !";
char* b="hello world !";
char* c="hello world !";

printf("a= %p\n",a);
a[0]='y';
printf("b= %p\n",b);
printf("c= %p\n",a);
那后面两个输出都变成“ yello world ”,如果编译器使用这种单次副本表示法,并允许 a[ 0 ] 修改 ‘ h ’,那将影响所有使用该字符串的代码。而把数组初始化为字符串字面量却不会导致类似问题,因为数组获得的原始字符串的副本。总之,如果你打算修改字符串,就不要用指针去初始化。
第3个回答  2023-03-20
可以使用C语言中的rand()函数生成随机数,将生成的随机数对关键词数组和颜表情数组进行下标索引,从而实现关键词和颜表情的随机匹配替换。
以下是一个简单的实现示例:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int main() {
srand(time(NULL)); // 设置随机数种子
char* keywords[] = {"am", "b", "tdm"}; // 关键词数组
char* expressions[] = {"˚*̥(∗*⁰͈꒨⁰͈)*̥哇哦~", "(♡ര‿ര)给我康康", "ʕ•̀ω•́ʔ✧"}; // 颜表情数组
int num_keywords = sizeof(keywords) / sizeof(keywords[0]); // 关键词数组长度
int num_expressions = sizeof(expressions) / sizeof(expressions[0]); // 颜表情数组长度
char sentence[1000] = "am is b, tdm is b too."; // 要处理的句子
char* p = sentence; // 句子指针
while (*p != '\0') {
int index = -1;
if (*p >= 'a' && *p <= 'z') { // 如果当前字符是小写字母
char word[100] = {0};
int i = 0;
while (*p != ' ' && *p != '\0') { // 找到关键词
word[i++] = *p++;
}
for (int j = 0; j < num_keywords; j++) { // 在关键词数组中查找匹配
if (strcmp(keywords[j], word) == 0) {
index = j;
break;
}
}
if (index != -1) { // 如果匹配到关键词
int expr_index = rand() % num_expressions; // 生成随机数
printf("%s", expressions[expr_index]); // 输出对应的颜表情
continue;
}
}
printf("%c", *p++); // 如果当前字符不是关键词,直接输出
}
return 0;
}
在上述示例中,首先定义了关键词数组和颜表情数组,然后使用rand()函数生成随机数,在句子中查找关键词,如果匹配到关键词,则根据生成的随机数输出对应的颜表情,否则直接输出当前字符。
第4个回答  2023-03-20
以下是一个简单的C++程序,用于实现任意关键词随机替换颜表情:
```

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()

{
// 定义关键词和颜表情
vector<string> keywords = {"am", "b", "tdm"};
vector<string> emojis = {"˚*̥(∗*⁰͈꒨⁰͈)*̥哇哦~", "(♡ര‿ര)给我康康", "(人 •͈ᴗ•͈) (୨୧•͈ᴗ•͈)◞ᵗʱᵃᵑᵏઽ*♡"};
// 创建关键词到颜表情的映射
map<string, string> keywordToEmoji;
for (int i = 0; i < keywords.size(); ++i) {
keywordToEmoji[keywords[i]] = emojis[i];
}
// 读取输入字符串
string input;
getline(cin, input);
// 替换关键词为随机颜表情
string output;
string delimiter = " ";
size_t pos = 0;
string token;
while ((pos = input.find(delimiter)) != string::npos) {
token = input.substr(0, pos);
if (keywordToEmoji.find(token) != keywordToEmoji.end()) {
output += keywordToEmoji[token] + " ";
} else {
output += token + " ";
}
input.erase(0, pos + delimiter.length());
}
if (keywordToEmoji.find(input) != keywordToEmoji.end()) {
output += keywordToEmoji[input];
} else {
output += input;
}
// 输出结果
cout << output << endl;
return 0;
}
```
这个程序首先定义了关键词和颜表情的向量,然后创建了一个关键词到颜表情的映射。接着,程序读取输入字符串,并根据空格分隔输入字符串,查找关键词并将其替换为随机颜表情。最后,程序输出替换后的字符串。

在程序中,我们使用了C++的STL库中的vector、map和string类来实现操作。在实现随机替换的功能时,我们使用了C++的随机数生成函数srand()和rand()来随机选择颜表情。本回答被提问者采纳