C语言基础之字符串(附测试代码)

正则表达式规则及C语言测试代码 正则表达式规则 规则数据来源于runoobcom,如果侵权,请告知删除,谢谢。 -非打印字符 字符 描述 \f 匹配一个换行符,等价于\x0x和\cL \n 匹配一个换行符,等价于\0x0a和\cJ \r 匹配一个回车符,等价于\x0d和\cM \t 匹配一个制表符,等价于\0x09和\cl \v 匹配一个垂直制表符,等价于\x0b和\cK \s 阅读详情

C语言提供一组标准函数用来处理字符串,为了使用这些标准函数需要包含string.h头文件。
常用字符串处理函数
1、常用字符串处理函数如下:
这里写图片描述

使用ubuntu系统写的测试代码如下:

  1 #include<stdio.h>
  2 #include<string.h>
  3 int main()
  4 {
  5         char str[20]="abcdef";
  6         printf("Length = %d\n",strlen(str));
  7         printf("sizeof(str)= %d\n",sizeof(str));
  8         printf("strcat = %s\n",strcat(str,"xy"));
  9         printf("str = %s\n",str);
 10         printf("strcmp = %d\n",strcmp("abc","abcd"));
 11         printf("strcpy=%s\n",strcpy(str,"123"));
 12         printf("str = %s\n",str);
 13         memset(str,'H',10);
 14         printf("%s \n",str);
 15         printf("%s\n",strstr("abcdefg","def"));
 16         return 0;
 17 }

测试输出结果如下:
这里写图片描述

2、其他字符串相关函数:
sprintf 按格式把多个数字打印到字符数组里形成字符串
sscanf 从字符串里获得多个数字并记录到变量里

3、字符串里的数字转换成数字类型的函数如下,使用它们需要包含stdlib.h头文件:
atoi 把字符串里的整数数字转换成整数类型
atof 把字符串里的浮点数转换成浮点类型

4、fgets函数可以用来从键盘得到一个字符串并记录到字符数组里,这个函数右三个参数:
1.数组名称
2.数组里存储区的个数
3.用stdin代表键盘

C语言字符、字符串与整型之间的转换 C语言的字符转、字符与整型、浮点型之间的转换 阅读详情

相关推荐

C语言检查一个字符串是否为另一个字符串的子字符串的算法(附完整源码)

C语言检查一个字符串是否为另一个字符串的子字符串的算法C语言检查一个字符串是否为另一个字符串的子字符串的算法完整源码(定义,实现,main函数测试) C语言检查一个字符串是否为另一个字符串的子字符串的算法完整源码(定义,实现,main函数测试) #include <iostream> #include <string> bool isRotation( std::string s1, std::string s2 ) { size_t len1 = s1.length(); s

希望我的博客,能帮上你解决学习中工作中所遇到的问题 1701

C语言:测试代码1

#include <stdio.h> #include <time.h> #include <math.h> clock_t start , stop; double duration; #define MAXN 10 double f1(int n , double a[] , double x); double f2(int n , double a[] , double x); double f1(int n , double a[] , double x) {

零小唬的博客 2480

Vue+ECharts-GL 3D地图实战:5层叠加打造炫酷中国地图(附完整代码)

本文详细介绍了如何利用Vue和ECharts-GL构建具有强烈立体感的3D中国地图。通过5层图层的巧妙叠加与样式调校,实现了从背景阴影到交互高光的完整视觉层次,并整合了动态飞线与散点标记。文章提供了完整的代码实现、核心配置解析以及性能优化与视觉调优的实战技巧,帮助开发者快速打造专业级的交互式地图可视化应用。

d6e7f8g9h的博客 651

【C语言】判断字符类型的三种方法

有关判断字符类型的三种方法:库函数法,ASCII码判断法和字符直接判断法详解以及完整代码实现.

C/C++领域,数据结构,linux,MySQl学习者 2万+

西门子S7-200 SMART与施耐德ATV变频器Modbus通讯实战:从接线到稳定运行的完整指南

本文详细介绍了西门子S7-200 SMART PLC与施耐德ATV变频器之间实现稳定Modbus通讯的完整实战指南。内容涵盖从硬件接线、参数配置、PLC程序编写到触摸屏设计的全流程,并分享了提升通讯稳定性的关键技巧和常见故障排查方法,助力工业自动化系统可靠运行。

kkk56的博客 1106

