itoa、atoi strchr

来源:互联网 发布:引用60分钟数据 编辑:程序博客网 时间:2024/06/06 15:03

atoi (表示 ascii to integer)是把字符串转换成整型数的一个函数,应用在计算机程序和办公软件中。

itoa是广泛应用的非标准C语言和C++语言扩展函数。由于它不是标准C/C++语言函数,

所以不能在所有的编译器中使用。但是,大多数的编译器(如Windows上的)通常

在<stdlib.h>/<cstdlib>头文件中包含这个函数。

功能:将任意类型的数字转换为字符串。在<stdlib.h>中与之有相反功能的函数是atoi。


#include <iostream>using namespace std;int main(){    char *data="13246";    int var=atoi(data);//字符串型转为整型    cout<<var<<endl;    int var1=1234;    char buf[100];    itoa(var1,buf,16);//可将整型转为其他进制!!!但是是个非标准化库函数    cout<<buf;    return 0;}

strchr函数原型:extern char *strchr(const char *s,char c);查找字符串s中首次出现字符c的位置。

#include <iostream>#include <string.h>using namespace std;int find_count(char *p,char ch){    int count=0;    while(p=strchr(p,ch))//通过p来更新查找位置(依次向后推进)    {        count++;        p++;//将当前指针向后移动一个位置,表示    }    return count;}int main(){    char  buf[1024]="great wall great wall eee";    char ch = 'e';    int num=find_count(buf,ch);    cout<<"字符串中"<<ch<<"的个数"<<num<<endl;    char *p=strchr(buf,ch);//找到第一个目标元素的位置并返回    if(p==NULL)    {        cout<<"find none"<<endl;    }    else cout<<"finded"<<endl<<p;    return 0;}





0 0