两个基本的c函数编写面试题,请高人指导。

ray9901 2005-12-30 11:37:36

1。编写一个 C 函数,该函数在一个字符串中找到可能的最长的子字符串,且该字符串是由
同一字符组成的。

2。请编写一个 C 函数,该函数在给定的内存区域搜索给定的字符,并返回该字符所在位置索引值。


比较急,请大侠们多多指教阿。
...全文
469 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuanchuang 2005-12-30
  • 打赏
  • 举报
回复
那自己写吧,很简单的。
yuanchuang 2005-12-30
  • 打赏
  • 举报
回复
第一个我写了,arr是传进去的字符串,subarr是传进去的一个字符变量的地址,不能是不知道指向哪的一个指针。用subarr传出字符,返回字符的个数。

int fun(char *arr, char *subarr)
{
int i = 0, num = 1, t = 1;
char ch = *subarr = *arr;
do
{
i++;
if (ch != arr[i])
{
if (num < t)
{
num = t;
*subarr = arr[i - 1];
}
t = 1;
ch = arr[i];
}
else
t++;
}
while (arr[i] != '\0');
return num;
}
ray9901 2005-12-30
  • 打赏
  • 举报
回复
楼上的兄弟,不能用有的,就是让你自己写一个c函数库的函数,是这个意思。
yuanchuang 2005-12-30
  • 打赏
  • 举报
回复
第二个,C里面有直接可以用的函数:strchr
pureqi 2005-12-30
  • 打赏
  • 举报
回复
可以去找java源代码的String.indexOf(String)看看,查找任意字符串可比查找相同字符的串要复杂多了.
ao168 2005-12-30
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <iostream>

#define MAX_LEN 256

using namespace std;

char * get_max_long_same_str(const char * in_str);

int main(int argc, char* argv[])
{
char *test_str = "122331344444567777777756666";
char *get_max_same_substr = new char[MAX_LEN];

get_max_same_substr = get_max_long_same_str(test_str);
cout<<"the max_sub_str of "<<test_str<<" is : "<<get_max_same_substr<<endl;

strcpy(test_str,"11111111111111");
get_max_same_substr = get_max_long_same_str(test_str);
cout<<"the max_sub_str of "<<test_str<<" is : "<<get_max_same_substr<<endl;

getch();

return 0;
}

char * get_max_long_same_str(const char * in_str)
{
char * out_str = new char[MAX_LEN];
char * out_str_save = out_str;

int max_str_len = 0;
char check_char = *in_str;
char the_char = check_char;
int jsq_len = 1;

while(*(++in_str))
{
if(check_char == *in_str)
{
jsq_len++;
if(max_str_len < jsq_len)
{
max_str_len = jsq_len;
the_char = check_char;
}
}
else
{
if(max_str_len < jsq_len)
{
max_str_len = jsq_len;
the_char = check_char;
}
check_char = *in_str;
jsq_len = 1;
}
}

for(int i = 0; i < max_str_len; i++)
{
*out_str++ = the_char;
}
*out_str = '\0';

return out_str_save;

}
yuanchuang 2005-12-30
  • 打赏
  • 举报
回复
megaboy(飞天御剑流之杀神一刀斩)和kevlin(国王企鹅):
你们编的程序测试了吗?
我大概看了一下,感觉你们的不合题意啊?
不知道你们想实现什么功能呢?
kevlin 2005-12-30
  • 打赏
  • 举报
回复
第一题
int findstr(char* str,char findc)
{
int m=0,n=0,i=0;
while(str[i]!=0)
{
if(findc==str[i])
++m;
else
{
if(m>n)
n=m;
m=0;
}
++i;
}
return n;
}
Echone902 2005-12-30
  • 打赏
  • 举报
回复
mark
dyh0 2005-12-30
  • 打赏
  • 举报
回复
mark
megaboy 2005-12-30
  • 打赏
  • 举报
回复
第一题:

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;
}

第二题:

int search(char *cpSource, int n, char ch)
{
int i;
for(i=0; i<n && *(cpSource+i) != ch; ++i);
return i;
}

如果返回值等于n,表示该区域没有字符ch。
Cliven 2005-12-30
  • 打赏
  • 举报
回复
第一题:
int SString( char*p)
{
int i=0,j=1,c=0,a=1;
while( i<strlen(p) && j<strlen(p) )
{
if (*(p+i)==*(p+j))
{
c++;i++;j++;
}
else
{
if(c>a)
{
a=c+1;
}
c=0;
i++;j++;

}
}
return a;

}

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