華爲的一道比較基礎的面試題,就是查找一個字符串中某個子字符串出現的次數,並只考慮最左對齊的情況。
如在"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 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 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中的具體實現
- /*****************************************************************************
- * @COPYRIGHT NOTICE
- * @Copyright (c) 2013, ChenMH
- * @All rights reserved
- * @file : TestCharacter.h
- * @version : ver 1.0
- * @author : ChenMH
- * @date : 2013/5/17 15:04
- * @brief : C語言中,一種從字符串中獲取字符所在位置的方法。
- 是在偶然中從一處開源代碼裏看到,記錄之......
- *****************************************************************************/
- #include <string.h>
- int main(int argc, char **argv)
- {
- //計算字符w在字符串string中的位置
- char* string = "Hello world!";
- char c = 'w';
- char* ptr = strchr(string, c);
- int pos = ptr-string;
- if (ptr)
- printf("The character [%c] was found at pos: [%d]\n", c, pos);
- else
- printf("The character was not found\n");
- return 0;
- }
- /**************************************************************************
- * @Copyright (c) 2013, ChenMH, All rights reserved.
- * @file : main.cpp
- * @version : ver 1.0
- * @author : ChenMH
- * @date : 2013/05/17 16:45
- * @brief : 使用strrchr截取字符串
- **************************************************************************/
- #include <cstdio>
- #include <cstring>
- //(C語言)從當前文件的路徑中截取文件名,及獲取文件名首字母在字符串中的位置
- int main(int argc, char **argv)
- {
- const char* pName = strrchr(*argv,'/');
- if (!pName) pName = strrchr(*argv,'\\');
- if (pName) pName++;
- int pos = pName - *argv;
- printf("file path: [%s]\n", *argv);
- printf("file name: [%s]; pos: [%d]\n", pName, pos);
- 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));
}