【C语言】字符指针数组查找字符串

字符串常识:
1.以’\0’结尾,用" "括起来,字符是用’ ‘括起来
2.字符串有字符数组和字符指针两种表现形式。字符数组不一定是字符串,有’\0’结束的字符数组才是字符串,但字符串一定是字符数组
3.字符串常量是个地址常量,存放在文字常量区
char s1=“abc”; char s2=“abc”; /* 这里s1和s2都是指向内存的同一个地址单元,即s1==s2,这个地址单元存放了"abc"的起始地址 */
“abc”[0] == s1[0] == s2[0] == ‘a’
“abc”[1] == s1[1] == s2[1] == ‘b’
4.字符数组要看作用域和存储类型才能确定存放在内存的什么区域
5.处理字符串常用string.h里面的字符串函数


字符指针数组查找字符串:

1.遍历数组,比较字符串大小判断是否相等

int str_search1(const char*dststr, const char**srcstr, int num) //适用于全部字符串(字符数组和字符串常量)
{
    
    
    int i;
    for (i = 0; i < num; i++)
    {
    
    
        if (strcmp(*srcstr++, dststr) == 0)       //从头遍历字符串数组,找到相等的字符串返回                    
        {
    
    
            return i;
        }
		//srcstr++;
    }
    return -1;
}

2.上述使用了strcmp函数比较字符串大小,增加了时间和空间上的消耗,若对象是字符串常量,可以直接比较地址值

int str_search2(const char*dststr, const char**srcstr, int num)//只适合字符串常量
{
    
    
    int i;
    for (i = 0; i < num; i++)
    {
    
    
        if(*srcstr++==dststr)                   //从头遍历字符串数组,比较字符指针的值,利用字符串常量只有唯一的内存地址
        {
    
    
            return i;
        }
		//srcstr++;
    }
    return -1;
}

3.上述的方法简单可观,但是当数组较大时,不能快速判断字符串在不在数组里面,每次寻找字符串都要遍历一次数组,不适应多次查找字符串的场景。此时可牺牲一下空间资源换取时间资源,使用静态数组存储字符串的存在状态。



测试程序:

#include <stdio.h>
#include <string.h>

const char* srcstr[] = {
    
     "Hello","我在那里!","我在哪里?","World","C语言","C++" };
const char* dststr = "我在哪里?";   //字符串常量

int str_search1(const char*dststr, const char**srcstr, int num) //适用于全部字符串(字符数组和字符串常量)
{
    
    
    int i;
    for (i = 0; i < num; i++)
    {
    
    
        if (strcmp(*srcstr++, dststr) == 0)       //从头遍历字符串数组,找到相等的字符串返回                    
        {
    
    
            return i;
        }
		//srcstr++;
    }
    return -1;
}

int str_search2(const char*dststr, const char**srcstr, int num)//只适合字符串常量
{
    
    
    int i;
    for (i = 0; i < num; i++)
    {
    
    
        if(*srcstr++==dststr)                   //从头遍历字符串数组,比较字符指针的值,利用字符串常量只有唯一的内存地址
        {
    
    
            return i;
        }
		//srcstr++;
    }
    return -1;
}

int str_search3(const char*dststr, const char**srcstr, int num)//只适合字符串常量
{
    
    
    int i;
	static int a[4096]={
    
    0};           //数组大小随字符指针数组大小而变
	int start=(int)*srcstr;          //将地址值转换为int型进行运算,防止溢出可以使用unsigned long long并进行取余操作
	int t=0;
    for (i = 0; i < num; i++)
    {
    
    
		t=((start>(int)*srcstr)?(start-(int)*srcstr):((int)*srcstr-start));//将数组里面存在的字符串常量映射到数组上,并且保证数组下标t不小于0
		a[t]=1;
		srcstr++;			
    }
	t=((start>(int)dststr)?(start-(int)dststr):((int)dststr-start));     
	if(a[t]!=0)
	{
    
    
		return 0;          //查找成功,但未定位字符串位置
	}
	else
	{
    
    
		return -1;
	}

}

int main()
{
    
    
    int ret;
    ret = str_search1(dststr, srcstr, sizeof(srcstr) / sizeof(char*));
    if (ret != -1)
    {
    
    
        printf("查找成功\n");
		printf("\"%s\"在%d号位置\n",dststr,ret);
    }
    else
    {
    
    
		printf("查找失败,\"%s\"不在数组里\n",dststr);

    }

    getchar();
	return 0;
}

结果:
在这里插入图片描述
在这里插入图片描述






最后,当然方法还有很多,欢迎指点一下啦!大家共勉!

猜你喜欢

转载自blog.csdn.net/yechongbinbin/article/details/114552778