C语言常见字符串面试题

来源:互联网 发布:单片机设计师 编辑:程序博客网 时间:2024/05/17 11:57

 一些常用字符串操作函数的内部实现 

memset:
  1. /*
  2.  * memset - Fill a region of memory with the given value
  3.  * @s: Pointer to the start of the area.
  4.  * @c: The byte to fill the area with
  5.  * @count: The size of the area.
  6.  */
  7. void *memset(void *s, int c, size_t count)
  8. {
  9.     char *xs = s;

  10.     while (count--)
  11.         *xs++ = c;
  12.     return s;
  13. }

memcpy:
  1. /*
  2. * memcpy - Copy one area of memory to another
  3. * @dest: Where to copy to
  4. * @src: Where to copy from
  5. * @count: The size of the area.
  6. */
  7. void *memcpy(void *dest, const void *src, size_t count)
  8. {
  9. char *tmp = dest;
  10. const char *s = src;
  11. while (count--)
  12. *tmp++ = *s++;
  13. return dest;
  14. }

memmove:
  1. /*
  2.  * memmove - Copy one area of memory to another
  3.  * @dest: Where to copy to
  4.  * @src: Where to copy from
  5.  * @count: The size of the area.
  6.  * Unlike memcpy(), memmove() copes with overlapping areas.
  7.  */
  8. void *memmove(void *dest, const void *src, size_t count)
  9. {
  10.     char *tmp;
  11.     const char *s;

  12.     if (dest <= src) {
  13.         tmp = dest;
  14.         s = src;
  15.         while (count--)
  16.             *tmp++ = *s++;
  17.     } else {
  18.         tmp = dest;
  19.         tmp += count;
  20.         s = src;
  21.         s += count;
  22.         while (count--)
  23.             *--tmp = *--s;
  24.     }
  25.     return dest;
  26. }

memcmp:
  1. /*
  2.  * memcmp - Compare two areas of memory
  3.  * @cs: One area of memory
  4.  * @ct: Another area of memory
  5.  * @count: The size of the area.
  6.  */
  7. int memcmp(const void *cs, const void *ct, size_t count)
  8. {
  9.     const unsigned char *su1, *su2;
  10.     int res = 0;

  11.     for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  12.         if ((res = *su1 - *su2) != 0)
  13.             break;
  14.     return res;
  15. }

strcpy:
  1. /*
  2.  * strcpy - Copy a %NUL terminated string
  3.  * @dest: Where to copy the string to
  4.  * @src: Where to copy the string from
  5.  */
  6. char *strcpy(char *dest, const char *src)
  7. {
  8.     char *tmp = dest;

  9.     while ((*dest++ = *src++) != '\0');

  10.     return tmp;
  11. }

strncpy:
  1. /*
  2.  * strncpy - Copy a length-limited, %NUL-terminated string
  3.  * @dest: Where to copy the string to
  4.  * @src: Where to copy the string from
  5.  * @count: The maximum number of bytes to copy
  6.  *
  7.  * The result is not %NUL-terminated if the source exceeds
  8.  * @count bytes.
  9.  *
  10.  * In the case where the length of @src is less than that of
  11.  * count, the remainder of @dest will be padded with %NUL.
  12.  */
  13. char *strncpy(char *dest, const char *src, size_t count)
  14. {
  15.     char *tmp = dest;

  16.     while (count) {
  17.         if ((*tmp = *src) != 0)
  18.             src++;
  19.         tmp++;
  20.         count--;
  21.     }

  22.     return dest;
  23. }

strcat:
  1. /*
  2.  * strcat - Append one %NUL-terminated string to another
  3.  * @dest: The string to be appended to
  4.  * @src: The string to append to it
  5.  */
  6. char *strcat(char *dest, const char *src)
  7. {
  8.     char *tmp = dest;

  9.     while (*dest)
  10.         dest++;
  11.     while ((*dest++ = *src++) != '\0');

  12.     return tmp;
  13. }

strncat:
  1. /*
  2.  * strncat - Append a length-limited, %NUL-terminated string to another
  3.  * @dest: The string to be appended to
  4.  * @src: The string to append to it
  5.  * @count: The maximum numbers of bytes to copy
  6.  *
  7.  * Note that in contrast to strncpy(), strncat() ensures the result is
  8.  * terminated.
  9.  */
  10. char *strncat(char *dest, const char *src, size_t count)
  11. {
  12.     char *tmp = dest;

  13.     if (count) {
  14.         while (*dest)
  15.             dest++;
  16.         while ((*dest++ = *src++) != 0) {
  17.             if (--count == 0) {
  18.                 *dest = '\0';
  19.                 break;
  20.             }
  21.         }
  22.     }

  23.     return tmp;
  24. }

strcmp:
  1. /*
  2.  * strcmp - Compare two strings
  3.  * @cs: One string
  4.  * @ct: Another string
  5.  */
  6. int strcmp(const char *cs, const char *ct)
  7. {
  8.     unsigned char c1, c2;

  9.     while (1) {
  10.         c1 = *cs++;
  11.         c2 = *ct++;
  12.         if (c1 != c2)
  13.             return c1 < c2 ? -: 1;
  14.         if (!c1)
  15.             break;
  16.     }

  17.     return 0;
  18. }

