指针 Day 02

1.指针和数组的关系

        int a[5] = {1, 2, 3, 4, 5};
        int *p = NULL;
        p = &a[0];

        注:访问数组下标为n的元素的方式为:a[n] == *(a+n) == *(p+n) == p[n]

2. 数组作为函数参数

        常用:  int fun3(int *parray, int len)

3.字符型数组和字符串的传参

        练习:封装一个函数传入一个字符串,统计该字符串中大写、小写和空格

#include   

// 函数,接收字符串及指向计数器的指针  
void CountChars(char *str, int *uppercaseCount, int *lowercaseCount, int *spaceCount) {  
    *uppercaseCount = 0; // 初始化大写字母计数器为0  
    *lowercaseCount = 0; // 初始化小写字母计数器为0  
    *spaceCount = 0;     // 初始化空格计数器为0  

    // 遍历字符串,直到遇到字符串结束符  
    while (*str) {  
        // 判断是否为大写字母  
        if (*str >= 'A' && *str <= 'Z') {  
            (*uppercaseCount)++; // 大写字母计数器加1  
        }   
        // 判断是否为小写字母  
        else if (*str >= 'a' && *str <= 'z') {  
            (*lowercaseCount)++; // 小写字母计数器加1  
        }   
        // 判断是否为空格  
        else if (*str == ' ') {  
            (*spaceCount)++;     // 空格计数器加1  
        }  
        str++; // 移动指针到下一个字符  
    }  
}  

int main(void) {  
    char str[100]; // 定义字符串数组,最大长度为100  
    int uppercaseCount, lowercaseCount, spaceCount; // 声明计数器变量  

    printf("Enter a string: "); // 提示用户输入字符串  
    fgets(str, sizeof(str), stdin); // 从标准输入读取字符串  

    // 调用 CountChars 函数,传入字符串和计数器的地址  
    CountChars(str, &uppercaseCount, &lowercaseCount, &spaceCount);  

    // 输出大写字母的计数结果  
    printf("Uppercase letters: %d\n", uppercaseCount);  
    // 输出小写字母的计数结果  
    printf("Lowercase letters: %d\n", lowercaseCount);  
    // 输出空格的计数结果  
    printf("Spaces: %d\n", spaceCount);  

    return 0; // 程序正常结束  
}
4.指针函数

        指针函数是函数,函数的返回值是指针

5.函数指针

        函数指针是指针,指针指向的是函数

6.长度、拷贝、拼接、比较

        ①MyStrlen

#include 

int MyStrlen(char *pstr)
{
	int cnt = 0;

	while (*pstr != '\0')
	{
		cnt++;
		pstr++;
	}

	return cnt;
}

int main(void)
{
	char str[32] = {0};
	int len = 0;

	gets(str);

	len = MyStrlen(str);
	
	printf("len = %d\n", len);

	return 0;
}

        ②MyStrcpy

#include 

int MyStrcpy(char *pdst, char *psrc)
{
	while (*psrc != '\0')
	{
		*pdst = *psrc;
		psrc++;
		pdst++;
	}

	*pdst = '\0';

	return 0;
}

int main(void)
{
	char src[32] = {0};
	char dst[32] = {0};

	gets(src);

	MyStrcpy(dst, src);

	printf("dst = %s\n", dst);

	return 0;
}

        ③MyStrcat

#include 

int MyStrcat(char *pstr1, char *pstr2)
{
	while (*pstr1 != '\0')
	{
		pstr1++;
	}

	while (*pstr2 != '\0')
	{
		*pstr1 = *pstr2;
		pstr1++;
		pstr2++;
	}
	*pstr1 = '\0';

	return 0;
}

int main(void)
{
	char str1[32] = {0};
	char str2[32] = {0};
	int Ret = 0;

	gets(str1);
	gets(str2);

    MyStrcat(str1, str2);
	

    printf("str1 = %s\n", str1);
	

	return 0;
}

        ④MyStrcmp

#include 

int MyStrcmp(char *pstr1, char *pstr2)
{
	while (*pstr1 == *pstr2)
	{
		if ('\0' == *pstr1)
		{
			return 0;
		}
		pstr1++;
		pstr2++;
	}

	return *pstr1 - *pstr2;
}

int main(void)
{
	char str1[32] = {0};
	char str2[32] = {0};
	int ret = 0;

	gets(str1);
	gets(str2);

	ret = MyStrcmp(str1, str2);
	
	printf("ret = %d\n", ret);

	return 0;
}

你可能感兴趣的:(c语言,算法)