字符串的基本操作和模式匹配
来源:互联网 发布:测定杨氏模量实验数据 编辑:程序博客网 时间:2024/06/05 17:52
#include <stdio.h>#include <stdlib.h>#define MAX_SIZE 100#define OK 1#define ERROR -1typedef int Status;
字符串的结构体
typedef struct{ char ch[MAX_SIZE]; int length;}String;
字符串长度初始化为0
Status StrInit(String *S){ S->length = 0; return OK;}
生成一个值等于char的串
Status StrAssign(String *S, char chars[]){ int i; for(i = 0; chars[i] != '\0'; i++) { S->ch[i] = chars[i]; S->length++; } return OK;}
输出字符串
void StrOutPut(String S){ int i; for(i = 0; i < S.length; i++) { printf("%c", S.ch[i]); } printf("\n");}
创建一个字符串
void StrCreate(String *S){ char ch[MAX_SIZE]; int i; S->length = 0; printf("Please enter a string:\n"); scanf("%s",ch); for(i = 0; ch[i] != '\0'; i++) { S->ch[i] = ch[i]; S->length++; }}
比较两个字符串的大小
若返回正数,则S>S1
若返回负数,则S
Status StrCompare(String *S, String *S1){ int i; for(i = 0; i < S->length && i < S1->length; i++) { if(S->ch[i] != S1->ch[i]) { return S->ch[i] - S1->ch[i]; } } return S->length - S1->length;//若长度短的字符串已经比较完(例如:abcdefg和abcd)则长度长的字符串大 或 两字符串的相等(例如:ggjghsg和ggjghsg)}
连接两个字符串
Status Concat(String *S1, String *S2, String *S){ int i; int j; for(i = 0; i < S1->length; i++) { S->ch[i] = S1->ch[i]; S->length++; } for(j = 0; j < S2->length; j++) { S->ch[i] = S2->ch[j]; S->length++; i++; } return OK;}
求字符串的长度
void StrLength(String *S){ printf("The length of the string is :%d\n", S->length); printf("\n");}
求字符串的子串
pos是子串起始位置
len是子串长度
Status SubString(String S, int pos, int len){ int i; if(pos < 0 || pos > S.length)//起始位置小于零或大于字符串的长度 { printf("Irregular position.\n"); } else if( pos + len > S.length)//字符串从pos开始到结束没有len位 { printf("It's too long.\n"); } else { for(i = pos - 1; i <= pos + len - 2; i++) { printf("%c", S.ch[i]); } printf("\n"); } return OK;}
字符串的模式匹配
Status Index(String S, int pos, String T){ int i; int j; int tag =0;//标记位(遇到重复的字符时) int posi = 0;//返回子串的起始位置 if(pos < 0 || pos >= S.length) { printf("Irregular position.\n"); } else { i = pos - 1; j = 0; while(i < S.length && j < T.length) { if(S.ch[i] == T.ch[j]) { if(j == 0) { tag = i; } i++; j++; } else { if(j != 0) { i = tag + 1; } else { i = i + 1; } posi = i; j = 0; } } if(j == T.length) { printf("success\n"); printf("position:%d\n", posi + 1); } else { printf("fail\n"); } } return OK;}
int main(){ int a = 0; String S; String S1; String S2; String S3; char chars[MAX_SIZE]; char ch[MAX_SIZE]; char ch1[MAX_SIZE]; StrInit(&S); printf("1.create a string equals char\n2.create a string\n3.compare S1 with S2\n4.connect two strings\n5.find a substring of length number\n6.string pattern matching\n"); printf("*****************\n"); printf(" S :new string\n"); printf("S1,S2:S1 compare with S2\n"); printf(" S3 :S1+S2\n"); printf("*****************\n"); while(1) { printf("Please enter the operation code:"); scanf("%d", &a); if(a == 1) { int res = 0; printf("Please enter a string:\n"); scanf("%s", chars); res = StrAssign(&S, chars); if(res == 1) { printf("Copy success!\n"); StrOutPut(S); StrLength(&S); } else { printf("Copy fail!\n"); } } else if(a == 2) { S.ch[0] = '\0'; S.length = 0; StrCreate(&S); StrOutPut(S); StrLength(&S); } else if(a == 3) { int res = 0; S1.ch[0] = '\0'; S1.length = 0; S2.ch[0] = '\0'; S2.length = 0; StrCreate(&S1); StrCreate(&S2); res = StrCompare(&S1, &S2); if(res > 0) { printf("The first string is larger than the the second string.\n"); } else if(res < 0) { printf("The first string is less than the second string.\n"); } else if(res == 0) { printf("The first string is equal to the second string.\n"); } } else if(a == 4) { StrInit(&S3); S1.ch[0] = '\0'; S1.length = 0; S2.ch[0] = '\0'; S2.length = 0; StrCreate(&S1); StrCreate(&S2); Concat(&S1, &S2, &S3); printf("*************\n"); StrOutPut(S3); StrLength(&S3); } else if(a == 5) { int g, h; StrCreate(&S); printf("Please enter position:"); scanf("%d", &g); printf("Please enter length:"); scanf("%d", &h); SubString(S, g, h); } else if(a == 6) { int b; String T1; String T2; StrInit(&T1); StrInit(&T2); printf("Please enter a string:\n"); scanf("%s", ch); StrAssign(&T1, ch); printf("Please enter a string:\n"); scanf("%s", ch1); StrAssign(&T2, ch1); printf("Please enter a position:\n"); scanf("%d", &b); Index(T1, b, T2); } else { printf("Operation coding error.\n"); printf("Please enter again.\n"); } }` return 0;}
运行结果:
阅读全文
0 0
- 字符串的基本操作和模式匹配
- Shell基于模式匹配的字符串操作
- 字符串的模式匹配
- 字符串的模式匹配
- 字符串的模式匹配
- 字符串的模式匹配
- 字符串的模式匹配
- 字符串的模式匹配
- 字符串的模式匹配
- Lua 基本的字符串匹配
- 正则表达式-字符串基本的匹配,拆分,替换和截取
- bash 字符串 模式匹配操作符简介
- 字符串的简单模式匹配
- 简单的字符串模式匹配
- 回溯的字符串模式匹配
- 字符串的多模式匹配
- 字符串的模式匹配KMP
- 字符串的模式匹配问题
- SpringMVC 通过Map、Model和ModelMap向页面传值
- ubuntu caffe 安装
- C语言枚举进程,实现一个简单的内存补丁
- codevs 1098 && 洛谷 P1031 均分纸牌 (修bug plus(两个注意*))
- LeetCode----- 24.Swap Nodes in Pairs
- 字符串的基本操作和模式匹配
- 解决AS上android monitors 显示no debuggable processes
- 在struts2框架中,获得Session遇到的问题
- CentOS之——CentOS 6.5 安装RTL8111/8168B PCI EXPRESS网卡并解决安装出现的问题
- 掩码
- 静态变量和实例变量的区别(配图解释专业术语,通俗易懂)
- Java中Xml文件的解析
- 简单的例子了解自定义ViewGroup
- 数据库 -- SQL