C/C++常用函数

1.strrchr函数

原型:char *strrchr(const char *str, char c);

 

#include<string.h>

 

找一个字符c在另一个字符串str中末次出现的位置(也就是从str的右侧开始查找字符c首次出现的位置),并返回从字符串中的这个位置起,一直到字符串结束的所有字符。如果未能找到指定字符,那么函数将返回NULL。

 

The strrchr function finds the last occurrence of c (converted to char) in str. The search includes the terminating null character.

DEMO:

 

#include <stdio.h>  
#include <conio.h>  
#include <string.h>  
#pragma warning (disable:4996)  
int main(void)  
{  
    char str[20];  
    char *ptr;  
    char c='r';  
    strcpy(str,"There are two rings");  
    ptr=strrchr(str,c);  

    if (ptr!=NULL)  
    {  
        printf("The character %c is at position:%s\n",c,ptr);  
    }  
    else  
    {  
        printf("The character was not found\n");  
    }  
    getch();  
    return 0;  
}  

2.strstr函数

原型:char *strstr(const char *str1, const char *str2);

#include<string.h>

找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)。返回该位置的指针,如找不到,返回空指针。

Returns a pointer to the first occurrence of strSearch in str, or NULL if strSearch does not appear in str. If strSearch points to a string of zero length, the function returns str.

3.strcat函数

原型:extern char *strcat(char *dest,char *src);

#include<string.h>

把src所指字符串添加到dest结尾处(覆盖dest结尾处的'\0')。

src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。
 

 

posted on 2018-03-27 20:32  凉城飞飞  阅读(161)  评论(0)    收藏  举报

导航