我是靠谱客的博主 淡淡大树,最近开发中收集的这篇文章主要介绍c 语言 string类型,C语言模拟String对象类型,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

length)

{

if(str->ch[i]>='a' && str->ch[i]<='z')

str->ch[i]-=32;/* Converts to capital */

i++;

}

}

void ToLowerCase(String* str)

{

int i=0;

while(ilength)

{

if(str->ch[i]>='A' && str->ch[i]<='Z')

str->ch[i]+=32;/* Converts to lower case */

i++;

}

}

int StartWith(const String* str,const char* chars)

{

int i=0;

while(*chars!='')

{

if(str->ch[i++] != *chars++)

return FALSE;

}

return TRUE;

}

int EndWith(const String* str,const char* chars)

{

int i=str->length-GetCharsLength(chars);

while(*chars!='')/* chars not end */

{

if(str->ch[i++] != *chars++)/* Not equal */

return FALSE;

}

return TRUE;

}

int GetCharsLength(const char* chars)

{

int len=0;

while(*chars!='')/* Not '' */

{

++len;

++chars;

}

return len;

}

int main()

{

String str,sub_str,concat_str;

CreateString(&str,"Yannis");

printf("String: ");

PrintlnString(&str);

printf("String Length: %dn",StringLength(&str));

printf("String Empty: %d (true:1,false:0)n",StringEmpty(&str));

printf("Substring from 1 to 4: ");

if(SubString(&str,1,4,&sub_str))

PrintlnString(&sub_str);

printf(""Yannis" contains "anni": %dn",Contains(&str,&sub_str));

printf("Index of sub-string "anni": %d (brute force method)n",Index(&str,&sub_str,0));

printf("Concat "Yannis" and "anni": ");

Concat(&concat_str,&str,&sub_str);

PrintlnString(&concat_str);

printf("Clear concated string: ");

Clear(&concat_str);

PrintlnString(&concat_str);

printf("Compare: "Yannis" with "anni"(EQ=0,LT<0,MT>0): %dn",Compare(&str,&sub_str));

printf("Fetch char: %cn",CharAt(&str,1));

printf("First index of 'n': %dn",FirstIndexOf(&str,'n'));

printf("Last index of 'n': %dn",LastIndexOf(&str,'n'));

printf("SetCharAt 5: (old:%c,new:%c): ",SetCharAt(&str,5,'o'),'o');

PrintlnString(&str);

printf("ReplaceFirst: (old:%c,new:%c): ",ReplaceFirst(&str,'w'),'w');

PrintlnString(&str);

printf("ReplaceLast: (old:%c,new:%c): ",ReplaceLast(&str,'s'),'s');

PrintlnString(&str);

printf("ReplaceAll: (old:%c,new:%c),%d characters placed: ",'n','t',ReplaceAll(&str,'n','t'));

PrintlnString(&str);

printf("ToUpperCase: ");

ToUpperCase(&str);

PrintlnString(&str);

printf("ToLowerCase: ");

ToLowerCase(&str);

PrintlnString(&str);

printf("Start with "wat": %dn",StartWith(&str,"wat"));

printf("End with "is": %dn",EndWith(&str,"is"));

return 0;

}

最后

以上就是淡淡大树为你收集整理的c 语言 string类型,C语言模拟String对象类型的全部内容,希望文章能够帮你解决c 语言 string类型,C语言模拟String对象类型所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部