基础代码|字符串操作
admin
2023-08-16 11:18:54
0

1.反转字符串:

define NUM 80 #include int main(){ char cc[NUM],c='a'; int i=0,ncount=0; printf("please input a line char:\n"); gets(cc); printf("\n\nyou input:%s\n",cc); for(i=0; cc[i]!='\0'; i++){ ncount++; } printf("the length of %s is %d\n",cc,ncount); for(i=0; i

2.输入字符串的多种方式:
#define NUM 80 #include int main(){ char cc[NUM],bb[NUM],c; int i=0,ncount=0; printf("please input a line char: \n"); scanf("%s",cc); printf("\n\nyou input :%s\n",cc); for(i=0; cc[i]!='\0'; i++){ if(cc[i]>='A'&&cc[i]<'A'+25){ cc[i]=cc[i]+32; } } printf("after to lower Case:%s\n",cc); //方法一遇到空格就结束输入 //方法二 printf("\nplease input another string:\n "); gets(bb); printf("\nanother:\n%s\n",bb); for(i=0; bb[i]!='\0'; i++){ if(bb[i]>='A'&&bb[i]<'A'+25){ bb[i]=bb[i]+32; } } printf("\nafter to lower case:\n%s\n",bb); }

基础代码|字符串操作
文章图片




方法三
#define NUM 80 #include #include int main(){ char cc[NUM],c; int i=0,ncount=0; while(ncount='a'&&cc[i]<97+25){ cc[i]=cc[i]-32; } } printf("after tocUpper Case:%s\n",cc); }

基础代码|字符串操作
文章图片

3.在字符串中查询某个字符:
#define NUM 80 #include int main(){ char cc[NUM],c='a'; int i=0,nCount=0,nPos[NUM]; printf("please input a line char:\n"); gets(cc); printf("please input a char:\n"); c=getchar(); printf("you input:%s\n",cc); printf("you will find :%c\n",c); while(i



4.找到并删除字符
#define NUM 80 #include int main(){ char cc[NUM],c='a'; int i=0,j=0; printf("please input a line char:\n"); gets(cc); printf("please input a char:"); c=getchar(); printf("\nyou input:%s\n",cc); printf("you will find :%c\n",c); while(i

基础代码|字符串操作
文章图片

#define NUM 80 #include int main(){ char cc[NUM],c='a'; int i=0,j=0,nCount=0,nPos[NUM]; printf("please input a line char:\n"); gets(cc); printf("please input a char:"); c=getchar(); printf("\nyou input :%s\n",cc); printf("you will find :%c\n",c); while(i

基础代码|字符串操作
文章图片

5.找到字符串出现的位置
(1)找到一个字符串首次出现的位置
#define NUM 256 #include int main(){ char cc[NUM],findC[NUM]; int i=0,nPos=0,j=0,lensub=0; printf("please input a line char:\n"); gets(cc); printf("please input a find string:"); gets(findC); printf("\nyou input:%s\n",cc); printf("you will find :%s \n",findC); for(i=0; (findC[i]!='\0'&& i0){ break; } i++; } if(nPos>0){ printf("the position:%d\n",nPos); } else{ printf("not found!"); } }


基础代码|字符串操作
文章图片

【基础代码|字符串操作】(2)找到一个字符串各次出现的位置
#define NUM 256 #include int main(){ char cc[NUM],findC[NUM]; int i=0,nPostmp=0,j=0,lensub=0,nCount=0,nPos[NUM]; printf("please input a line char:\n"); gets(cc); printf("please input a find string:"); gets(findC); printf("\nyou input :%s\n",cc); printf("you will find :%s\n",findC); for(i=0; (findC[i]!='\0'&&i

基础代码|字符串操作
文章图片

6.删除一个子串各次出现
(1)使用复制到新数组的方法:
#define NUM 256 #include int main(){ char cc[NUM],findC[NUM],newcc[NUM]; int i=0,nPos[NUM],nCount=0,j=0,lensub=0,nPostmp=0,k=0; printf("please input a line char:\n"); gets(cc); printf("please input a find string:"); gets(findC); printf("\nyou input\n"); printf("%s\n",cc); printf("you will find:%s \n",findC); for(i=0; (findC[i]!='\0'&& i

