2743:字符串判等

本文介绍了一个简单的字符串判等算法实现,该算法通过忽略大小写及空格来判断两个字符串是否相等。首先去除字符串中的空格,并将所有字符转换为小写,然后使用strcmp函数进行比较。

2743:字符串判等


总时间限制: 
1000ms 
内存限制: 
65536kB
描述 字符串的相关比较 strcmp strlen 最后为了容易比较 在字符串后加上 '\0' 代表字符串的结束

判断两个由大小写字母和空格组成的字符串在忽略大小写,且忽略空格后是否相等。

输入
两行,每行包含一个字符串。
输出
若两个字符串相等,输出YES,否则输出NO。
样例输入
a A bb BB ccc CCC
Aa BBbb CCCccc
样例输出
YES
#include <stdio.h>
#include <iostream>
#include <stack>
#include <string.h>
#include <queue>
#include <cmath>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <string>
using namespace std;
typedef long long LL;

int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt","w",stdout);
    char c1[200], c2[200], c3[200], c4[200];
    gets(c1);
    //getchar();
    gets(c2);
    int len1 = strlen(c1);
    int len2 = strlen(c2);
    int index = 0;
    for(int i = 0; i < len1; i++){
        if(c1[i] != ' '){
            if(c1[i] >= 'A' && c1[i] <= 'Z'){
                c3[index++] = c1[i] - 'A' + 'a';
            }else {
                c3[index++] = c1[i];
            }
        }
    }
    c3[index] = '\0';
    index = 0;
    for(int i = 0; i < len2; i++){
        if(c2[i] != ' '){
            if(c2[i] >= 'A' && c2[i] <= 'Z'){
                c4[index++] = c2[i] - 'A' + 'a';
            }else {
                c4[index++] = c2[i];
            }
        }
    }
    c4[index] = '\0';
    //cout<<"s3:"<<c3<<",s4:"<<c4<<endl;

    /**
    * 设这两个字符串为str1,str2,
    * 若str1==str2,则返回零;
    * 若str1<str2,则返回负数;
    * 若str1>str2,则返回正数。
    **/
    if(strcmp(c3, c4) == 0){
        cout << "YES" << endl;
    }else {
        cout << "NO" << endl;
    }
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值