字符串基本操作以及内存函数

/* ============================================================================ Name: TestString.c Author: lf Version: Copyright : Your copyright notice Description : C语言字符串相关操作以及内存函数 1 在Java中有String数据类型,但是在C语言中没有 2 在C语言中一般用字符数组来表示字符串,因为在C中没有String这个数据类型表示字符串的两种方式:第一种: char c0[]="hello";第二种: char c00[]={'h','e','l','l','o'};注意的问题: 1 在第一种方式中系统会自动在其末尾添加'\0'即变成了"hello\0"所以sizeof(&c0)大小为6.但是strlen(&c0)=5而不是6.这是因为strlen()只获取'\0'之前的长度. 2 字符常量不可以写.char *c="hello";*c='A';//报错:Segmentation fault因为"hello"是字符串常量存储在文字常量区;若去修改一个常量的值当然是不行的 ============================================================================ */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <memory.h>void test0();void test1();void test3();void test4();void test5();void myStrrev(char *p);void myStrupr(char *p);void myStrlwr(char *p);char charArray1[20]="hello hello";char charArray2[20]="world world";int main(void) {test0();test1();test2();test3();test4();test5();return EXIT_SUCCESS;}/** * 字符数组的初始化 */void test0(){char c[5]={'h','e','l','l','o','\0'};printf("c=%x\n",c);printf("c=%s\n",c);char d[10]="hello";printf("d=%s\n",d);printf("==========\n");}/** * 字符串常量不可以修改 * Segmentation fault */void test1(){char *c="hello";printf("c=%s\n",c);printf("==========\n");//报错 Segmentation fault//*c='A';//错误的做法.数组越界//a[10]="hello";代表的是从数组下标为10的位置开始存储char a[10];a[10]="hello";}/** * 利用字符串初始化字符数组 */void test2(){//char c[15]={"morning"};//一般简写为:char c[15]="morning";printf("c=%s\n",c);printf("==========\n");}/** * 字符串的遍历 * putchar()输出一个字符 */void test3(){int len=0;char *str="hello world";printf("size of hello world=%d\n",sizeof("hello world"));//存储字符串首地址char *p=str;//*str的内容不为0(即最后的终止符)时输出字符while(*str){putchar(*str);str++;len++;}printf("\n");printf("str=%s,len=%d\n",p,len);printf("==========\n");}/** * 字符串常用操作 * 0 strlen()求字符串长度 * 1 strstr()查找字符串 * 2 strcmp()按照ASCII对比字符串的大小(文件夹按照字母排序的大小一样) * 3 strcha()查找字符在字符串中首次出现的位置 * 4 strcat()连接字符串 * 5 atoi()字符串转整数 * 6 myStrrev()字符串逆转 * 7 myStrupr()字符串转大写 * 8 myStrlwr()字符串转小写 */void test4(){//strlen()求字符串长度char c0[]="hello";char c00[]={'h','e','l','l','o'};printf("c0 strlen =%d\n",strlen(&c0));//5printf("c00 strlen =%d\n",strlen(&c00));//5printf("sizeof(*&c0) =%d\n",sizeof(*&c0));//6printf("sizeof(*&c00) =%d\n",sizeof(*&c00));//5printf("==========\n");//strstr()查找字符串char c1[20]="hello world";char c2[5]="or";char *p=strstr(c1,c2);if (p==NULL) {printf("NULL\n");} else {printf("p=%x,*p=%c\n",p,*p);}//strcmp()对比字符串的大小char c3[6]="hello";char c4[6]="hello";int result=strcmp(c3,c4);if (result==0) {printf("char c3 = char c4 \n");} else if(result<0){printf("char c3 < char c4 \n");}else{printf("char c3 > char c4 \n");}//strchr查找字符在字符串中首次出现的位置char c5[6]="hello";char c='e';char *f=strchr(c5,c);if(f==NULL){printf("not found\n");}else{printf("found ! location=%x\n",f);}//strcat()连接字符串char c6[6]="hello";char c7[6]="hello";char *n=strcat(c6,c7);printf("strcat result=%s\n",n);//atoi()字符串转整数char c8[6]="123456";int i=atoi(c8);printf("i=%d\n",i);//myStrrev()字符串逆转char str[30]="123456789";//myStrrev(str);//myStrrev(&str);//char *pointer=&str;//myStrrev(pointer);printf("str=%s\n",str);char charString[30]="abcd";myStrupr(&charString);printf("charString=%s\n",charString);myStrlwr(&charString);printf("charString=%s\n",charString);printf("==========\n");}/** * 实现字符串的逆转 */void myStrrev(char *p){//获取字符串长度int len=strlen(p);int i;for(i=0;i<len/2;i++){char c=p[i];p[i]=p[len-1-i];p[len-1-i]=c;}}/** * 实现字符串小写转大写 */void myStrupr(char *p) {while (*p != '\0') {if (*p >= 'a' && *p <= 'z') {*p = *p – 32;}p++;}}/** * 实现字符串大写转小写 */void myStrlwr(char *p) {while (*p != '\0') {if (*p >= 'A' && *p <= 'Z') {*p = *p + 32;}p++;}}/** * 常用内存函数 * 1 memset()更改字符中的字符 * 2 memcpy()从源字符串中拷贝n个字节到目标字符串中 * 还有一个memccpy()与此类似但更灵活和强大 * 3 memchr()在字符串的前n个字节中搜索字符 * 4 memicmp()比较两个字符串前n个字节,且忽略大小写 * (非标准C函数,在此未实现) * */void test5(){//memset()char c0[10]="hello";memset(c0,'A',3);printf("c0=%s\n",c0);//memcpy()char c1[10]="123456";memcpy(c1,c0,3);printf("c1=%s\n",c1);//memchr()char c3[10]="hellohi";char *p=memchr(c3,'e',10);if (p==NULL) {printf("NOT FOUND\n");} else {printf("result=%c\n",*p);}printf("==========\n");}

,顺境的美德是节制,逆境的美德是坚韧,这后一种是较为伟大的德性。

字符串基本操作以及内存函数

相关文章:

你感兴趣的文章:

标签云: