基础代码|字符串操作
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.两个字符串进行比较
推荐阅读
- CVE-2020-16898|CVE-2020-16898 TCP/IP远程代码执行漏洞
- 一起来学习C语言的字符串转换函数
- Python基础|Python基础 - 练习1
- 不废话,代码实践带你掌握|不废话,代码实践带你掌握 强缓存、协商缓存!
- 工具|后天就是七夕节,你准备好了吗(送上几个七夕代码,展示你技能的时候到了!)
- Java|Java基础——数组
- 字符串拼接成段落,换行符(\n)如何只执行n-1次
- 《机器学习实战》高清中文版PDF英文版PDF+源代码下载
- Java基础-高级特性-枚举实现状态机
- 营养基础学20180331(课间随笔)??