C/C++size(),sizeof(),length(),strlen() 对比分析详解

 

题目

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 true
solution('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
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CtrlCherry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值