首页 > 编程知识 正文

c语言中strlen函数,strlen函数和sizeof

时间:2023-05-03 23:18:27 阅读:11115 作者:4327

c语言字符串处理函数strlen'c语言字符串处理函数strlen (

引言

c库提供了多个字符串处理函数,ansi c将这些函数的原型放置在string.h头文件中。 其中最常用的是strlen ()、strcat ()、strcmp ()、strncmp ()、strcpy ()、strncpy () )。 sprintf (),其原型位于stdio.h头文件中。 所有这些函数都将在稍后的博客中详细分析。

strlen ()函数

strlen )函数用于计数字符串的长度,并对包含空格和标点的字符进行计数。 不计算空白字符0。 请注意区分sizeof运算符。 sizeof返回运算对象(变量名、类型名等)的大小(以字节为单位)。

示例:

/* test _ fit.c-- try the string-shrinking function * /

#包含

# include/* containsstringfunctionprototypes * /

语音匹配(char *,unsigned int );

输入主(语音) )。

{

charmesg [ ]=' thingsshouldbeassimpleaspossible,'

' but not simpler.';

puts(mesg;

fit(mesg,38 );

puts(mesg;

puts (let ' slookatsomemoreofthestring.';

puts(mesg39;

返回0;

}

voidfit(char*string,unsigned int size ) ) ) ) ) ) ) ) )。

{

if (字符串大小)。

string[size]=' ';

}

该程序的输出如下。

thingsshouldbeassimpleaspossible,but not simpler。

thingsshouldbeassimpleaspossible

let's look at some more of the string。

but not simpler。

fit ) )函数用空字符替换了第39个元素的逗号。 puts ) )函数在出现空字符时停止输出,而忽略其余字符。 但是,这些字符仍在缓冲区中,下一个函数调用将打印这些字符。

puts(mesg8;

string.h头文件包含c字符串函数系统的原型,因此程序中包含该头文件。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。