请c语言大神解惑 编写一程序,实现从键盘输入两个字符串str1和str2,要求各串中无重复的字符,求 编一个C程序,功能是:从键盘输入两个字符串str1 str2...

\u7f16\u5199\u4e00\u4e2aC\u7a0b\u5e8f,\u529f\u80fd\u662f:\u4ece\u952e\u76d8\u8f93\u5165\u4e24\u4e2a\u5b57\u7b26\u4e32str1 str2,\u5e76\u5c06\u5b57\u7b26\u4e32 str2\u8fde\u63a5str1\u540e\u9762\uff0c\u5e76\u5c06\u7ed3\u679c\u663e\u793a\u51fa\u6765

\u7531\u4e8eC\u7684\u5b57\u7b26\u4e32\u662f\u7531\u5b57\u7b26\u6570\u7ec4\u64cd\u4f5c\u7684\uff0c\u6240\u4ee5\u8fd9\u79cd\u9898\u5f97\u4fdd\u8bc1\u5b57\u7b26\u6570\u7ec4str1\u80fd\u653e\u5f97\u4e0b\u5b83\u81ea\u8eab\u548c\u62f7\u8d1d\u5728\u5b83\u540e\u9762\u7684str2\u7684\u5185\u5bb9\u3002\u4e0d\u7528\u5e93\u51fd\u6570\u7684\u8bdd\u53ef\u4ee5\u5982\u4e0b\u64cd\u4f5c\uff1a
#include "stdio.h"int main(int argc,char *argv[]){char str1[301],str2[101];int i,j;printf("Please enter 2 strings...\n");scanf("%200s%100s",str1,str2);//\u9650\u5236\u957f\u5ea6\u4ee5\u4fdd\u8bc1\u4e0d\u6ea2\u51fafor(j=i=0;str1[i];i++);//\u6d4bstr1\u7684\u7ed3\u5c3ewhile(str1[i++]=str2[j++]);//\u628astr2\u7684\u5185\u5bb9\u9010\u4e00\u62f7\u8d1d\u5230str1\u7684\u540e\u9762printf("\nAfter connection string as follows:\n%s\n",str1);return 0;}\u8bd5\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a

#include
#include

#define N 100 //\u5b9a\u4e49\u6570\u7ec4\u6700\u5927\u957f\u5ea6100
void main()
{
char str1[N],str2[N];
int i=0,j=0;
gets(str1); //\u83b7\u53d6\u5b57\u7b26\u4e32\uff0c\u56de\u8f66\u7ed3\u675f
gets(str2);
while(str1[i]!='\0')//\u8ba1\u7b97str1\u6570\u7ec4\u957f\u5ea6
i++;
while(str2[j]!='\0')//\u628astr2\u8fde\u63a5\u5230str1\u5c3e\u5df4
{
str1[i++]=str2[j];
j++;
}
for(int n=0;n<i;n++)
printf("%c",str1[n]);
}

vc6.0\u4e0b\u6ca1\u95ee\u9898

#include <stdio.h>
#include <string.h>
int repeated(char* str)/*检查字符串内是否有重复的字符,有就返回1,无就返回0*/
{
    int i=0, len=strlen(str)-1;//调用strlen得到字符串的长度
    for(i=0; i<len; i++)/*依次查看每个字符在后面是否重复出现*/
        if(strchr(str+i+1, str[i]))
            return 1;/*一旦发现就立即返回*/
    return 0;
}
int main()
{
    int i, len, j=0;
    char s1[100], s2[100], s3[100];/*保存两个字符串和交集串*/
    char* s[2]={s1,s2};/*用一个数组s来管理s1和s2以便在下面用for循环处理*/
    for(i=0; i<2; i++){/*依次输入两个字符串*///这里使用do,while循环!因为至少要输出一次!
        do{
            printf("请输入第%d个字符串:
", i+1);
            scanf("%s",s[i]);
        }while(repeated(s[i])&&printf("串内有重复字符!
"));/*检查是否有重复*/
    }
    len = strlen(s1);
    for(i=0; i<len; i++)/*依次查找s1中的每一个字符是否在s2中存在*/
        if(strchr(s2,s1[i]))
            s3[j++] = s1[i];/*存在的字符就加入到交集中*/
    s3[j] = '\0';/*添加字符串结束标记,紧记C语言里的字符串一定要加上结束符!*/
    if(j>0)
        printf("交集为[%s]
", s3);
    else
        printf("交集为空
");
    return 0;
}

我把注释都完整了,少年还有不解之处,追问便是!


希望这些能给你提供一点帮助,亲!



扩展阅读:扫一扫题目出答案 ... c++编程 ... c++编程入门自学 ... 编程学习 ... c#语言编译器手机版 ... c#入门基础知识 ... c#语言翻译器 ... c语言解惑pdf百度网盘 ... c语言解惑在线阅读 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网