当前位置:网站首页>C中字符串基本操作
C中字符串基本操作
2022-06-26 09:36:00 【later_rql】
C中字符串基本操作
(1)采用静态数组来存储字符串
//通过牺牲0位,使得字符的存储与实际位置保持一致
//静态存储
typedef struct Str{
char ch[MAXLEN];
int length;
}SString;
(2)主要操作方法:
//字符串的初始化
SString InitSString(SString &str);
//判断字符串是否为空
int IsEmpty(SString str);
//字符串赋值(把串str赋值为chars)
void StrAssign(SString &str,char chars[]) ;
//字符串复制(将str2复制到str1上)
void StrCopy(SString &str1,SString str2);
//打印字符串
void PrintSString(SString str);
//字符串的拼接(将str2拼接到str1的后面)
bool StrCat(SString &str1,SString str2);
//求子串,用Sub返回串S中第pos个字符起长度为len的子串
bool SubString(SString &Sub,SString S,int pos,int len);
//比较操作
int StrCompare(SString S,SString T);
//定位操作(子串T在主串S中首次出现的位置)
int Index(SString S, SString T);
(3)测试案例及结果
#include<stdio.h>
#define MAXLEN 255
//通过牺牲0位,使得字符的存储与实际位置保持一致
//静态存储
typedef struct Str{
char ch[MAXLEN];
int length;
}SString;
//字符串的初始化
SString InitSString(SString &str){
str.length=0;
}
//判断字符串是否为空
int IsEmpty(SString str){
if(str.length==0){
return 1;
}
return 0;
}
//字符串赋值(把串str赋值为chars)
void StrAssign(SString &str,char chars[])
{
int i=0;
str.length=0;
while(chars[i])
{
str.ch[++str.length]=chars[i];
i++;
}
}
//字符串复制(将str2复制到str1上)
void StrCopy(SString &str1,SString str2)
{
int i;
for(i=1;i<=str2.length;i++)
{
str1.ch[i]=str2.ch[i];
}
str1.length=str2.length;
}
//打印字符串
void PrintSString(SString str)
{
int i;
for(i=1;i<=str.length;i++)
printf("%c",str.ch[i]);
printf("\n");
}
//字符串的拼接(将str2拼接到str1的后面)
bool StrCat(SString &str1,SString str2)
{
int i=1;
if((str1.length+str2.length)>MAXLEN)
return false;
while(str2.ch[i]){
str1.ch[++str1.length]=str2.ch[i];
i++;
}
return true;
}
//求子串,用Sub返回串S中第pos个字符起长度为len的子串
bool SubString(SString &Sub,SString S,int pos,int len){
//判断子串范围越界
if(pos+len-1>S.length)
return false;
for(int i=pos;i<pos+len;i++)
Sub.ch[i-pos+1]=S.ch[i];
Sub.length=len;
return true;
}
//比较操作
int StrCompare(SString S,SString T){
for(int i=1;i<=S.length && i<=T.length;i++){
if(S.ch[i]!=T.ch[i])
return S.ch[i]-T.ch[i];
}
return S.length-T.length;
}
//定位操作
int Index(SString S, SString T){
int i=1,n=S.length,m=T.length;
SString sub;
while(i<=n-m+1){
if(SubString(sub,S,i,m)){
if(StrCompare(sub,T)!=0)++i;
else return i;
}
}
return 0;
}
int main()
{
SString str1,str2,S;
InitSString(str1);
InitSString(str2);
InitSString(S);
char arr[50],arr1[50];
printf("请输入一个字符串:\n");
gets(arr);
printf("请再次输入一个字符串:\n");
gets(arr1);
//赋值操作
printf("--------赋值操作---------\n");
StrAssign(str1,arr);
PrintSString(str1);
printf("-----------------\n");
//复制操作
printf("--------复制操作---------\n");
StrCopy(str2,str1);
PrintSString(str2);
printf("-----------------\n");
//拼接操作
printf("--------字符串拼接操作---------\n");
if(StrCat(str1,str2))
PrintSString(str1);
printf("-----------------\n");
//求子串
printf("--------截取主串中的子串操作---------\n");
if(SubString(S,str1,2,3))
PrintSString(S);
printf("-----------------\n");
//字符串比较操作
printf("--------字符串比较操作---------\n");
if(StrCompare(str1,str2)>0)
printf("str1>str2");
else if(StrCompare(str1,str2)<0)
printf("str1<str2");
else
printf("str1==str2");printf("\n");
printf("-----------------\n");
//定位操作(子串在主串中出现的位置)
printf("--------查找子串在主串中的位置操作---------\n");
StrAssign(str2,arr1);
PrintSString(str2);
int index=Index(str1,str2);
printf("%s位置为:%d\n",str2,index);
return 0;
}
测试结果截图:
边栏推荐
- logback
- c语言语法基础之——函数 小程序 求阶乘
- 力扣------从数组中移除最大值和最小值
- TensorFlow动态分配显存
- The basis of C language grammar -- function nesting, Fibonacci sum of recursive applet and factorial
- LeetCode 剑指 Offer II 091.粉刷房子 - 原地修改
- Some problems to be considered when designing technical implementation scheme
- Automated testing -- Introduction and use of pytest itself and third-party modules
- MapReduce&Yarn理论
- Mysql database operation commands (constantly updated)
猜你喜欢
![Logical English structure [key points]](/img/4b/52a666ed01087adbc5fa4f9e1db393.png)
Logical English structure [key points]

Deep learning (tentsorflow2. version) three good student performance problems (1)

logback

Constraintlayout control uses full Raiders

Go learning notes (83) - code specification and common development skills

mysql学习总结

jar版本冲突问题解决

Summary of common commands of vim

2021-11-29 quintic polynomial of trajectory planning

How to create an IE tab in edge browser
随机推荐
Retrofit common request methods and comments, post, get heard file upload
thinkphp6.0的第三方扩展包,支持上传阿里云,七牛云
MySQL learning summary
Configuration internationale
自动化测试——pytest框架介绍及示例
druid数据源实现后台监控
Poj3682 king arthur's birthday celebration (probability)
Force buckle ----- remove the maximum and minimum values from the array
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.npm ER
自动化测试——关于unitest与pytest初始化共存问题
DAY 3 数组,前置后置,字符空间,关键词和地址指针
Detailed explanation of the network security competition questions (2) of the 2021 national vocational college skills competition (secondary vocational group)
[trajectory planning] testing of ruckig Library
Logview Pro can be used if the log is too large
c语言语法基础之——局部变量及存储类别、全局变量及存储类别、宏定义 学习
P1296 whispers of cows (quick row + binary search)
Summary of common commands of vim
Standard implementation of streaming layout: a guide to flexboxlayout
Speed test of adding, deleting, modifying and querying 5million pieces of data in a single MySQL table
Day 3 array, pre post, character space, keyword and address pointer