子字符串出現的次數

華爲的一道比較基礎的面試題,就是查找一個字符串中某個子字符串出現的次數,並只考慮最左對齊的情況。

    如在"helloejesjhelloejedjshhello"中查找子字符串"hello"出現的次數。

    最直接的方法是使用遍歷實現。

int string_find( char str[], char substr[] )
{
    int i, j, check ,count = 0;
    int len = strlen( str );        /*取得字符串長度,不包括'\0'*/
    int sublen = strlen( substr );
    for( i = 0; i < len; i++ )
    {
        check = 1;        /*檢測標記*/
        for( j = 0; j + i < len && j < sublen; j++ )        /*逐個字符進行檢測,在sublen長度內,一旦出現不同字符便將check置爲0*/
        {
            if( str[i + j] != substr[j] )
            {
                check = 0;
                break;
            }
        }
        if( check == 1 )        /*在sublen長度內的字符都相等*/
        {
            count++;
            i = i + sublen;        /*調整檢測起始位置*/
        }
    }
    return count;
}



/*********************************************************************
 * Author  : Samson
 * Date    : 01/09/2012
 * Test platform:
 *               GNU Linux version 2.6.29.4
 *               gcc version 4.4.0 20090506 (Red Hat 4.4.0-4) (GCC) 

 * *******************************************************************/


/* locate a substring */

char * _strstr(char *s1, char *s2)
{
    char c1, c2;
    int sublen = _strlen(s2);

    c2 = *s2++;

    do
    {

        do
        {
            c1 = *s1++;
            
            if(0 == c1)
                return NULL;
                            
        }while(c1 != c2);

    }while(_strncmp(s1, s2, sublen));
    
    return --s1;
}


/* locate  substring of N characters */

char * _strstrn(char *s1, char *s2, int n)
{
        char c1, c2;

        c2 = *s2++;

        do
        {
                do
                {
                    c1 = *s1++;

                    if(0 == c1)
                    {
                        return NULL;    
                    }
                }while(c1 != c2);

        }while(_strncmp(s1, s2, n - 1));

        return --s1;
}


/* locate  substring of N characters and Ignore case*/

