C语言常用字符串函数总结

1、将字符串转换为数字

strtol    根据进制转化为 long int型数字,比如要将字符串"1a"转化成16进制数字 0x1a

strtoul  根据进制转化为 unsigned long int 型数字。比如要将字符串"1a"转化成16进制数字 0x1a

atoi  将字符串转化为int型数字。比如要将字符串"123"转化成10进制数字123

/* strtoul */const char* str = "1a";   char* endptr = NULL;   unsigned long int n;   n = strtoul(str, &endptr, 16);   printf("The number(unsigned long int) is: %lu\n", n);   /* strtol */const char* str = "1a";   char* endptr = NULL;   unsigned long int n;   n = strtol(str, &endptr, 16);   printf("The number(long int) is: %l\n", n);   /* atoi */const char* str = "123";     unsigned long int n;   n = atoi(str);   printf("The number(int) is: %d\n", n);   

2、在字符串中查找字符串/字符

char *strstr(const char *haystack, const char *needle)

函数功能:在字符串haystack中查找字符串needle,如果存在则返回第一次出现的字串的首地址,如果不存在返回NULL。

include <stdio.h>
#include <string.h>int main () {const char haystack[20] = "TutorialsPoint";const char needle[10] = "Point";char *ret;ret = strstr(haystack, needle);printf("The substring is: %s\n", ret);return(0);
}# result
The substring is: Point

参考:C library function - strstr()

char *strchr(const char *str, int c)

函数功能:在字符串str中查找字符c,如果存在则返回第一次出现的首地址,如果不存在返回NULL。

#include <stdio.h>
#include <string.h>int main () {const char str[] = "https://www.tutorialspoint.com";const char ch = '.';char *ret;ret = strchr(str, ch);printf("String after |%c| is - |%s|\n", ch, ret);return(0);
}# result
String after |.| is - |.tutorialspoint.com|

参考:C library function - strchr()

3、根据分隔符,将字符串分割成一组字串

比如这种字符串  "20 10 0f"    "1a:2f:5c:26:34:56" ,如果分割后转化成数字,一般会搭配strtoul  使用。

char *strtok(char *str, const char *delim)
  • str − 要被分割的字符串,再次调用要把str设为NULL,If str is NULL, the saved pointer in SAVE_PTR is used as the next starting point.

  • delim − This is the C string containing the delimiters. These may vary from one call to another.

 Breaks string str into a series of tokens using the delimiter delim. 返回第一个被找到的token的首地址。如果后面没有了,则返回NULL。

#include <string.h>
#include <stdio.h>int main () {char str[80] = "This is - www.tutorialspoint.com - website";const char s[2] = "-";char *token;/* get the first token */token = strtok(str, s);/* walk through other tokens */while( token != NULL ) {printf( " %s\n", token );token = strtok(NULL, s);}return(0);
}#resultThis is www.tutorialspoint.com website

参考:C library function - strtok()

4、字符串连接

char *strcat(char *dest, const char *src)

Appends the string pointed to by src to the end of the string pointed to by dest

This function returns a pointer to the resulting string dest.

  • dest − This is pointer to the destination array, which should contain a C string, and should be large enough to contain the concatenated resulting string.

  • src − This is the string to be appended. This should not overlap the destination.

#include <stdio.h>
#include <string.h>int main () {char src[50], dest[50];strcpy(src,  "This is source");strcpy(dest, "This is destination");strcat(dest, src);printf("Final destination string : |%s|", dest);return(0);
}
char *strncat(char *dest, const char *src, size_t n)将src前n个字节 append到dest
  • dest − This is pointer to the destination array, which should contain a C string, and should be large enough to contain the concatenated resulting string which includes the additional null-character.

  • src − This is the string to be appended.

  • n − This is the maximum number of characters to be appended.

#include <stdio.h>
#include <string.h>int main () {char src[50], dest[50];strcpy(src,  "This is source");strcpy(dest, "This is destination");strncat(dest, src, 10);printf("Final destination string : |%s|", dest);return(0);
}#resultFinal destination string : |This is destinationThis is so|

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/223510.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C/C++ 知识点:类成员初始化方法

