题目
Complete the solution so that it returns true if the first argument(string) passed in ends with the 2nd argument (also a string).
写一个函数,如果传入的第一个参数(字符串)以第二个参数(也是一个字符串)结尾,则它返回 true。
例子:
solution('abc', 'bc') // returns truesolution('abc', 'd') // returns false
复制代码
代码区:
#include <stdbool.h>
bool solution(const char *string, const char *ending){ return true;}
复制代码
#include <string>bool solution(std::string const &str, std::string const &ending) { return true;}
复制代码
解答
CPP
#include <string>
bool solution(const std::string& str, const std::string& ending) { return str.size() >= ending.size() && str.compare(str.size() - ending.size(), std::string::npos, ending) == 0;}
复制代码
bool solution(std::string const &str, std::string const &ending) { return (std::string(str.end() - ending.size(), str.end()) == ending);}
复制代码
bool solution(std::string const &str, std::string const &ending) { const int slen = str.length(); const int eLen = ending.length(); if (slen < eLen) { return false; } for (int i = 1; i <= eLen; i++) { if (str[slen - i] != ending[eLen - i]) { return false; } } return true;}
复制代码
C
#include <stdbool.h>#include <string.h>
bool solution(const char *string, const char *ending){ int len = strlen(string) - strlen(ending); return len < 0 ?false :strcmp(string + len, ending) == 0;}
复制代码
#include <stdbool.h>#include <string.h>
bool solution(const char *string, const char *ending){ int slen = strlen(string); int elen = strlen(ending); if (slen < elen) return false; int y = 0; for(int x = slen - elen; x <= slen; ++x) { if(string[x] != ending[y]) { return false; } ++y; } return true;}
复制代码
#include <stdbool.h>
bool solution(const char *string, const char *ending) { int str1 = strlen(string); int str2 = strlen(ending); return str1 >= str2 ? 0 == memcmp(&string[str1 - str2], ending, str2) : false;}
复制代码
D
#include<iostream>using namespace std;
bool solution(string const& str, string const& ending) { int n1 = strlen(str.c_str());//获取字符串长度 int n2 = strlen(ending.c_str()); int flag = 0; for (int i = n1 - n2 ; i < n1; i ++) { if (str[i] == ending[flag]) flag ++; else return false; } if (flag = n2)return true; }
int main(){ string str , ending; cin >> str; cin >> ending; cout<<solution(str, ending); system("pause"); return 0;}
复制代码
PS
strlen()函数
strlen()函数 用于 计算 指定字符串的 长度,但 不包括 结束字符(打印字符串长度)。
注意事项:
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include <string.h> int main() { char arr1[] = "abc"; // "abc" -- 'a' 'b' 'c' '\0' // '\0'是字符串的结束标志 char arr2[] = {'a', 'b', 'c'}; // 'a' 'b' 'c' //区别是无'\0',导致错误 printf("%s\n", arr1);//输出abc printf("%d\n", strlen(arr1));//输出3 printf("%s\n", arr2);//输出乱码 printf("%d\n", strlen(arr2));//输出乱数 return 0; }
复制代码
"abc" 结尾默认包含 'a' 'b' 'c' '\0' ,而'a' 'b' 'c',区别是无'\0'。
优化方案:
char arr2[] = {'a', 'b', 'c', 0};// 'a' 'b' 'c' '\0'
复制代码
结论:
结论:字符串的结束标志是一个" \0 "的转义字符。在计算字符串长度的时候" \0 "是结束标志,不算字符串内容。如果没有结束标志,则程序可能沿着数组在内存中的位置不断向前寻找,直到遇见空字符才会停止。可能会导致输出的内容变多,计算长度时过长。
strcmp()函数
将指定的两个字符串进行比较。
声明:
int strcmp(const char *str1, const char *str2)
复制代码
参数:
str1 – 要进行比较的第一个字符串。str2 – 要进行比较的第二个字符串。
返回值:(比较指定的 ASCII 值)
如果 str1 < str2返回值为 < 0 的数如果 str2 > str1返回值为 > 0 的数如果 str1 = str2返回值为 0
原理:
strcmp()函数是根据 ACSII 码的值来比较两个字符串的;strcmp()函数首先将 s1 字符串的第一个字符值减去 s2 第一个字符,若差值为零则继续比较下去,接着比较第二个字符然后第三个字符等等,若差值不为零,则停止比较并返回两个 ASCII 码差值。
无论两个字符串是什么样,strcmp 函数最多比较到其中一个字符串遇到结束符'/0'为止,得出结果。
注意:
strcmp(const char *s1,const char * s2)这里面只能比较字符串,即可用于比较两个字符串常量,或比较数组和字符串常量,不能比较数字等其他形式的参数。
size(),sizeof(),length(),strlen()对比分析
(1)size()和 sizeof()
使用范围:
C++中 size()函数除了跟 length()函数一样可以获取字符串长度之外,还可以获取 vector 类型的长度。size()主要是进行元素个数的计算,传入的参数一定要是一个数组。不能是单个变量或者是指针。
string str = "ADAS";vector < int> num(10,5)
int lenstr = str.size();int lenvec = num.size();//lenstr = 4; lenvec = 10
复制代码
sizeof()主要是进行所占字节大小的计算,不管传进的参数是什么,它是运算符不是函数。
(2)length()和 strlen()
使用范围:
两者都是针对的字符串计算大小
C++中 length()函数只能用来获取字符串长度(用于 string),类似于 size()计算的是元素的个数
string str = "ADAS";int len = str.length();//len = 4
复制代码
评论