基础代码|字符串操作
文章图片

(2)直接在原数组中移动以删除子串:
#include #define NUM 256 int main(){ char cc[NUM],newcc[NUM],findC[NUM]; int i=0,nPos[NUM],nCount=0,j=0,lensub=0,nPostmp=0,k=0; printf("please input a line char:\n"); gets(cc); printf("please input a find string:"); gets(findC); printf("you will find:%s\n",findC); for(i=0; i

基础代码|字符串操作
文章图片




7.在字符串的指定位置插入字符串
#define NUM 256 #include int main(){ char cc[NUM],findC[NUM]; int i=0,j=0,insertIndex=0,ncc=0,nfindc=0; printf("please input the line char:"); gets(cc); printf("please input the insert string:"); gets(findC); printf("%s\n",cc); scanf("%d",&insertIndex); for(i=0; (findC[i]!='\0'); i++); nfindc=i; for(i=0; (cc[i]!='\0'); i++) ncc=i; ncc++; for(i=ncc; i>=insertIndex; i--){ cc[i+nfindc]=cc[i]; } for(i=0; i

基础代码|字符串操作
文章图片

8.获取字符串的一部分
#define NUM 256 #include int main(){ char cc[NUM],findC[NUM]; int i=0,j=0,iStart=0,iEnd=0; printf("please input a line char:\n"); gets(cc); printf("%s\n",cc); printf("please input start position,end position:"); scanf("%d %d",&iStart,&iEnd); for(i=iStart; (i

基础代码|字符串操作
文章图片

9.将两个字符串连接起来
#define NUM 256 #include int main(){ char cc[NUM],cc2[NUM]; int i=0,j=0,lencc=0,lencc2=0; printf("please input a first string:\n"); gets(cc); printf("please input a secondary string:\n"); gets(cc2); for(i=0; cc[i]!='\0'; i++){ lencc=i; } lencc++; for(i=0; cc2[i]!='\0'; i++){ lencc2=i; } lencc2++; for(i=0; i

基础代码|字符串操作
文章图片

10.两个字符串进行比较


上一篇:jQuery插件

下一篇:雅集

相关内容

热门资讯

a... 本文目录导航: abaqus振动照应剖析中处罚力的加载方法 abaqus反...
a... 本文目录导航: abaqus资料可以不设置参数吗 abaqus外面没有自带...
A... 本文目录导航: ANSYS Workbench中二维平面剖析的设置。 an...
h... 本文目录导航: hfss19.2允许win11吗 微带如何假负载 ...
a... 本文目录导航: ansys模流剖析用哪个模块 Ansys_workbenc...
v... 本文目录导航: 在ansys中,建好几何模型后,随意变动一下视图,旋转、加大、增加,Fit...
A... 本文目录导航: Ansys软件可以收费试用吗? ansys有哪些软件 ...
a... 本文目录导航: abaqus中如何同时关上两个cae文件? abaqus找...
a... 本文目录导航: ansys流体剖析迭代次数可以中止嘛 Ansys Flot...
这... 本文目录导航: 哪位大佬有 Fluent14.5流场剖析从入门到知晓,这种百度网盘资源的链...
经... 双击D2进入System Coupling的设置,在Solution上右击选用Clear Gener...
a... 本文目录导航: ansys软件引见 ansys有哪些软件 ...
a... 本文目录导航: abaqus系列技巧18:聊聊abaqus云图外面的魔术加大镜--缩放倍数...
a... 本文目录导航: abaqus中网格不可绘制 abaqus结果可视化某个部件...
a... 本文目录导航: abaqus结果可视化某个部件云图只显示网格接壤处的? a...
驳... 本文目录导航: ABAQUS中假设选择施加的载荷类型为pressure,对应的载荷单位应该...
a... 本文目录导航: abaqus2020旋转不了物体 怎样判别abaqus转的...
在... 本文目录导航: 在abaqus中如何删除一个单元 ABAQUS怎样删除网格...
它... 本文目录导航: abaqus为什么叫abaqus,它是那些词的简称么?全称是什么呢? ...
披着“现货订购”外衣的非法期货...   近期,甘肃西北商品交易市场有限公司旗下的“铂银掘金APP”引发大量投资者集中维权。该平台打着“低...