0
点赞
收藏
分享

微信扫一扫

C++NOIP字符串反转

是她丫 2022-04-29 阅读 83
c++

关注1:关于如何数出字符串的一个长度

方法1:sizeof

sizeof :计算储存的字节大小。

其中一个思路,是计算数组中,字符的一个个数

sizeof(num)  /  sizeof( num[0] )

这种方法需要对结果N-1.

#include<iostream>
#include<string.h>
using namespace std;
int reserve(char c);
int main()
{
	char s[]="hello";
	cout<<sizeof(s);
	return 0;	
} 

输出为6

第二种方法:

strlen

需要用到string.h头文件。

推荐这种方式。

#include<iostream>
#include<string.h>
using namespace std;
int reserve(char c);
int main()
{
	char s[]="hello";
	cout<<strlen(s);
	return 0;	
} 

 主程序:

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
	char s[]= "hello world";
	char box;
	for(int i=0;i<(int)(strlen(s))/2;i++)
	{
		box=s[i];
		s[i]=s[strlen(s)-1-i];
		s[strlen(s)-1-i]=box;
	}
	cout<<s;
	return 0;	
} 
举报

相关推荐

0 条评论