C++实现一个比较两个字符串的大小的函数int strcmp_s(const char *s1, const char *s2);该函数返回值如下: 如果返回值小于 0,则表示 str1 小于 str2。 如果返回值大于 0,则表示 str1 大于 str2。 如果返回值等于 0,则表示 str1 等于 str2。
时间: 2024-03-08 18:49:01 浏览: 33
可以使用C++标准库中的`strcmp_s`函数进行实现,该函数在`<cstring>`头文件中声明。代码示例如下:
```c++
#include <cstring>
#include <iostream>
int compareStrings(const char* str1, const char* str2) {
return std::strcmp(str1, str2);
}
int main() {
const char* str1 = "hello";
const char* str2 = "world";
std::cout << compareStrings(str1, str2) << std::endl; // 输出-15,即str1 < str2
return 0;
}
```
上述代码中,`compareStrings`函数使用`std::strcmp`函数比较两个字符串的大小,该函数返回值小于0表示str1小于str2,返回值大于0表示str1大于str2,返回值等于0表示str1等于str2。在`main`函数中,我们定义了两个字符串指针`str1`和`str2`,并将它们作为参数传递给`compareStrings`函数进行比较。最后输出比较结果。
相关问题
实现函数int compare(char*s1,char*s2),该函数功能:比较两个字符串大小,再编写主程序调用函数,在屏幕上输出比较结果
可以使用strcmp函数进行字符串比较。函数原型为:
```
int strcmp(const char *s1, const char *s2);
```
该函数会比较两个字符串 s1 和 s2,如果 s1 < s2,返回负数;如果 s1 > s2,返回正数;如果 s1 = s2,返回 0。
可以根据这个函数编写compare函数,代码如下:
```c++
#include <iostream>
#include <cstring>
using namespace std;
int compare(char* s1, char* s2) {
return strcmp(s1, s2);
}
int main() {
char s1[100], s2[100];
cout << "请输入字符串1:";
cin >> s1;
cout << "请输入字符串2:";
cin >> s2;
int result = compare(s1, s2);
if (result < 0) {
cout << "字符串1小于字符串2" << endl;
} else if (result > 0) {
cout << "字符串1大于字符串2" << endl;
} else {
cout << "字符串1等于字符串2" << endl;
}
return 0;
}
```
在主函数中,先输入两个字符串,然后调用 compare 函数进行比较,最后根据比较结果输出对应的信息。
c++,完成程序,只在Program和End之间增加代码。 英语单词的后缀构成有一定的规律,例如”teacher”和”programmer”有相同后缀”er”。请完成如下的C++函数功能:findCommonSuffix(char * word1, char * word2, char * suffix),该函数分析得到word1和word2两个单词的最长共同后缀,分析结果存到参数suffix中。 例如,”globally”和”internally”的最长共同后缀是”ally”,”gloves”和”dove”的最长共同后缀是空串。 提示:可能用到的字符串函数说明如下: strlen(const char * str):返回字符串str的长度; strcpy(char * dest, const char * src):把字符串src复制到dest; strcat(char * dest, const char * src):把字符串src连接到dest后面; char * strstr(const char * s1, const char * s2):从s1中查找子串s2第一次出现的位置。
以下是实现该功能的C++代码:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
void findCommonSuffix(char *word1, char *word2, char *suffix) {
int len1 = strlen(word1);
int len2 = strlen(word2);
int minLen = min(len1, len2);
int i;
for (i = 1; i <= minLen; i++) {
if (strcmp(word1 + len1 - i, word2 + len2 - i) != 0) {
break;
}
}
if (i == 1) {
suffix[0] = '\0';
} else {
strcpy(suffix, word1 + len1 - i + 1);
}
}
int main() {
char word1[] = "globally";
char word2[] = "internally";
char suffix[100];
findCommonSuffix(word1, word2, suffix);
cout << suffix << endl;
char word3[] = "gloves";
char word4[] = "dove";
findCommonSuffix(word3, word4, suffix);
cout << suffix << endl;
return 0;
}
```
该函数首先获取两个单词的长度,然后比较它们的末尾字符是否相同,直到找到第一个不相同的字符。这个字符之后的部分就是它们的最长共同后缀。如果没有共同后缀,则返回空串。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)