C语言:返回传入字符串的长度
2017-07-25 20:29
288 查看
#include <stdio.h> #include <stdlib.h> #include <string.h> //返回传入字符串的长度 int GetStrLength(char[]); //封装fgets,用来接受字符串的字符数组,接受的字符总数 void GetString(char [], int count); void GetString(char str[], int count) { //使用fgets函数接受字符串,使用\0替换字符数组的最后一位\n fgets(str, count, stdin); //返回\n字符所在的指针 char * find = strchr(str, '\n'); //查找换行符 if(find)//如果找到了 *find = '\0'; //根据找到的指针,修改指向的元素为\0 } int GetStrLength(char str[]) { int count = 0;//字符串中的字符个数 int i; while(str[count] != '\0') { if(str[count] == '\n') { str[count] = '\0';//替换 break; } count++; } return count; } int main() { char names1[50]; //fgets(names1, 5, stdin); GetString(names1, 20); int len = GetStrLength(names1); printf("字符串的长度为: %d\n", len); return 0; }
相关文章推荐
- C语言strlen()函数:返回字符串的长度
- C语言strlen()函数:返回字符串的实际长度
- C语言strlen()函数:返回字符串的长度
- 输入一个长度未知的字符串,用动态分配内存的方法(C语言实现)
- c语言,strcmp(),字符串比较,看Asic 码,str1>str2,返回值 > 0;两串相等,返回
- 对于一个字符串,设计一个算法,将包括i位置在内的左侧部分移动到右边,将右侧部分移动到左边。给定字符串A和它的长度n以及特点位置p,请返回旋转后的结果。
- 第25题: 写一个函数,它的原形是int continumax(char *outputstr,char *intputstr),在字符串中找出连续最长的数字串,并把这个串的长度返回
- 在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置
- 求字符串长度之递归与非递归的C语言实现
- 第25题:返回字符串中最长数字字符串的长度
- C语言中计算字符串长度的函数
- 每天学习一算法系列(22)(在字符串中找出连续最长的数字串,并把这个串的长度返回)
- C语言rindex()函数:查找字符串并返回最后一次出现的位置
- C语言里面获取字符串长度sizeof和strlen的区别
- 求字符串长度并返回(第十七周上机任务)
- 【c语言】模拟实现strchr函数,功能:在一个字符串中查找一个字符第一次出现的位置,如果没有出现返回NULL
- C语言strerror()函数:返回错误原因的描述字符串
- 回复:String中如果有中文,如何保证length()返回的长度 在 Java 中字符串是以 Unicode 方式编码的,所以,无论中英文都算的长度为 1。根据你的描述,估计是想要取得英文长度为 1 中文长度为 2 的总长度吧。这个,理论上可以构造 An
- SQL中Int To String,返回指定长度的字符串,不足补0
- 返回字符串真实长度, 1个汉字长度为2