strncmp:
  1. /*
  2.  * strncmp - Compare two length-limited strings
  3.  * @cs: One string
  4.  * @ct: Another string
  5.  * @count: The maximum number of bytes to compare
  6.  */
  7. int strncmp(const char *cs, const char *ct, size_t count)
  8. {
  9.     unsigned char c1, c2;

  10.     while (count) {
  11.         c1 = *cs++;
  12.         c2 = *ct++;
  13.         if (c1 != c2)
  14.             return c1 < c2 ? -: 1;
  15.         if (!c1)
  16.             break;
  17.         count--;
  18.     }

  19.     return 0;
  20. }

strchr:
  1. /*
  2.  * strchr - Find the first occurrence of a character in a string
  3.  * @s: The string to be searched
  4.  * @c: The character to search for
  5.  */
  6. char *strchr(const char *s, int c)
  7. {
  8.     for (; *!= (char)c; ++s)
  9.         if (*== '\0')
  10.             return NULL;

  11.     return (char *)s;
  12. }

strlen:
  1. /*
  2.  * strlen - Find the length of a string
  3.  * @s: The string to be sized
  4.  */
  5. size_t strlen(const char *s)
  6. {
  7.     const char *sc;

  8.     for (sc = s; *sc != '\0'; ++sc);

  9.     return sc - s;
  10. }

strnlen:
  1. /*
  2.  * strnlen - Find the length of a length-limited string
  3.  * @s: The string to be sized
  4.  * @count: The maximum number of bytes to search
  5.  */
  6. size_t strnlen(const char *s, size_t count)
  7. {
  8.     const char *sc;

  9.     for (sc = s; count-- && *sc != '\0'; ++sc);

  10.     return sc - s;
  11. }

strsep:
  1. /*
  2.  * strsep - Split a string into tokens
  3.  * @s: The string to be searched
  4.  * @ct: The characters to search for
  5.  *
  6.  * strsep() updates @s to point after the token, ready for the next call.
  7.  */
  8. char *strsep(char **s, const char *ct)
  9. {
  10.     char *sbegin = *s;
  11.     char *end;

  12.     if (sbegin == NULL)
  13.         return NULL;

  14.     end = strpbrk(sbegin, ct);
  15.     if (end)
  16.         *end++ = '\0';
  17.     *= end;

  18.     return sbegin;
  19. }

strstr:
  1. /*
  2.  * strstr - Find the first substring in a %NUL terminated string
  3.  * @s1: The string to be searched
  4.  * @s2: The string to search for
  5.  */
  6. char *strstr(const char *s1, const char *s2)
  7. {
  8.     int l1, l2;

  9.     l2 = strlen(s2);
  10.     if (!l2)
  11.         return (char *)s1;
  12.     l1 = strlen(s1);
  13.     while (l1 >= l2) {
  14.         l1--;
  15.         if (!memcmp(s1, s2, l2))
  16.             return (char *)s1;
  17.         s1++;
  18.     }

  19.     return NULL;
  20. }
常见字符串面试题:
1)写出在母串中查找子串出现次数的代码.
int count1(char* str,char* s){    char* s1;    char* s2;    int count = 0;    while(*str!='/0')    {        s1 = str;        s2 = s;        while(*s2 == *s1&&(*s2!='/0')&&(*s1!='0'))        {            s2++;            s1++;        }        if(*s2 == '/0')            count++;        str++;    }    return count;}
2)查找第一个匹配子串位置,如果返回的是s1长度len1表示没有找到
size_t find(char* s1,char* s2)    {        size_t i=0;         size_t len1 = strlen(s1)        size_t len2 = strlen(s2);        if(len1-len2<0) return len1;        for(;i<len1-len2;i++)        {            size_t m = i;            for(size_t j=0;j<len2;j++)            {                if(s1[m]!=s2[j])                    break;                m++;            }            if(j==len)                break;        }        return i<len1-len2?i:len1;    }

3)实现strcpy函数
char *strcpy(char *destination, const char *source) {     assert(destination!=NULL&&source!=NULL);    char* target = destinaton;    while(*destinaton++=*source++);     return target ; } 
出现次数相当频繁
4)实现strcmp函数
int strcmp11(char* l,char* r){    assert(l!=0&&r!=0);    while(*l == *r &&*l != '/0') l++,r++;    if(*l > *r)        return 1;    else if(*l == *r)        return 0;    return -1;}
5) 实现字符串翻转
void reserve(char* str){    assert(str != NULL);    char * p1 = str;    char * p2 = str-1;    while(*++p2);         //一般要求不能使用strlen    p2 -= 1;    while(p1<p2)    {        char c = *p1;        *p1++ = *p2;        *p2-- = c;   }}
6)、用指针的方法,将字符串“ABCD1234efgh”前后对调显示
//不要用strlen求字符串长度,这样就没分了
代码如下:
    
