百度面试题

一、 一个文本文件有多行,每行为一个URL。请编写代码,统计出URL中的文件名及出现次数。

  a) 文件名不包括域名、路径和URL参数,例如http://www.rs.com/n.op/q/rs?id=1中的文件名是rs。

  b) 部分URL可能没有文件名,例如http://www.abc.com/,这类统计为“空文件名”。

  c) 出现在不同URL中的相同文件名视为同一文件名,例如http://www.ceshi.com/hi.php和ftp://ftp.cdef.com/hi.php为同一文件名

  文件内容示例如下:

  http://www.test.com/abc/de/fg.php?id=1&url=http://www.test.com/index.html

  http://www.ceshi.com/hi.jsp

  ftp://ftp.ceshi.com/hi.jsp

  http://www.hello.com/cw/hi.jsp?k=8

  http://www.hi.com/jk/l.html?id=1&s=a.html

  http://www.rs.com/n.op/q/rs?id=1

  http://www.abc.com/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void UrlFileName(char* fileName, char* p[], int nLen);
void GetUrlFileName(const char* fileName);
char* StringTrim(char* pFile);
char* GetFileName(const char* pstrUrl,int nStrLen);

void UrlFileName(char* fileName, char* p[], int nLen)
{
int nCount = 0;
for(int i = 0 ;i < nLen;++i)
{
if(!strcmp(p[i], fileName))
{
++nCount;
}
}
printf("文件名%s出现的次数为%d\n", fileName, nCount);
free(p[i]);
}

void GetUrlFileName(const char* fileName)
{
FILE* fRead = fopen(fileName, "r");
FILE* fWrite = fopen("d:\\write.txt", "w");
char szFile[1000] = {0};
char* p[100] = {NULL};
char* p1[100] = {NULL};
int index = 0;
int nflag = 0;
int index1 = 0;
while(1)
{
memset(szFile, 0, 1000);
fgets(szFile, 1000, fRead);
strcpy(szFile, StringTrim(szFile));
int nStr = strlen(szFile);
if(nStr == 1 || nStr == 0) continue;
fputs(szFile, fWrite);
p[index] = GetFileName(szFile, nStr);
for(int i = 0;i < index1;++i)
{
if(!strcmp(p1[i], p[index]))
{
++nflag;
break;
}
}
if(nflag == 0)
{
p1[index1] = p[index];
++index1;
}else
{
nflag = 0;
}
++index;
if(feof(fRead)) break;
}
for(int i = 0;i < index1;++i)
{

UrlFileName(p1[i], p, index);
}
fclose(fRead);
fclose(fWrite);
}
/*
去除前后空格
abcdefg
*/
char* StringTrim(char* pFile)
{
char* pStr = pFile;
while(*pFile != 0)
{
if(*pFile != ' ')
{
break;
}else
{
++pStr;
}
++pFile;
}
int i = strlen(pFile) - 1;
do
{
if(*(pFile+i) != ' ')
{
break;
}else
{
*(pStr+i) = '\0';
}
--i;
} while (i >= 0);

return pStr;
}

/*
截取最后一个反斜的杠的位置和"?"号的位置
*/
char* GetFileName(const char* pstrUrl,int nStrLen)
{
char* pUrlName = (char*)malloc(20);
memset(pUrlName, 0, 20);
int i = 0;
int nCount1 = 0;
int nCount2 = 0;
do
{
if('/' == *(pstrUrl+i))
{
if(nCount1 != 0)
{
nCount1 = 0;
}
nCount1 += i;
}
if('?' == *(pstrUrl+i))
{
nCount2 += i;
break;
}
++i;
}while(i < nStrLen);

if(!nCount2)
{
nCount2 = i-1;
}
i = nCount1 + 1;
int urlIndex = 0;
if(i == nCount2)
{
pUrlName = "空文件";
}else
{
for(;i < nCount2;++i)
{
*(pUrlName+urlIndex) = *(pstrUrl+i);
++urlIndex;
}
}
return pUrlName;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值