找出两个字符串中相应位置上相

找出两个字符串中相应位置上相同的字母存入另一字符数组,并反序输出。

 

 

#include “stdio.h”
#include “conio.h”
#define MAXLEN 81

void InputString(char s[],char t[])
{ int i;
  printf(“Please input 2 strings.\nFirst string:\n”);
  gets(s);
  printf(“Another string:\n”);
  gets(t);
}

void HandleString(char s[],char t[],char d[])
{ int i=0,j=0;

  for(;s[i]!=’\0’&&t[i]!=’\0’;i++)
  if (
      (
       (’A’<=s[i]&&s[i]<=’Z’)||(’a’<=s[i]&&s[i]<=’z’)
      )&&(s[i]==t[i])
     )d[j++]=s[i];
  d[j]=0;

}

void OutputDestination(char d[])
{ char *p;
  printf(“The same letters between the 2 strings are “);
  if(d[0]!=0)
   { printf(“as follows: \n%s\nAnd it\’s reversed version are as follows:\n”,d);
     for(p=d+strlen(d)-1;d<=p;p–) printf(“%c”,*p);
     printf(“\n”);
   }
  else printf(“none.\n”,d);
}

void main( )
{ char a[MAXLEN],b[MAXLEN],c[MAXLEN];
  InputString(a,b);
  HandleString(a,b,c);
  OutputDestination(c);
  getch();
}

程序的执行结果是

第一次:
Please input 2 strings.
First string:
asdg
Another string:
dfgh
The same letters between the 2 strings are none.

第二次:
Please input 2 strings.
First string:
Are you a worker ? Yes, I am.
Another string:
Is she a teacher ? Yes, she is.
The same letters between the 2 strings are as follows:
erYes
And it’s reversed version are as follows:
seYre

© 版权声明
THE END
喜欢就支持以下吧
点赞10 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容