char str123[] = "ABCD1234efgh";    char * p1 = str123;    char * p2 = str123-1;    while(*++p2);    p2 -= 1;    while(p1<p2)    {        char c = *p1;        *p1++ = *p2;        *p2-- = c;    }

7) 给定字符串A和B,输出A和B中的最大公共子串。比如A="aocdfe" B="pmcdfa" 则输出"cdf"

#i nclude<stdio.h>#i nclude<stdlib.h>#i nclude<string.h>char *commanstring(char shortstring[], char longstring[]){    int i, j;    char *substring=malloc(256);    if(strstr(longstring, shortstring)!=NULL)              //如果……,那么返回shortstring        return shortstring;     for(i=strlen(shortstring)-1;i>0; i--)                 //否则,开始循环计算    {        for(j=0; j<=strlen(shortstring)-i; j++)        {            memcpy(substring, &shortstring[j], i);            substring[i]='/0';            if(strstr(longstring, substring)!=NULL)            return substring;        }    }    return NULL;}main(){    char *str1=malloc(256);    char *str2=malloc(256);    char *comman=NULL;    gets(str1);    gets(str2);    if(strlen(str1)>strlen(str2))                         //将短的字符串放前面        comman=commanstring(str2, str1);    else        comman=commanstring(str1, str2);    printf("the longest comman string is: %s/n", comman);}

8) 判断一个字符串是不是回文

int IsReverseStr(char *str){    int i,j;    int found=1;    if(str==NULL)        return -1;    char* p = str-1;    while(*++p!= '/0');    --p;    while(*str==*p&&str<p) str++,p--;    if(str < p)        found = 0;    return found;}

9)写函数完成内存的拷贝
void* memcpy( void *dst, const void *src, unsigned int len ){    register char *d;    register char *s;    if (len == 0)        return dst;    if ( dst > src )   //考虑覆盖情况    {        d = (char *)dst + len - 1;        s = (char *)src + len - 1;        while ( len >= 4 )   //循环展开,提高执行效率        {            *d-- = *s--;            *d-- = *s--;            *d-- = *s--;            *d-- = *s--;            len -= 4;        }        while ( len-- )         {            *d-- = *s--;        }    }     else if ( dst < src )     {        d = (char *)dst;        s = (char *)src;        while ( len >= 4 )        {            *d++ = *s++;            *d++ = *s++;            *d++ = *s++;            *d++ = *s++;            len -= 4;        }        while ( len-- )        {            *d++ = *s++;        }    }    return dst;}
出现次数相当频繁

10)写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)
功能:
在字符串中找出连续最长的数字串,并把这个串的长度返回,并把这个最长数字串付给其中一个函数参数outputstr所指内存。例如:"abcd12345ed125ss123456789"的首地址传给intputstr后,函数将返回9,outputstr所指的值为123456789

int continumax(char *outputstr, char *inputstr){    char *in = inputstr, *out = outputstr, *temp, *final;    int count = 0, maxlen = 0;    while( *in != '/0' )    {        if( *in > 47 && *in < 58 )        {            for(temp = in; *in > 47 && *in < 58 ; in++ )            count++;        }    else    in++;    if( maxlen < count )    {        maxlen = count;        count = 0;        final = temp;    }    }    for(int i = 0; i < maxlen; i++)    {        *out = *final;        out++;        final++;    }    *out = '/0';    return maxlen;}

11) 编写一个 C 函数,该函数在一个字符串中找到可能的最长的子字符串,且该字符串是由同一字符组成的。
char * search(char *cpSource, char ch){         char *cpTemp=NULL, *cpDest=NULL;         int iTemp, iCount=0;         while(*cpSource)         {                 if(*cpSource == ch)                 {                          iTemp = 0;                          cpTemp = cpSource;                          while(*cpSource == ch)                                    ++iTemp, ++cpSource;                          if(iTemp > iCount)                                 iCount = iTemp, cpDest = cpTemp;                        if(!*cpSource)                             break;                 }                 ++cpSource;     }     return cpDest;}      
排序算法:

冒泡排序: 出现次数相当频繁

void buble(int *a,int n){    for(int i=0;i<n;i++)    {        for(int j=1;j<n-i;j++)        {            if(a[j]<a[j-1])            {                int temp=a[j];                a[j] = a[j-1];                a[j-1] = temp;            }        }    }}

插入排序:
void insertsort(int* a,int n){    int key;    for(int j=1;j<n;j++)    {        key = a[j];        for(int i=j-1;i>=0&&a[i]>key;i--)        {            a[i+1] = a[i];        }        a[i+1] = key;    }}

将一个数字字符串转换为数字."1234" -->1234

int atoii(char* s){    assert(s!=NULL);    int num = 0;    int temp;    while(*s>'0' && *s<'9')    {        num *= 10;        num += *s-'0';        s++;    }    return num;}



原创粉丝点击