C语言大神求助把两个字符串合并,重复出现的字符保留一个,然后排序后输出。

作者&投稿:淫姬 (若有异议请与网页底部的电邮联系)
紧急!!在线求解! C语言输入两个字符串,并且进行合并,输出合并后的字符串,然后将该合并字符串逆序输出~

利用栈和队列就可以了,
# include "stdio.h"
# include "stdlib.h"
# include "stdio.h"
# include "stdlib.h"
# define True 1
# define FALSE 0
# define OK 1
# define ERROR 0
# define INFEASIBLE -1
# define OVERFLOW -2
# define STACK_INIT_SIZE 100
# define STACK_INCREMENT 10
# define QUEUE_INIT_SIZE 100
typedef char SElemType;
typedef char QElemType;
typedef int Status;
typedef struct SqStack
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
typedef struct
{
QElemType *base;
int front;
int rear;
}SqQueue;
Status InitStack (SqStack &S);
Status GetTop(SqStack S,SElemType &e);
Status Push (SqStack &S,SElemType e);
Status Pop(SqStack &S,SElemType &e);
Status DestroyStack(SqStack &S);
Status ClearStack(SqStack &S);
int StackLength(SqStack S);
Status StackEmpty(SqStack &S);
Status StackTraverse(SqStack S,Status (* Func)(SElemType e));
Status InitSqQueue(SqQueue & Q);
Status EnQueue (SqQueue &Q,QElemType e);
Status CreateSqQueue(SqQueue &Q);
int QueueLength(SqQueue Q);
Status DeQueue(SqQueue & Q,QElemType &e);
Status DestroySqQueue(SqQueue &Q);
Status QueueTraverse(SqQueue Q,Status (* Func)(QElemType e));
Status Print(char e);
Status Verse(SqQueue Q);
Status hebing(SqQueue &Q1,SqQueue Q2);
int main()
{
SqQueue Q,Q1;
InitSqQueue(Q);
InitSqQueue(Q1);
CreateSqQueue(Q);
CreateSqQueue(Q1);
hebing(Q,Q1);
Verse(Q);
return 0;
}
Status InitStack (SqStack &S)
{
S.base=(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if(!S.base) exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status GetTop(SqStack S,SElemType &e)
{
if(S.base==S.top) return ERROR;
e=*(S.top-1);
return OK;
}
Status Push (SqStack &S,SElemType e)
{
if(S.top-S.base==S.stacksize)
{
S.base=( SElemType*)realloc(S.base,STACK_INCREMENT*sizeof(SElemType));
if(!S.base)exit(OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACK_INCREMENT;
}
*S.top++=e;
return OK;
}
Status Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base) return ERROR;
e=*(--S.top);
return OK;
}
Status DestroyStack(SqStack &S)
{
if(!S.base) return ERROR;
free(S.base);
return OK;
}
Status ClearStack(SqStack &S)
{
if(!S.base) return ERROR;
S.top=S.base;
return OK;
}
int StackLength(SqStack S)
{
if(!S.base) return ERROR;
return S.top-S.base;
}
Status StackEmpty(SqStack &S)
{
if(!S.base) return ERROR;
if(StackLength(S)==0) return 1;
return 0;
}
Status StackTraverse(SqStack S,Status (* Func)(SElemType e))
{
if(S.top==S.base) return ERROR;
for(SElemType *p=S.top-1;p>=S.base;p--)
{
Func(*p);
}
return OK;
}
Status InitSqQueue(SqQueue & Q)
{
Q.base=(QElemType *)malloc(QUEUE_INIT_SIZE*sizeof(QElemType));
if(!Q.base) exit (OVERFLOW);
Q.front=Q.rear=0;
return OK;
}
Status EnQueue (SqQueue &Q,QElemType e)
{
if((Q.rear+1)%QUEUE_INIT_SIZE==Q.front) return ERROR;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%QUEUE_INIT_SIZE;
return OK;
}
Status CreateSqQueue(SqQueue &Q)
{
QElemType a;
printf("请输入字符串:(以字符'@'结束)
");
while ((a=getchar())!='@')
{
if(a!='
')
EnQueue(Q,a);
}
return OK;
}
int QueueLength(SqQueue Q)
{
return (Q.rear-Q.front+1)%QUEUE_INIT_SIZE;
}
Status DeQueue(SqQueue & Q,QElemType &e)
{
if(Q.front==Q.rear) return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%QUEUE_INIT_SIZE;
return OK;
}
Status DestroySqQueue(SqQueue &Q)
{
if(!Q.base) return ERROR;
free(Q.base);
return OK;
}
Status QueueTraverse(SqQueue Q,Status (* Func)(QElemType e))
{
if(Q.rear==Q.front) return ERROR;
for(int i=Q.front;i%QUEUE_INIT_SIZE!=Q.rear;i++)
{
Func(Q.base[i]);
}
return OK;
}
Status Print(char e)
{
printf("%c",e);
return OK;
}
Status Verse(SqQueue Q)
{
QElemType e;
SqStack S;
SqQueue Q2;
InitStack (S);
InitSqQueue (Q2);
while (Q.rear!=Q.front)
{
DeQueue(Q,e);
EnQueue(Q2,e);
Push(S,e);
}
printf("源数据为:
");
QueueTraverse(Q2,Print);
printf("
");
printf("逆置的数据为:
");
StackTraverse(S,Print);
printf("
");
return OK;
}
Status hebing(SqQueue &Q,SqQueue Q1)
{
char e;
while(Q1.front!=Q1.rear)
{
DeQueue(Q1,e);
EnQueue (Q, e);
}
return 0;
}

调试了一下,这一句有问题:
if(str1[p]>str1[j]); //这里多了分号
p=j; //导致这一句每一循环都被执行
改为:
if(str1[p]>str1[j])
p=j;
即可

代码如下(建议使用C++编译器编译或支持最新C标准的编译器,我在C11标准下通过):

#include <stdio.h>
#include <string.h>
#include <malloc.h>
void Trim(char *str) {
 char *strTmp = (char *)malloc(sizeof(char) * strlen(str));
 int i = 1, j = 1;
 while (str[j] != '\0') {
  if (str[j] != str[j - 1]) {
   strTmp[i - 1] = str[j - 1];
   i++;
   j++;
  } else {
   j++;
  }
 }
 strTmp[i - 1] = str[j - 1];
 strcpy(str, strTmp);
 free(strTmp);
}
int main() {
 char *str1 = "a3B2c1";
 char *str2 = "Q5BcF570";
 char *str3 = (char *)malloc(sizeof(char) * (strlen(str1) + strlen(str2)));
 strcpy(str3, str1);
 strcat(str3, str2);
 for (int i = 0; i < strlen(str3); i++)
  for (int j = 0; j < i; j++)
   if (str3[i] < str3[j]) {
    char cTmp = str3[i];
    str3[i] = str3[j];
    str3[j] = cTmp;
   }
 Trim(str3);
   
 printf("%s + %s => %s
", str1, str2, str3);

free(str3);
   
 return 0;
}



C语言,求两个字符串的相同字符的个数,求大神帮忙看看这个程序哪里有问题...
include<stdio.h>void main(){ char a[2][100],*pa[2]; int i,j,n=0; \/\/初始化 printf("输入两个字符串\\n"); for(i=0;i<2;i++) { \/\/scanf("%s",a[i]); gets(a[i]); \/\/最好用gets } pa[0]=a[0]; pa[1]=a[1]; printf("%...

求c语言大神解答!在线等!急!!!用switch语句编写程序实现两个数的+-*...
include<stdio.h>int main(void){int a, b;char c;printf("请输入两个数:");scanf("%d %d", &a, &b);getchar();printf("请输入你需要的运算法则:");scanf("%c",&c);switch (c){case '+':printf("%d+%d = %d", a, b, a+b);break;case '-':printf("%d-%d = %d",...

求解C语言两个问题
1、int x=2,y=2,a;a=(x=x+1)丨丨(y=y+1),括号优先级最高,先计算第一个括号里面的x = x + 1,x = 3,3非0,为逻辑真 ||逻辑或运算符,一边为真,那结果一定为真,所以||右边就不计算了,整个结果为逻辑真赋值给a 最终就是a=1,x=3,y=2(y=y+1被优化掉不计算了)2、...

求C语言大神帮忙解决一下关于宏的两个习题,要习题7和8详细解答过程...
第七题,逐层代入即可 语句为PRINT(F(3)*x)代入PRINT定义 PR(F(3)*x); putchar('\\n');代入PR printf("%d",(int)(F(3)*x));putchar('\\n');代入F printf("%d",(int)(3.84+3*x));putchar('\\n');于是输出的是(int)(3.84+3*x)=(int)(3.84+3*2)=(int)(9.84)...

c语言编程,输入两个数a,b (a
include <stdio.h>int main(){ int a, b; int i; scanf ("%d%d", &a, &b); for (i = a; i <= b; ++i){ printf ("%d ", i); } printf ("\\n"); return 0;} 程序执行结果如下:

求助英语语言学大神
选择C中心词,因为中心词是决定词性的。中心词在复合词中,往往是最后边的那个词,在从左往右写的语言中,如英语、德语、俄语等,中心词确实是右边的那个词,但是如果是从右往左倒着写的语言,像阿语、希伯来语等,中心词则是左边的那个词了。所以这个题只能选C,而不能选择A。

c语言 求助各位大神帮忙把程序写下!
using namespace std;void main (){ int year;int month;int day;int days[13]={0,31,28,30,30,31,30,31,31,30,31,30,31};printf("请输入年份=");scanf("%d",&year);printf("请输入月份=");scanf("%d",&month);if(((i%4==0)&&(i%100!=00))||(i%400==0)){ days[2...

c语言,如图,求大神解释为什么两个数为什么不同?
计算机内部采用的是二进制处理数据的,你可以把一个带小数的十进制数(比如那个123.456789)先转成二进制数,再还原成十进制数,看看还是不是123.456789,多数情况下是无法精确还原的,原因很简单,十进制小数转成二进制小数时,由于是小数部分乘以2,直到小数部分为0时才结束,你想想,除了0.5乘以2后...

c语言两个double类型数据比较大小,求大神解答下
有什么疑问?double类型数据存储的数据是不精确的存储,如0.1可能是0.0999999。。。这是由于计算机表示浮点数的方法造成的精度缺陷,所以,在比较时,一般通过判断两数差与一个精度值的大小,来确定两数的大小,这个精度值由使用者根据情况自行确定,如,你这代码中的1e-6 ...

c语言程序,输入两个数和一个运算符,计算其结果,用if吧,求大神帮帮忙
include <stdio.h>int main(){int a,b,c; char op; scanf("%d %d %c",&a,&b,&op); if(op=='+')c=a+b; if(op=='-')c=a-b; if(op=='*')c=a*b; if(op=='\/')if(b!=0)c=a\/b; if(op=='%')if(b!=0)c=a%b; if(op=='%')c=a+b; if(b!=0||op...

宝安区18870777637: c语言编写一个程序:实现两个字符串的连接谢谢了,大神帮忙啊 -
言侄金双: #includevoid main() { char *p1,*p2,s1[100],s2[100]; printf("输入字源符zhidao串1:"); scanf("%s",s1); printf("输入字符串2:"); scanf("%s",s2); for(p1=s1;*p1;p1++); for(p2=s2;*p2;*p1++=*p2++); *p1='\0'; printf("连接后的字符串:%s",s1); }

宝安区18870777637: c语言问题:字符串的交叉合并问题;如,输入两个字符串,要求将这两个字符串交叉连接. -
言侄金双: i没有初始值 int i =0 ; 就可以了 还有结尾处的 while(b[i]!='\0') { c[i]=*q; q++; } 改成 while(*q!='\0') { c[i]=*q; q++; }

宝安区18870777637: c语言 字符串 求助 -
言侄金双: #include <stdio.h>#include <stdlib.h>#include <string.h> void sort(char *s,char *t) { int i=0,j=0,k=0; char tmp[256]; int lens=strlen(s),lent=strlen(t); while(i < lens&&j < lent) { if(s[i] > t[j]) { tmp[k++]=t[j++]; } else { tmp[k++]=s[i++]; } } while(i < lens) tmp[k++]=...

宝安区18870777637: C语言如何实现两个字符串相连接,不能用strcat -
言侄金双: 首先,你需要保证第一个字符串可以容纳下第二个字符串,否则,应该新建立一个字符串数组,另外C类型的字符串,需要在字符串的最后加上一个'\0'否则会在正常输出之后输出乱码.

宝安区18870777637: c语言 编写函数用于进行俩个字符串的连接,编写函数用于统计一个字符串的长度,并在主函数中调用.求大神 -
言侄金双: #include "stdio.h"int catstr(char *a,char *b,char *c){ int i=0,j=0; for(;*(a+i);i++)*(c+i)=...

宝安区18870777637: 编写程序,合并一个字符串中所有相邻的重复字符? -
言侄金双: 两个指针,一个p指向当前字符,一个q指向下一个字符, 如果*p和*q相等,一直q++,一直到不相等时然后用strcpy函数把q后面的字符拷贝到p后面, 不相等时p,q都后移一位, 循环条件是q !=null

宝安区18870777637: C语言字符串拼接问题 -
言侄金双: 因为你数组a,初始化的时候没有指定具体的大小,所以是按初始化的值来取的大小,也就是4.你两个一下合并,导致数组a越界了,所以出错了

宝安区18870777637: 下面是用C语言写的把两个字符串连接在一起,为什么第一个程序在输入字符时可以输入空格而第二个不能输入 -
言侄金双: 第一个:for (i=0;a[i]!=0;i++,l++) { b[l++]=a[i]; } 以上l++使用了两次,会出错的!改为:for (i=0;a[i]!=0;i++) { b[l++]=a[i]; }

宝安区18870777637: c语言 不用strcat()函数,实现两个字符串连接.自己写的简单一点的 -
言侄金双: #include<stdio.h> char*strcat(char*str1,char*str2) { char*p=str1; int i=0; while(*p!=0)p++; do { *p++=str2[i++]; } while(str2[i]!=0); return str1; } int main() { char a[90]="hello "; printf("%s\n",strcat(a,"world")); return 0; }

宝安区18870777637: C语言一个字符串连接的问题,请各位大神帮忙看一下错在哪了? -
言侄金双: 我用GCC编译你的程序,没有出现你说的情况. 测试的字串分别为hello和world,输出的结果为dehllorwe 你再看看是不是测试的字串问题,或者换个编译器试试.

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网