C字符串处理函数汇总实现(面试大多会用到)

来源:互联网 发布:做数据库发展好吗 编辑:程序博客网 时间:2024/06/05 15:11

 

如果你的简历上,写了你精通C/C++,如果让你写个字符串函数,结果写不出来,是不是很囧。

字符串是C中很重要的一部分,我们在编程的操作上,很多都是处理字符串的。

对C中的字符串你应该这样认识,它是一个结尾带'/0'的数组。不关写成

char *s = "nihao";

char s[] ="nihao";

char s[10] = "nihao";

他们在内存中都是可以认为是数组形式的存在,连续的存放,最后以'/0'结尾。

数组在C中是二等公民,所以它不是像内置类型int,double,...等这样可以直接写=,>,==...这样进行操作的。

他需要用到string.h中的字符串处理函数。

首先结束3个最常用的。

1,字符串拷贝函数strcpy(); //string copy;

2,字符串比较函数strcmp(); //string compare;

3,字符串长度函数strlen(); //string length;

下面是上面3个函数的实现

#include <stdio.h>

//字符串拷贝函数,把dest指向的字符串拷贝到source中
char *mystrcpy(char *source,const char *dest)
{
char *s = source;
while (*dest!='/0')
   *s++ = *dest++;
*s = '/0';
return source;
}
//字符串比较函数,相等返回0,s1大返回正整数,s2大返回负整数
int mystrcmp(const char *s1,const char *s2)
{
    while(*s1!='/0'&&*s2!='/0'&&(*s1==*s2))s1++,s2++;
    return (*s1)-(*s2);
}
//字符串长度函数,长度不包括结尾的'/0'
int mystrlen(const char *s)
{
int res = 0;
while(*s++!='/0')
   res++;
return res;
}

void main()
{
char data[100];
char *s = "ni";
char *s1 = "nihao";
mystrcpy(data,s);
printf("%s,%d/n",data,mystrlen(s));
printf("%d/n",mystrcmp(s,s1));
}

注:这3个函数常用,实现的代码其实也简单,可以看到,每个函数的体的代码不超过5行。最少的2行。

其次是会用到的,没有前3个那么高。

1,字符串连接函数strcat();//string catenate

2,字符串转换为小写函数strlwr();//string lowercase

3,字符串转换为大写函数strupr();//string uppercase

字符串连接函数,类似C++中的+号操作。大小写字母的转换,本质上就是ASCII码值加减32.

下面是这3个函数的实现。

#include <stdio.h>
//字符串连接函数
char *mystrcat(char *source,const char *dest)
{
char *s = source;
while(*s!='/0')s++;
while(*dest!='/0')*s++ = *dest++;
*s='/0';
return source;
}
//字符串转换小写字母函数
char *mystrlwr(char *source)
{
   char *s = source;
   do
   {
   if(*s>='A'&&*s<='Z')
      *s+=32;
   } while (*s++!='/0');
   return source;
}
//字符串转换大写字母函数
char *mystrupr(char *source)
{
char *s = source;
do
{
   if(*s>='a'&&*s<='z')
    *s-=32;
} while (*s++!='/0');
    return source;
}
void main()
{
char data[100]="ni";
char *s = " hao!";
printf("%s/n",mystrcat(data,s));
printf("%s/n",mystrupr(data));
printf("%s/n",mystrlwr(data));
}

注:看这3个函数的代码实现也不难,函数体内的代码也不长。大小写转换函数很相似。

然后说几个不常用,但往往确会容易面试遇到的字符串处理函数。

1,字符串倒转函数。strrev();//string reverse

2,字符串查找字符串第一次出现函数。strstr();

这2个函数实现如下:

#include <stdio.h>
//字符串倒转函数
char *mystrrev(char *source)
{
char *s1 = source,*s2 = source,temp;
while(*s2!='/0')s2++;
if(s1!=s2)s2--;
while(s1<s2)temp = *s1,*s1 = *s2,*s2 = temp,s1++,s2--;
return source;
}
//字符串查找指定字符串的第一次出现,没有出现返回NULL,出现则返回出现的位置
char *mystrstr(const char *source,const char *dest)
{
   char *s = source,*d = dest;
   while(*source!='/0')
   {
    while(*s!='/0'&&*d!='/0'&&*s==*d)s++,d++;
    if(*d=='/0')
    {
     s = source;break;
    }
    if(*s=='/0')
    {
     s = NULL;break;
    }
       source++;
    s=source,d=dest;
   }
   if (*source=='/0')s=NULL;
   return s;
}
void main()
{
char data[100]="huifeng00 : ni hao";
char *s = " in";
printf("%s/n",mystrrev(data));
printf("%s/n",mystrstr(data,s));
}

最后说一个调用比较另类的字符串处理函数

1,查找由第2个字符串指定分界符对第一个串分解后的单词函数

strtok();//string token;

函数原型:char *strtok(char *s, const char *delim);它的调用比较特别。

首次调用s指向要分解的串,以后在调用s参数则设为NULL。

例如:char data[] = "a,b,c,d";

分界符","就是逗点,字符串形式表示。

第一次调用strtok(data,",");然后在调用就是strtok(NULL,",");

这个函数的处理思想:用一个静态变量记录每次调用时候分解字符串的处理指针位置,且每次调用的时候,

把字符串中出现分界符的字符用'/0'替换,直到把字符串遍历完为止。

这个函数的实现

#include <stdio.h>
#include <string.h>
char * mystrtok(char * source, char *delim)
{
static char *begin;
if (source!=NULL)
{
   begin = strstr(source,delim);
   while(begin==source)
   {
    source += strlen(delim);
    begin = strstr(source,delim);
   }
}
else
{
        source = begin;
   if (source==NULL)
   {
    return NULL;
   }
   begin = strstr(source,delim);
   while(begin==source)
   {
    source += strlen(delim);
    begin = strstr(source,delim);
   }
}
if (begin!=NULL)
{
   memset(begin,0,strlen(delim));
   begin += strlen(delim);
}
else
   if (*source==0)
   {
    return NULL;
   }
   return source;
}

void main()
{
char data[100]=" huifeng00 ni hao";
char *p = mystrtok(data," ");
printf("%s/n",p);
while (p!=NULL)
{
   p = mystrtok(NULL," ");
   if(p!=NULL)
        printf("%s/n",p);
}
}
以前专门写过这个函数的实现

http://hi.baidu.com/huifeng00/blog/item/f8dfa80b420a408ed0581b0f.html

能看到这里,算你有耐心。

算是一个总结吧。用了一定的时间写代码和文章。

希望对能看到这篇文章的C爱好者有帮助。

coder:huifeng00

原创粉丝点击