Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use string in C language

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use strings in C language, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

Definition of string

A finite sequence of zero or more characters.

Comparison of strings

The comparison of strings is actually the encoding of characters in the comparison string.

There is a certain k

< min(n,m),使得ai = bi (i = 1,2,3,4..k) 如果 ak < bk -->

So, srt1,

< srt2 (反之也成立) 除去相等的字符,在第一个不相等的字符位置以Ascii码进行比较 串的抽象数据类型 串的顺序存储结构示意图 串的顺序存储结构是用一组地址连续的存储单元来存储串中的字符序列 typedef struct sqString{ char* ch; //若串为空,则按串长分配存储区 //否则ch = NULL int length;//串长}sqString;串的初始化相关定义初始化 /** 状态码 **/#define TRUE 1#define FALSE 0#define EQ 0#define GT 1 //大于#define LT -1 //小于定长类初始化 #define MAX_SIZE 1024typedef struct{ char ch[MAX_SIZE + 1]; //定长方式实现了字符串的顺序结构--缺点是浪费空间 int length; }SString;串的堆式顺序存储结构(Heap) /** 串的堆式顺序存储结构(Heap)**/typedef struct{ char * ch; //如果是非空串,那么就按照指定长度分配内存,否则ch就指向NULL int length; //串当前长度}HString;初始化堆字符串赋值操作/** 为串str赋值,值为字符串常量chars **/void StrAssign_HeapString(HString * str,char * chars){ int len = strlen(chars); if(!len) return ERROR; InitString_HeapString(str); //动态为字符串分配空间 str->

Ch = (char*) malloc (len * sizeof (char)); if (! str- > ch) {exit (OVERFLOW); / / memory overflow, allocation failed} / / input strings one by one into for in the allocated space (int I = 0th I

< len ; i++) { str->

Ch [I] = chars [I];} str- > length = len; / / assign length return OK;} compare the size of two heap strings

Str1 = = str2 returns 0; str1

< str2 返回-1 ; str1 >

Str2 returns 1

Status Strcmp_HeapString (HString * str1,HString * str2) {for (int I = 0)

< str->

Length & & I

< str2->

Length; I + +) {/ / compare Ascii if directly when encountering different characters (str- > ch [I]! = str [2]-> ch [I]) {/ / greater returns an integer, less than a negative number return str- > ch [I]-str [2]-> ch [I] }} / / characters are all the same but vary in length, so compare the length return str1- > length-str2- > length;} above is all the content of this article "how to use strings in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report