itoa、atoi strchr

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值