百度智能云训练图像分割模型+python调用模型并分割图像

目录 训练模型阶段 发布图像分割API 调用图像分割API 创建应用后显示以下两个代码 在pycharm中可以调用API mask解码 依赖库pycocotools安装步骤: 依赖库urllib2的安装 源码下载: 最近上课听老师介绍了一个训练图像分割模型的网站,把数据集上传之后做图像标注,然后进行训练就能得到准确率等指标。 训练模型阶段 图像分割:https://ai.baidu.com/easydl/vision/ 图像分割-->创建模型-->上传数据--&gt

icecreamdinner的博客 1388

测试电脑性能(c语言简单应用)

使用c语言的递归测试电脑性能。

m0_73345579的博客 1599

FPGA+CMV4000实战:从零搭建20fps成像系统的5个关键步骤(附避坑指南)

本文详细介绍了如何从零搭建基于FPGA和CMV4000传感器的20fps成像系统,涵盖硬件选型、SPI通信优化、LVDS数据采集、DDR2内存管理及系统集成等5个关键步骤。特别针对CMV4000传感器的SPI配置和高速数据处理的常见问题提供实用解决方案,帮助开发者避免常见陷阱,提升成像系统性能。

weixin_42628688的博客 427

C语言实验代码

【代码】C语言实验代码。

qq_48257021的博客 996

【黑马点评优化】3-压测秒杀接口,算出性能塌陷区,之后用sentinel实现秒杀接口限流

【黑马点评优化】3-压测秒杀接口,算出性能塌陷区,之后用sentinel实现秒杀接口限流

m0_52031708的博客 3570

如何借助GPT- 6 写好一篇论文:从引言到参考文献完整步骤(附专业提示词)

七哥就结合这篇文章的写作思路,依次拆解引言、方法、结果、讨论、摘要、标题和参考文献分别应该怎么写,同时也聊聊,在每一个环节里,我们可以怎样合理借助 GPT-6,让论文写作变得更清晰、更高效。可以和七哥交流学术写作或Gemini、GPT、Claude 等大模型 学术实操相关问题,多多交流,相互成就,共同进步。8、不修改、不补写、不猜测任何样本量、阈值、剂量、设备型号、参数或统计方法,所有缺失但影响方法完整性的信息统一标记为【待作者确认】、检查方法中设定的每一个分析项目能否在后续结果章节找到对应位置。

智能AI让学术科研更容易 110

C 测试程序

int main()return 0;#endifreturn a+b;#endif./main。

weixin_58619062的博客 524

华为OD机试真题 新系统2026-04-01 C语言 实现【空间占用计算】

员工A的磁盘空间经常被耗尽,他需要找到占用空间最大的目录或文件,然后决定如何清理文件释放空间。给定某一目录,请编写程序帮助他统计该目录内一级子目录和文件的占用空间,并返回目标目录一级子项(文件或子目录)中占用空间最大的项。

MISAYAONE的博客 231

《C语言深度解剖》附录解析,附测试代码

《C语言深度解剖》附录解析,附测试代码前言1.隐式转换2.字符数组strcpy()函数3.static的作用4.sizeof求字节大小5.strlen()函数6.常量指针和指向常量的指针7.volatile8.数组与指针9.运算符优先级10.宏定义函数11.结构体字节数12.内存地址写值13.数组与指针14.结构体大小与数据类型15.数组与逗号表达式16.数组与指针17.栈和指针18.递归19.getchar()20.大小端与union 前言   即将面对各种招聘会场,即将迎来各种面试题目,师兄推荐我一本书

qq_22038849的博客 1646

OpenClaw 超级 AI 实战专栏【实战案例】(五)邮件收发 + 日历日程 + 文件管理一站式搞定

OpenClaw 实现办公自动化核心是模块解耦:邮件收发、文件管理、日历日程各自封装,便于维护和扩展;开发中需重点解决编码兼容、权限配置、时间格式、重复处理四大类坑点,核心思路是 “提前校验 + 异常兜底 + 统一格式”;敏感信息(邮箱密码、API Token)务必通过.env管理,避免硬编码泄露。

qq_36130719的博客 742

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

fgets()函数的详解-使用技巧-C语言基础

