• C/C++字符串使用整理


    strcmp 函数

    正确地比较字符串,应该使用库函数 strcmp。该函数以两个 C 字符串作为形参,并返回一个整数,表示两个字符串相互比较的结果。其原型如下:

    int strcmp(char *string1, char *string2)

    表示函数将釆用两个字符串作为形参(char* 是 C 字符串的类型)并返回一个整数结果。结果的值是根据以下约定设置的:

    1、如果两个字符串的每个字符都相等,则结果为 0。

    2、如果 string1 以字母顺序排在 string2 之前,则结果为负数。

    3、如果 string1 以字母顺序排在 string2 之后,则结果为正值。

    下面是一段参考代码:

    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int main()
    {
        const int LENGTH = 40;
        char firststring[LENGTH], secondstring[LENGTH];
        // Read two strings
        cout << "Enter a string: ";
        cin.getline(firststring, LENGTH);
        cout << "Enter another string:";
        cin.getline(secondstring, LENGTH);
        if (strcmp(firststring, secondstring) == 0)
            cout << "You entered the same string twice. \n";
        else
            cout << "The strings are not the same.\n";
        return 0;
    }
    首先是头文件,使用字符串的函数必须加上头文件#include<iostream>。接着定义一个常数 LENGTJH表示长度,然后就是定义两个字符串,
    同时要注意的是字符串的输入应该用cin.getline,这样才能让空格等字符也能输入到字符串中。输入完字符串后就直接用strcmp
    函数将两个字符串进行对比。当比较字符串时,函数 strcmp 是区分大小写的。如果用户在此程序中输入 "Dog" 和 "dog",则程序将报告它们不一样。
    一些编译器提供执行不区分大小写比较的非标准版本的 strcmp。除了字符的大小写被忽略之外,这些函数与 strcmp —样工作。
  • 相关阅读:
    images have the “stationarity” property, which implies that features that are useful in one region are also likely to be useful for other regions.
    算法报告
    word2vec_basic.py
    Softmax_function
    Convex combination
    函数的光滑化或正则化 卷积 应用 两个统计独立变量X与Y的和的概率密度函数是X与Y的概率密度函数的卷积
    world embedding 嵌入
    dump json 显示中文问题
    dump json 显示中文问题
    perl $d = encode_utf8($r); $f = decode_json($d)
  • 原文地址:https://www.cnblogs.com/SHXT/p/10779409.html
Copyright © 2020-2023  润新知