从键盘输入字符串1和字符串2(串长不超过50个字符),将在字符串1中出现,但未在字符串2中出现的字符组成一个新的字符串输出,不去掉未出现过的重复字符。
基于new bing的参考学习:
#include <stdio.h>
#include <string.h>
#define MAX_LEN 50
int main()
{
char str1[MAX_LEN+1], str2[MAX_LEN+1], result[MAX_LEN+1];
int i, j, k, flag;
printf("请输入字符串1:");
gets(str1);
printf("请输入字符串2:");
gets(str2);
k = 0; // 记录新字符串result的长度
for (i = 0; i < strlen(str1); i++) {
flag = 0; // 初始化为未出现过
for (j = 0; j < strlen(str2); j++) {
if (str1[i] == str2[j]) {
flag = 1; // 出现过
break;
}
}
if (!flag) { // 如果在str2中未出现过
result[k++] = str1[i]; // 存入result中
}
}
// 添加结束符
result[k] = '\0';
printf("新的字符串为:%s\n", result);
return 0;
}
(有待思考)#define _CRT_SECURE_NO_WARNINGS
#include
#include
#includeusing namespace std;
void index(char* s1, char* s2)
{
int len ,max = 0;
char temp[50];
while (*s1)
{
while (*s1 == ’ ’ && *s1 != ‘\0’) s1++;//过滤空格;
len = 0;
while (*s1 != ' ' && *s1 != '\0')
{
*(temp + len) = *s1;//不能用*temp=*s1,why?
len++;
s1++;
}
*(temp + len) = '\0';//注意这种方式。
if (len > max)
{
max = len;
strcpy(s2, temp);
}
if (*s1 == '\0') break;
}}
int main()
{
char s1[50],s2[50];cin.get(s1,50); index(s1, s2);
cout << “s2:” << s2;
}用队列的方法输出杨辉三角:#include
using namespace std;
const int maxsize = 100;
typedef struct {
int Q[maxsize];//存放数据
int front, rear;
}sequeue;
sequeue qu;
void setkong(sequeue& qq)
{
qq.front = 0;
qq.rear = 0;
}//置队空
void rudui(sequeue& qq, int x)
{
if (qq.front == (qq.rear + 1) % maxsize)
cout << “overflow\n”;
else
{
qq.Q[qq.rear] = x;
qq.rear = (qq.rear + 1) % maxsize;
}
}
void chudui(sequeue &qq, int& x)
{
if (qq.front == qq.rear)
{
cout << “underflow\n”;
}
else
{
x = qq.Q[qq.front];
qq.front = (qq.front + 1) % maxsize;
}
}
void getfront(sequeue qq, int &x)//读取队头元素
{
if (qq.front == qq.rear)
{
cout << “error!\n”;
}
else
{
x = qq.Q[qq.front];
}
}
int empty(sequeue qq)//判断队列是否为空
{
if (qq.front == qq.rear)
return 1;
else
return 0;
}
void yanghui(int n,sequeue qu)
{
int i, j,s,t;
setkong(qu);
rudui(qu, 1); rudui(qu, 1);
cout << endl;
cout.width(4); cout << 1;
cout.width(4); cout << 1<<endl;
for (i = 2; i <= n; i++)//生成并输出杨辉三角第i~n行的数据
{
rudui(qu, 1);
cout.width(4); cout << 1;
chudui(qu, s);
for (j = 2; j <= i; j++)//处理第i行中间的各数据
{
chudui(qu, t);
rudui(qu, s + t);
cout.width(4); cout << s + t;
s = t;
}
rudui(qu, 1);
cout.width(4); cout << 1<<endl;
}
cout << endl;
}
int main()
{
int m;
cin >> m;
yanghui(m, qu);
}
针对这个问题,可以通过以下步骤解决:
string1
和string2
,并使用fgets
函数从键盘输入两个字符串:char string1[51], string2[51];
printf("请输入字符串1:");
fgets(string1, 51, stdin);
printf("请输入字符串2:");
fgets(string2, 51, stdin);
result
,并将其全部初始化为0:char result[256] = {0};
for(int i = 0; i < strlen(string1); i++) {
for(int j = 0; j < strlen(string2); j++) {
if(string1[i] == string2[j]) break;
if(j == strlen(string2)-1) result[string1[i]] = 1;
}
}
其中,result数组中存储的是字符是否出现过的信息,如果字符出现过,则将其对应的ASCII码位置上的值设为1。由于ASCII码共有256个,所以数组的大小设为256。
char new_string[256];
int index = 0;
for(int i = 0; i < 256; i++) {
if(result[i] == 1) {
new_string[index++] = (char)i;
}
}
new_string[index] = '\0';
printf("新字符串为:%s", new_string);
完整的代码如下:
```c
int main() {
char string1[51], string2[51];
printf("请输入字符串1:");
fgets(string1, 51, stdin);
printf("请输入字符串2:");
fgets(string2, 51, stdin);
char result[256] = {0};
for(int i = 0; i < strlen(string1); i++) {
for(int j = 0; j <