c strchr函数_linux c stdout_strchr函数php(10)

电脑杂谈  发布时间:2017-02-23 21:04:05  来源:网络整理

}

运行结果:Tsinghua Computer

注意:在定义字符数组1的长度时应该考虑字符数组2的长度,因为连接后新字符串的长度为两个字符串长度之和。进行字符串连接后,字符串1的结尾符将自动被去掉,在结尾串末尾保留新字符串后面一个结尾符。

原型:strncat(char target[], const char source[], int numchars);

功能:将字符串source的前numchars个字符接到字符串target的后面

例程:

#include <iostream.h>

#include <string.h>

void main(void)

{

char str1[] = { "Tsinghua "};

char str2[] = { "Computer"};

cout <<strncat(str1,str2,3)<<endl;

}

运行结果:Tsinghua Com

原型:int strcmp(const char firststring[], const char secondstring);

功能:比较两个字符串firststring和secondstring

例程:

#include <iostream.h>

#include <string.h>

void main(void)

{

char buf1[] = "aaa";

char buf2[] = "bbb";

char buf3[] = "ccc";

int ptr;

ptr = strcmp(buf2,buf1);

if(ptr > 0)

cout <<"Buffer 2 is greater than buffer 1"<<endl;

else

cout <<"Buffer 2 is less than buffer 1"<<endl;

ptr = strcmp(buf2,buf3);

if(ptr > 0)

cout <<"Buffer 2 is greater than buffer 3"<<endl;

else

cout <<"Buffer 2 is less than buffer 3"<<endl;

}

运行结果是:Buffer 2 is less than buffer 1

Buffer 2 is greater than buffer 3

原型:strlen( const char string[] );

功能:统计字符串string中字符的个数

例程:

#include <iostream.h>

#include <string.h>

void main(void)

{

char str[100];

cout <<"请输入一个字符串:";

cin >>str;

cout <<"The length of the string is :"<<strlen(str)<<"个"<<endl;

}

运行结果The length of the string is x (x为你输入的字符总数字)

注意:strlen函数的功能是计算字符串的实际长度,不包括'\0'在内。另外,strlen函数也可以直接测试字符串常量的长度,如:strlen("Welcome")。


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-34103-10.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    • 宋自逊
      宋自逊

      放心找不到老婆的不是我了

    热点图片
    拼命载入中...