一、类成员初始化方法 C支持的类成员初始化方法有&#xff1a;初始化列表、构造函数初始化、声明时初始化&#xff08;C11后才支持&#xff09;。从C11之后&#xff0c;这三种初始化的方法都可以使用&#xff0c;并不会存在冲突&#xff0c;但是&#xff0c;他们之间是有优先级…

ChatGPT对话为什么不用WebSocket而使用EventSource?

文章目录 1. 引言2. WebSocket和EventSource简介2.1 WebSocket2.2 EventSource 3. ChatGPT对话系统的特点4. EventSource的优势4.1 简单易用4.2 容错性强4.3 兼容性良好 5. 为何选择EventSource而非WebSocket&#xff1f;5.1 单向通信模式5.2 长轮询模式5.3 简化部署和维护 6. …

Git 命令一览

一&#xff0c;常用操作 # 将所有修改的文件从工作区放入暂存区 git add ./ --> 放入暂存区 # 添加commit信息&#xff0c;文件从暂存区提交到本地仓库中 git commit -m xxx --> 提交到本地仓库 # 拉取远程主机某个分支&#xff0c;再与本地分支合并 git pull --&g…

SpringBoot+Netty+Websocket实现消息推送

这样一个需求&#xff1a;把设备异常的状态每10秒推送到页面并且以弹窗弹出来&#xff0c;这个时候用Websocket最为合适&#xff0c;今天主要是后端代码展示。 添加依赖 <dependency><groupId>io.netty</groupId><artifactId>netty-all</artifact…

用Go汇编实现一个快速排序算法

本代码全网首发&#xff0c;使用Go plan9 windows arm64汇编&#xff0c;实现基础版快速排序算法。 未引入随机因子的快速排序的普通Go代码长这样。 func QuickSort(arr []int) {if len(arr) < 1 {return}base, l, r : arr[0], 0, len(arr)-1for i : 1; i < r; {if arr…

软件工程 课堂测验 简答

结构化的软件设计的工具有哪些&#xff1f;各有什么特点&#xff1f; 表示软件结构的图形工具&#xff1a; 1&#xff09;层次图和HIPO图&#xff1a;层次图描绘软件的层次结构&#xff0c;一个矩形框代表一个模块&#xff0c;框间的连线表示调用关系&#xff0c;每个方框可带编…

Vue H5项目,怎么引入uni.webview sdk,调用uni postMessage实现手机蓝牙连接打印功能(uniapp)

前言 目前公司Vue H5项目&#xff0c;用webview打包成APP&#xff0c;现产品提出这样打包出来的app运行较慢&#xff0c;需要用uniapp方式&#xff08;即使用HBuilder编辑器来打包H5&#xff09;来打包&#xff0c;那需要的基座就不是安卓的基座而是uniapp的基座&#xff0c;而…

扭矩法、屈服点法哪个比较高效?——SunTorque智能扭矩系统

在机械制造和维修领域&#xff0c;拧紧螺栓和螺母是一项重要的操作。拧紧方法的合理选择和使用&#xff0c;对于确保机械设备的稳定性和安全性具有至关重要的作用。本文SunTorque智能扭矩系统将介绍两种最常用的拧紧方法&#xff0c;并探讨它们的轴力范围计算方法。一、扭矩法 …

电信网关配置管理系统后台 upload.php 文件上传漏洞复现

0x01 产品简介 中国电信集团有限公司(英文名称“China Telecom”、简称“中国电信”)成立于2000年9月,是中国特大型国有通信企业、上海世博会全球合作伙伴。 0x02 漏洞概述 电信网关配置管理系统后台 /manager/teletext/material/upload.php 接口存在文件上传漏洞,攻击者…

open3D绘制圆柱形和长方体

import time import open3d as o3d; import numpy as np;#圆柱形 mesh_cylinder o3d.geometry.TriangleMesh.create_cylinder(radius0.3,height4.0) mesh_cylinder.compute_vertex_normals() mesh_cylinder.paint_uniform_color([0.1, 0.4, 0.1]) o3d.visualization.draw_geo…

