腾讯面试题

来源:互联网 发布:数据库应用教程 编辑:程序博客网 时间:2024/06/06 03:58

  在一篇英文文章中查找指定的人名,人名使用二十六个英文字母(可以是大写或小写)、空格以及两个通配符组成(*、?),通配符“*”表示零个或多个任意字母,通配符“?”表示一个任意字母。
如:“J* Smi??” 可以匹配“John Smith” .

// FindName.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "stdio.h"bool isAlpha(char c){if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))return true;elsereturn false;}void strcpy(char* dest, const char* src, int len){for (int i = 0; i < len; i++){*dest = *src;src++;dest++;}*dest = 0;}void scan(const char* pszText, const char* pszName){int tmp;char nameFind[100];for (int i = 0; pszText[i] != NULL; i++){tmp = i;for (int j = 0; pszName[j] != NULL; j++, tmp++){if (pszName[j] == '*') {while(isAlpha(pszText[tmp]))tmp++;tmp--;continue;}if (pszName[j] == '?'){if (!isAlpha(pszText[tmp])) break;else continue;}if (pszText[tmp] != pszName[j])break;}strcpy(nameFind, pszText+i, tmp - i);if (pszName[j] == NULL)printf("%s", nameFind);}}int main(int argc, char* argv[]){const char pszText[] = "Johcxcn Smith ss Johcxcn Smith";const char pszName[] = "J* Smi??";scan(pszText, pszName);return 0;}


0 0
原创粉丝点击