Getting Exercise 3 string string comparison explanations title

Written for: http://codeforces.com/problemset/problem/112/A

Title Description

Give you a two strings and b, you need to compare their lexicographical size in the case of case-insensitive.
Ignore case means: "abc" and "Abc" lexicographical ignoring capitalization is equal.
In the case of case-insensitive:

  • If the string is a string lexicographically less than b, the output 1;
  • If the string is a string lexicographically and b are equal, outputs 0;
  • If the string is a string lexicographically play than b, the output 1.

    Input Format

    Comprising two input lines, the first line character string corresponding to a, the second row corresponding character string b. Two strings of equal length, between 1 and 100.

    Output Format

    Described in accordance with the subject of said output comparison result lexicographical two strings (-1, 0 or 1) in the case of case-insensitive.

    Sample input 1

aaaa
aaaA

Sample output 1

0

Sample input 2

abs
Abz

Sample output 2

-1

Sample input 3

abcdefg
AbCdEfF

Sample output 3

1

Topic analysis

First, because the question head are case insensitive, so we can before the formal process of the two strings of all uppercase characters converted to lowercase characters.
Then you can use a variable char tolower () function to convert aligned.
Then, if we are operating with char array, we can compare two strings lexicographically with strcmp function;
if we are operating with a string, then we can use the symbols directly compare compare two strings size, or use the compare string member function to compare.

Using the code char array to achieve the following:

#include <bits/stdc++.h>
using namespace std;

char a[110], b[110];

int main() {
    cin >> a >> b;
    int n = strlen(a);
    for (int i = 0; i < n; i ++) a[i] = tolower(a[i]);
    for (int i = 0; i < n; i ++) b[i] = tolower(b[i]);
    cout << strcmp(a, b) << endl;
    return 0;
}

Using the code string to achieve the following:

#include <bits/stdc++.h>
using namespace std;

string a, b;

int main() {
    cin >> a >> b;
    int n = a.length();
    for (int i = 0; i < n; i ++) a[i] = tolower(a[i]);
    for (int i = 0; i < n; i ++) b[i] = tolower(b[i]);
    cout << a.compare(b) << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/zifeiynoip/p/11450567.html