unsigned char * _strnocasestrn(unsigned char *s1, unsigned char *s2, int n)
{
        unsigned char c1, c2;

        c2 = *s2++;
        (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;

        do
        {

            do
            {
                c1 = *s1++;
                if(0 == c1)
                    return NULL;
                
                c1 = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;
            }while(c1 != c2);

        }while(_strncmp(s1, s2, n - 1) != 0);
     
     return --s1;

}


以上實現中的_strncmp請參照http://blog.csdn.net/yygydjkthh/article/details/7188518中的具體實現


  1. /***************************************************************************** 
  2.     *  @COPYRIGHT NOTICE 
  3.     *  @Copyright (c) 2013, ChenMH 
  4.     *  @All rights reserved 
  5.  
  6.     *  @file     : TestCharacter.h 
  7.     *  @version  : ver 1.0 
  8.  
  9.     *  @author   : ChenMH 
  10.     *  @date     : 2013/5/17 15:04 
  11.     *  @brief    : C語言中,一種從字符串中獲取字符所在位置的方法。 
  12.                     是在偶然中從一處開源代碼裏看到,記錄之...... 
  13. *****************************************************************************/  
  14.   
  15. #include <string.h>  
  16.   
  17. int main(int argc, char **argv)  
  18. {  
  19.     //計算字符w在字符串string中的位置  
  20.     char* string = "Hello world!";  
  21.     char  c = 'w';  
  22.     char* ptr = strchr(string, c);  
  23.     int pos = ptr-string;  
  24.     if (ptr)  
  25.         printf("The character [%c] was found at pos: [%d]\n", c, pos);  
  26.     else  
  27.         printf("The character was not found\n");  
  28.           
  29.         return 0;  
  30.  }  
  1. /************************************************************************** 
  2.     *  @Copyright (c) 2013, ChenMH, All rights reserved. 
  3.  
  4.     *  @file     : main.cpp 
  5.     *  @version  : ver 1.0 
  6.  
  7.     *  @author   : ChenMH 
  8.     *  @date     : 2013/05/17 16:45 
  9.     *  @brief    : 使用strrchr截取字符串 
  10. **************************************************************************/  
  11. #include <cstdio>  
  12. #include <cstring>  
  13.   
  14. //(C語言)從當前文件的路徑中截取文件名,及獲取文件名首字母在字符串中的位置  
  15. int main(int argc, char **argv)  
  16. {  
  17.     const char* pName = strrchr(*argv,'/');  
  18.     if (!pName) pName = strrchr(*argv,'\\');  
  19.     if (pName)  pName++;  
  20.   
  21.     int pos = pName - *argv;  
  22.     printf("file path: [%s]\n", *argv);  
  23.     printf("file name: [%s]; pos: [%d]\n", pName, pos);  
  24.     return 0;  


題目:編一程序,將兩個字符串連接起來,不要用strcat函數。
第1種方法:

main()
{
char str1[10]="world!";
char str2[20]="hello ";
strcpy(str2+strlen(str2),str1);
printf("%s",str2);
}

C語言中strcpy,strcmp,strlen,strcat函數原型
今天去文思創新面試,考官問了我一個簡單的實現,即:自己編寫strcpm的實現,IBM曾經也考過寫strcpy原型,這幾個函數在面試的時候經常被考到,很具有代表性,突然被問起還真有點措手不及呢。現在記下供大家學習和以後溫習:(下面的程序經本人通過)


1、Strcat函數原型如下:
char *strcat(char *strDest, const char *strScr) //將源字符串加const,表明其爲輸入參數
{
       char * address = strDest;             //該語句若放在assert之後,編譯出錯
       assert((strDest != NULL) && (strScr != NULL)); //對源地址和目的地址加非0斷言
       while(*strDest)             //是while(*strDest!=’\0’)的簡化形式
       {                        //若使用while(*strDest++),則會出錯,因爲++是不受循環
              strDest++;               //約束的。所以要在循環體內++;因爲要是*strDest最後指
       }                        //向該字符串的結束標誌’\0’。
       while(*strDest++ = *strScr++) 
       {
              NULL;                 //該循環條件內可以用++,
       }                          //此處可以加語句*strDest=’\0’;有無必要?
return address;               //爲了實現鏈式操作,將目的地址返回
}
以下是在VC6.0中調試的例子,函數名用strcata代替。
#include <stdio.h>
#include <assert.h>
char *strcata(char *strDest,const char *strScr)
{
       char * address = strDest;
       assert((strDest != NULL) && (strScr != NULL));
       while(*strDest)
       {
              strDest++;
       }
       while(*strDest++ = *strScr++)
       {
              NULL;
       }
       return address;
}


void main()
{
       char str1[100]={"i love"};
       char str2[50]={"China"};
       printf("%s\n",strcata(str1,str2));
}
2、Strcpy函數原型如下:
char *strcpy(char *strDest, const char *strScr)
{
       char *address=strDest;
       assert((strDest != NULL) && (strScr != NULL));
       while(*strScr)                   //是while(*strScr != ’\0’)的簡化形式;
       {
              *strDest++ = *strScr++;
       }
       *strDest = '\0';                       //當strScr字符串長度小於原strDest字符串長度
       return address;                      //時,如果沒有改語句,就會出錯了。
}
以下是在VC6.0中調試的例子,函數名用strcpya代替。
#include <stdio.h>
#include <assert.h>
char *strcpya(char *strDest, const char *strScr)
{
       char *address = strDest;
       assert((strDest != NULL) && (strScr != NULL));
       while(*strScr)
       {
              *strDest++ = *strScr++;
       }
       *strDest = '\0';
       return address;
}


void main()
{
       char str1[100]={"i love"};
       char str2[50]={"China"};
       printf("%s\n",strcpya(str1,str2));
}
3、Strcmp函數原型如下:
int strcmp (const char *str1,const char *str2)
{           
       int len = 0;
       assert((str1 != '\0') && (str2 != '\0'));
       while(*str1 && *str2 && (*str1 == *str2))
       {
              str1++;
              str2++;
       }
       return *str1-*str2;
}
以下是在VC6.0中調試的例子,函數名用strcmpa代替。
#include <stdio.h>
#include <assert.h>
int strcmpa (const char *str1,const char *str2)
{           
       int len = 0;
       assert((str1 != '\0') && (str2 != '\0'));
       while(*str1 && *str2 && (*str1==*str2))
       {
              str1++;
              str2++;
       }
       return *str1-*str2;
}


void main()
{
       char str1[100] = {"i love"};
       char str2[50] = {"China "};
       printf("%d\n",strcmpa(str1,str2));
}
4、Strlen函數原型如下:
int strlen(const char *str)
{
    int len = 0;
       assert(str != NULL);
       while(*str++)
       {
              len++;
       }
       return len;
}
以下是在VC6.0中調試的例子,函數名用strlena代替。
#include <stdio.h>
#include <assert.h>
int strlena(const char *str)
{
    int len = 0;
       assert(str != NULL);
       while(*str++)
       {
              len++;
       }
       return len;
}
void main()
{
       char str1[100] = {"i love"};
       char str2[50] = {"China "};
       printf("%d\n",strlena(str1));
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章