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

简介: /* ============================================================================ Name : TestString.
/*
 ============================================================================
 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));//5
	printf("c00 strlen =%d\n",strlen(&c00));//5

	printf("sizeof(*&c0) =%d\n",sizeof(*&c0));//6
	printf("sizeof(*&c00) =%d\n",sizeof(*&c00));//5
	printf("==========\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");
}





相关文章
|
9天前
|
程序员 C语言
C语言库函数 — 内存函数(含模拟实现内存函数)
C语言库函数 — 内存函数(含模拟实现内存函数)
16 0
|
20天前
|
编译器 C语言 C++
【C语言】memset()函数(内存块初始化函数)
【C语言】memset()函数(内存块初始化函数)
23 0
|
20天前
|
编译器 C语言 C++
【C语言】memcpy()函数(内存块拷贝函数)
【C语言】memcpy()函数(内存块拷贝函数)
38 0
|
20天前
|
编译器 C语言 C++
【C语言】calloc()函数详解(动态内存开辟函数)
【C语言】calloc()函数详解(动态内存开辟函数)
24 0
|
20天前
|
存储 前端开发 编译器
【C语言】memmove()函数(拷贝重叠内存块函数详解)
【C语言】memmove()函数(拷贝重叠内存块函数详解)
32 1
|
23天前
|
安全 程序员 C++
【C++ 基本知识】现代C++内存管理:探究std::make_系列函数的力量
【C++ 基本知识】现代C++内存管理:探究std::make_系列函数的力量
97 0
|
1月前
|
存储
内存管理之内存释放函数
内存管理之内存释放函数
15 0
|
1月前
|
存储 程序员 C语言
内存管理——内存分配函数
内存管理——内存分配函数
21 0
|
1月前
|
C语言
【C语言进阶】字符函数和内存函数(二)
【C语言进阶】字符函数和内存函数(二)