int yong_strcmp(const char *str1, const char *str2)
{
if (NULL == str1 || NULL == str2)
{
// TODO - yongqiang cheng
return 0;
}
int i = 0;
for (i = 0; str1[i] != '\0'; ++i)
{
if (str1[i] != str2[i])
{
return str1[i] - str2[i];
}
}
return str1[i] - str2[i];
}
bool qiang_strcmp(const char *str1, const char *str2)
{
if (NULL == str1 || NULL == str2)
{
// TODO - yongqiang cheng
return false;
}
while (*str1 && (*str1 == *str2))
{
++str1;
++str2;
}
return *str1 == *str2;
}
int cheng_strcmp(const char str1[], const char str2[])
{
if (NULL == str1 || NULL == str2)
{
// TODO - yongqiang cheng
return 0;
}
int i = 0;
while (str1[i] != '\0' && (str1[i] == str2[i]))
{
++i;
}
return str1[i] - str2[i];
}
4.1 Example 1
//============================================================================
// Name : std::strlen
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
#include <string.h>
int main()
{
const char str1[] = "yong qiang";
const char str2[] = "yong qiang";
int ret = 0;
ret = strcmp(str1, str2);
if (ret < 0) {
printf("str1 is less than str2\n");
}
else if (ret > 0) {
printf("str2 is less than str1\n");
}
else {
printf("str1 is equal to str2\n");
}
return(0);
}
str1 is equal to str2
请按任意键继续. . .
4.2 Example 2
//============================================================================
// Name : std::strlen
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
#include <string.h>
int main()
{
const char str1[] = "1 yong qiang";
const char str2[] = "2 yong qiang";
int ret = 0;
ret = strcmp(str1, str2);
if (ret < 0) {
printf("str1 is less than str2\n");
}
else if (ret > 0) {
printf("str2 is less than str1\n");
}
else {
printf("str1 is equal to str2\n");
}
return(0);
}
str1 is less than str2
请按任意键继续. . .
4.3 Example 3
//============================================================================
// Name : std::strlen
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
#include <string.h>
int main()
{
const char str1[] = "y yong qiang";
const char str2[] = "q yong qiang";
int ret = 0;
ret = strcmp(str1, str2);
if (ret < 0) {
printf("str1 is less than str2\n");
}
else if (ret > 0) {
printf("str2 is less than str1\n");
}
else {
printf("str1 is equal to str2\n");
}
return(0);
}
str2 is less than str1
请按任意键继续. . .
4.4 Example 4
//============================================================================
// Name : std::strlen
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
#include <string.h>
int yong_strcmp(const char *str1, const char *str2)
{
if (NULL == str1 || NULL == str2)
{
// TODO - yongqiang cheng
return 0;
}
int i = 0;
for (i = 0; str1[i] != '\0'; ++i)
{
if (str1[i] != str2[i])
{
return str1[i] - str2[i];
}
}
return str1[i] - str2[i];
}
bool qiang_strcmp(const char *str1, const char *str2)
{
if (NULL == str1 || NULL == str2)
{
// TODO - yongqiang cheng
return false;
}
while (*str1 && (*str1 == *str2))
{
++str1;
++str2;
}
return *str1 == *str2;
}
int main()
{
const char str1[] = "yong qiang";
const char str2[] = "yong qiang";
int ret = 0;
ret = yong_strcmp(str1, str2);
if (ret < 0) {
printf("str1 is less than str2\n");
}
else if (ret > 0) {
printf("str2 is less than str1\n");
}
else {
printf("str1 is equal to str2\n");
}
bool res = false;
res = qiang_strcmp(str1, str2);
if (res) {
printf("true - equal\n");
}
else {
printf("false - unequal\n");
}
return(0);
}
str1 is equal to str2
true - equal
请按任意键继续. . .
4.5 Example 5
//============================================================================
// Name : std::strlen
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
#include <string.h>
int yong_strcmp(const char *str1, const char *str2)
{
if (NULL == str1 || NULL == str2)
{
// TODO - yongqiang cheng
return 0;
}
int i = 0;
for (i = 0; str1[i] != '\0'; ++i)
{
if (str1[i] != str2[i])
{
return str1[i] - str2[i];
}
}
return str1[i] - str2[i];
}
bool qiang_strcmp(const char *str1, const char *str2)
{
if (NULL == str1 || NULL == str2)
{
// TODO - yongqiang cheng
return false;
}
while (*str1 && (*str1 == *str2))
{
++str1;
++str2;
}
return *str1 == *str2;
}
int main()
{
const char str1[] = "1 yong qiang";
const char str2[] = "2 yong qiang";
int ret = 0;
ret = yong_strcmp(str1, str2);
if (ret < 0) {
printf("str1 is less than str2\n");
}
else if (ret > 0) {
printf("str2 is less than str1\n");
}
else {
printf("str1 is equal to str2\n");
}
bool res = false;
res = qiang_strcmp(str1, str2);
if (res) {
printf("true - equal\n");
}
else {
printf("false - unequal\n");
}
return(0);
}
str1 is less than str2
false - unequal
请按任意键继续. . .
4.6 Example 6
//============================================================================
// Name : std::strlen
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
#include <string.h>
int yong_strcmp(const char *str1, const char *str2)
{
if (NULL == str1 || NULL == str2)
{
// TODO - yongqiang cheng
return 0;
}
int i = 0;
for (i = 0; str1[i] != '\0'; ++i)
{
if (str1[i] != str2[i])
{
return str1[i] - str2[i];
}
}
return str1[i] - str2[i];
}
bool qiang_strcmp(const char *str1, const char *str2)
{
if (NULL == str1 || NULL == str2)
{
// TODO - yongqiang cheng
return false;
}
while (*str1 && (*str1 == *str2))
{
++str1;
++str2;
}
return *str1 == *str2;
}
int main()
{
const char str1[] = "y yong qiang";
const char str2[] = "q yong qiang";
int ret = 0;
ret = yong_strcmp(str1, str2);
if (ret < 0) {
printf("str1 is less than str2\n");
}
else if (ret > 0) {
printf("str2 is less than str1\n");
}
else {
printf("str1 is equal to str2\n");
}
bool res = false;
res = qiang_strcmp(str1, str2);
if (res) {
printf("true - equal\n");
}
else {
printf("false - unequal\n");
}
return(0);
}
str2 is less than str1
false - unequal
请按任意键继续. . .
4.7 Example 7
//============================================================================
// Name : std::strlen
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
#include <string.h>
int yong_strcmp(const char *str1, const char *str2)
{
if (NULL == str1 || NULL == str2)
{
// TODO - yongqiang cheng
return 0;
}
int i = 0;
for (i = 0; str1[i] != '\0'; ++i)
{
if (str1[i] != str2[i])
{
return str1[i] - str2[i];
}
}
return str1[i] - str2[i];
}
bool qiang_strcmp(const char *str1, const char *str2)
{
if (NULL == str1 || NULL == str2)
{
// TODO - yongqiang cheng
return false;
}
while (*str1 && (*str1 == *str2))
{
++str1;
++str2;
}
return *str1 == *str2;
}
int cheng_strcmp(const char str1[], const char str2[])
{
if (NULL == str1 || NULL == str2)
{
// TODO - yongqiang cheng
return 0;
}
int i = 0;
while (str1[i] != '\0' && (str1[i] == str2[i]))
{
++i;
}
return str1[i] - str2[i];
}
int main()
{
const char str1[] = "y yong qiang";
const char str2[] = "q yong qiang";
int ret = 0;
ret = cheng_strcmp(str1, str2);
if (ret < 0) {
printf("str1 is less than str2\n");
}
else if (ret > 0) {
printf("str2 is less than str1\n");
}
else {
printf("str1 is equal to str2\n");
}
bool res = false;
res = qiang_strcmp(str1, str2);
if (res) {
printf("true - equal\n");
}
else {
printf("false - unequal\n");
}
return(0);
}
str2 is less than str1
false - unequal
请按任意键继续. . .
5. yongqiangcheng - strncmp
int yong_strncmp(const char *str1, const char *str2, const int len)
{
if (NULL == str1 || NULL == str2)
{
// TODO - yongqiang cheng
return 0;
}
for (int i = 0; i < len; ++i)
{
if (str1[i] != str2[i])
{
return str1[i] - str2[i];
}
}
return 0;
}
5.1 Example 1
//============================================================================
// Name : std::strlen
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
#include <string.h>
int yong_strncmp(const char *str1, const char *str2, const int len)
{
if (NULL == str1 || NULL == str2)
{
// TODO - yongqiang cheng
return 0;
}
for (int i = 0; i < len; ++i)
{
if (str1[i] != str2[i])
{
return str1[i] - str2[i];
}
}
return 0;
}
int main()
{
const char str1[] = "yong1";
const char str2[] = "yong2";
int ret = 0;
ret = yong_strncmp(str1, str2, 4);
if (ret < 0) {
printf("str1 is less than str2\n");
}
else if (ret > 0) {
printf("str2 is less than str1\n");
}
else {
printf("str1 is equal to str2\n");
}
ret = yong_strncmp(str1, str2, 5);
if (ret < 0) {
printf("str1 is less than str2\n");
}
else if (ret > 0) {
printf("str2 is less than str1\n");
}
else {
printf("str1 is equal to str2\n");
}
return(0);
}
str1 is equal to str2
str1 is less than str2
请按任意键继续. . .