当前位置:   article > 正文

字符串对比

字符串对比

7-4 字符串对比

分数 15

给定两个仅由大写字母或小写字母组成的字符串(长度介于1到10之间),它们之间的关系是以下4中情况之一

1:两个字符串长度不等。比如 Beijing 和 Hebei

2:两个字符串不仅长度相等,而且相应位置上的字符完全一致(区分大小写),比如 Beijing 和 Beijing

3:两个字符串长度相等,相应位置上的字符仅在不区分大小写的前提下才能达到完全一致(也就是说,它并不满足情况2)。比如 beijing 和 BEIjing

4:两个字符串长度相等,但是即使是不区分大小写也不能使这两个字符串一致。比如 Beijing 和 Nanjing
编程判断输入的两个字符串之间的关系属于这四类中的哪一类,给出所属的类的编号。

输入格式:

包括两行,每行都是一个字符串 。

输出格式:

仅有一个数字,表明这两个字符串的关系编号。

输入样例:

  1. BEIjing
  2. beiJing

输出样例:

3

c++ 

  1. #include <iostream>
  2. #include<string>
  3. using namespace std;
  4. //字符串对比
  5. // A 65 a 97
  6. int main()
  7. {
  8. string str1;
  9. string str2;
  10. cin>>str1>>str2;
  11. if(str1.size() != str2.size())
  12. cout<<1;
  13. else{
  14. if(str1 == str2)
  15. cout<<2;
  16. else {
  17. for(int i = 0; i < str1.size(); i++){
  18. if (str1[i] > 97)
  19. str1[i] -= 32;
  20. if (str2[i] > 97)
  21. str2[i] -= 32;
  22. }
  23. if(str1 == str2)
  24. cout<<3;
  25. else
  26. cout<<4;
  27. }
  28. }
  29. }

C语言 

  1. #include <stdio.h>
  2. #include <string.h>
  3. int main()
  4. {
  5. char a[100],b[100];
  6. int l1,l2;
  7. gets(a);
  8. gets(b);
  9. l1=strlen(a);
  10. l2=strlen(b);
  11. if(l1!=l2)
  12. printf("1");
  13. else{
  14. if(strcmp(a,b)==0)
  15. printf("2");
  16. else{
  17. strcpy(a,strlwr(a));
  18. strcpy(b,strlwr(b));
  19. if(strcmp(a,b)==0)
  20. printf("3");
  21. else
  22. printf("4");
  23. }
  24. }
  25. return 0;
  26. }

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h> // 包含toupper和tolower函数
  4. int main() {
  5. char s1[11], s2[11];
  6. scanf("%s%s", s1, s2);
  7. int len1 = strlen(s1), len2 = strlen(s2);
  8. // 情况1
  9. if (len1 != len2)
  10. {
  11. printf("%d", 1);
  12. }
  13. // 情况2
  14. else if (strcmp(s1, s2) == 0)
  15. {
  16. printf("%d", 2);
  17. }
  18. // 情况3
  19. else {
  20. int flag = 1;
  21. for (int i = 0; i < len1; i++) {
  22. if (tolower(s1[i]) != tolower(s2[i]))
  23. { // 不区分大小写比较
  24. flag = 0;
  25. break;
  26. }
  27. }
  28. if (flag)
  29. {
  30. printf("%d", 3);
  31. }
  32. // 情况4
  33. else
  34. {
  35. printf("%d", 4);
  36. }
  37. }
  38. return 0;
  39. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/594328
推荐阅读
相关标签
  

闽ICP备14008679号