这篇文章要探讨的是“fgets()函数的详解以及使用时需要注意的一些细节”。涉及fgets()函数的应用和需要注意的问题。属于C语言基础篇(持续更新)。 fgets()(函数原型:char *fgets(char *restrict str, int size, FILE *restrict stream)) 这个函数原型不太好看出个所以然来,可以理解为(char *fgets(“容器的地址”, “容器的大小”, “从哪里读取”)) 一般用法: char a[100] = {0}; fgets

Devour_的博客 8万+

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

16进制与字符串相互转换(C语言)

编译语言: C 编译工具链:MinGW GCC 编译环境:eclipse 字符串转换成16进制函数 先写一个字符转大写的函数lowtocap (也可以直接调用C库函数toupper,需#include <ctype.h>) /*lowercase letters transform to capital letter*/ char lowtocap(char c) { ...

kkconan的博客 1万+

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

C语言获取字符串长度的几种方法

在C语言中,想要获取字符串长度可以有很多方法,下面分别介绍 一、使用sizeof()运算符 在C语言中,sizeof() 是长度的运算符,括号中可以放入数据类型或者表达式,一般我们用来计算字符串长度。 基本用法: int i=10; sizeof(i);//表达式 char str[]="hello world"; sizeof(str); sizeof(double);//数据类型 在使用sizeof()求字符串长度时,会将 ‘\0’ 也计算到字符串长度中。例如"abcd"用sizeof()求长度会

st66688的博客 20万+

fgets()函数的详解以及使用时需要注意的一些细节-C语言基础

这篇文章要探讨的是“fgets()函数的详解以及使用时需要注意的一些细节”。涉及fgets()函数的应用和需要注意的问题。属于C语言基础篇(持续更新)。 fgets()(函数原型:char *fgets(char *restrict str, int size, FILE *restrict stream)) 这个函数原型不太好看出个所以然来,可以理解为(char *fgets(“容器的地址”...

因为自己也淋过雨,所以想给别人撑把伞。 1万+

C语言版Tokenize()函数,由分隔符获取字符串

C语言版Tokenize(),类似MFC/VC++中的CString类的方法Tokenize(),搜索分隔符,返回分隔符前的字符串,每一次搜索结束后uPos保存分隔符的下一个位置

C语言是最好的编程语言 6767

c语言字符串功能,用C语言实现字符串替换功能

下面是用C语言实现字符串替换功能的代码:char  *replace(char  *source,  char  *sub,  char  *rep){char  *result;/*pc1  是复制到结果result的扫描指针*//*pc2  是扫描 source 的辅助指针*//*pc3  寻找子串时,为检查变化中的source是否与子串相等,是指向sub的扫描指针 *//*找到匹配后,为了复...

weixin_42099633的博客 1146

【C基础】空字符'\0'与字符串

字符'NUL':字符'NUL'即空字符,是一个ASCII值为0的控制字符,在程序代码中通常以转义序列'\0'表示,在C语言中,空字符非常重要,主要体现在字符串中。

bairenxinmo的专栏 1万+

C语言中字符串的创建

  在C中创建字符串的方法有两种,即通过指针创建或通过数组创建。                 通过指针创建   创建语句: 1 char *str = "abc";   这句代码首先创建一个指针,再将字符串赋值给指针,其实这句代码可以写为两句,即: 1 char *str; 2 str = "abc";   为什么第2句代码可以成立,可以这样来解释,在C语言中赋值...

weixin_30657541的博客 1171

由浅入深C系列六:C中实现字符串trim的功能

一个项目中,需要用c语言实现对字符串中的字定字符进行过滤并从字符串的删除,查询了C语言的基本库,没有发现有这样的函数,于是发挥程序员的主观能力性,自力更生就啥都有了。

招财猫_Martin 的专栏 687

C语言字符串和数字转换函数大全_c语言字符串转化为数字

另外在 C99 / C++11 规范中又新增了5个函数,分别是 atoll()、strtof()、strtold()、strtoll()、strtoull(),在此不做介绍,请大家自行学习。另外在 C99 / C++11 规范中又新增了5个函数,分别是 atoll()、strtof()、strtold()、strtoll()、strtoull(),在此不做介绍,请大家自行学习。函数来检测),直到遇上数字或正负符号才开始做转换,到出现非数字或字符串结束时(’\0’)才结束转换,并将结果返回。

2401_89284222的博客 1923
上一篇: C语言基础之基本数据类型
下一篇: C语言基础之常用文件操作1(附代码)
Coding13
博客等级 码龄10年 53粉丝 43原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coding13

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值