文章目录
- 前言
- 一、题目描述
- 二、题目分析
- 三、解题
- 程序运行代码
前言
本系列为一维字符数组编程题,点滴成长,一起逆袭。
一、题目描述
求字符串长度, 占内存字节数
二、题目分析
求字符串长度
法一:
while(str[i]!=‘\0’){
i++;
}
printf(“%s’s long is %d\n”,str,i);
法二:
include<string.h>
printf(“%s’s long is %d\n”,str,strlen(str));
三、解题
程序运行代码
#include<stdio.h>
#include<string.h>
int main() {char str[5]={'h','e','l','l','o'};char str1[]="hello"; int i,j;while(str[i]!='\0'){i++;}printf("%s's long is %d\n",str,i);//字符串长度//printf("%s's long is %d\n",str,strlen(str));printf("%s's sizeof is %d\n",str,sizeof(str));//字符串占内存字节数printf("\n");//putchar('\n');return 0;
}