基于JAVA(springboot框架)在线拍卖竞拍系统 毕业设计开题报告

博主介绍&#xff1a;《Vue.js入门与商城开发实战》《微信小程序商城开发》图书作者&#xff0c;CSDN博客专家&#xff0c;在线教育专家&#xff0c;CSDN钻石讲师&#xff1b;专注大学生毕业设计教育和辅导。 所有项目都配有从入门到精通的基础知识视频课程&#xff0c;免费 项…

【数电笔记】56-消抖开关

目录 说明&#xff1a; 1. 按键抖动形成的原因 2. 按键消抖的方法 3. 用与非RS触发器构成消抖开关&#xff08;硬件消抖&#xff09; 说明&#xff1a; 笔记配套视频来源&#xff1a;B站本系列笔记并未记录所有章节&#xff0c;只对个人认为重要章节做了笔记&#xff1b;标…

飞天使-linux操作的一些技巧与知识点5-ansible之roles

文章目录 roles批量替换文件 role 的依赖关系role 的实际案例 roles tasks 和 handlers &#xff0c;那怎样组织 playbook 才是最好的方式呢&#xff1f;简 单的回答就是&#xff1a;使用 Roles Roles 基于一个已知的文件结构&#xff0c;去自动的加载 vars&#xff0c;tasks 以…

【稳定检索|投稿优惠】2024年艺术鉴赏与社会科学教育国际会议(ICAASSE 2024)

2024年艺术鉴赏与社会科学教育国际会议(ICAASSE 2024) 2024 International Conference on Art Appreciation and Social Science Education(ICAASSE) 一、【会议简介】 2024年艺术鉴赏与社会科学教育国际会议(ICAASSE 2024)&#xff0c;这场学术盛宴&#xff0c;将于2024年2月1…

系列四、DML

一、DML 1.1、概述 DML的英文全称是Data Manipulation Language&#xff0c;中文意思为&#xff1a;数据操作语言&#xff0c;用来对数据库表中的数据进行增、删、改操作&#xff0c;这些操作大家在日常工作中常用&#xff0c;这里不再赘述。

郝斌C语言自学教程笔记

赫斌C语言——笔记目录 c语言编程预备知识流程控制函数变量指针结构体位运算符 前段时间康哥看我C语言基础不牢,推荐我学习郝斌老师的C语言课程&#xff0c;花2周看完之后发现确实是目前所看的C语言课程中最好的&#xff0c;不仅非常适合入门&#xff0c;而且对即使学了几年C语…

怒斥以色列后突发心脏病倒地,土耳其议员抢救无效身亡!

这两天互联网上热传一段视频&#xff0c;说的就是土耳其议员在议会演讲时突然倒地晕厥&#xff0c;两天后就去世了。这可真是让人震惊啊&#xff01; 据说这位议员是土耳其反对党幸福党的&#xff0c;名字叫比特梅兹。他在议会发表批评以色列的言论时&#xff0c;情绪过于激动…

安装2023最新版Java SE 21.0.1来开发Java应用程序

安装2023最新版Java SE 21.0.1来开发Java应用程序 Install the latest version of Java SE 21.01 to Develop Java Applications By JacksonML 本文简要介绍如何下载和安装2023年最新版Java Development Kit (简称JDK&#xff0c;即Java开发工具包标准版&#xff09;21.0.1&…

android webrtc入门教程一(简单一对一通话实现)

webrtc Android入门非常的简单不要被那些博客给带乱了&#xff0c;我看了几篇这方面的博客都是给你零散的代码更本就不能实现通话&#xff0c;学这个要先从全局流程再到详细步骤来学习。 简单介绍下实现webrtc通话总体流程并且给出全部代码&#xff0c;复制粘贴即可 本文福利&…

长尾问题之LDAM

做法&代码&公式 step1: 全连接层的权重W和特征向量X都归一化,相乘 W * X P (得到各个类别的概率) # 定义权重&#xff0c;初始化 weight nn.Parameter(torch.FloatTensor(num_classes, num_features)) weight.data.uniform_(-1, 1).renorm_(2, 1, 1e-5).mul_(1e5)#…