由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - A technical question about C
相关主题
说说某著名软件公司的onsite面试看到一个c的面试题,求教。
bloomberg onsite & offer问个C的基本问题
菜鸟求救 请大家看看我的代码有没有问题large file的一道题
请问strcpy()和memcpy()的写法问题两道F电面题
两道面试题,请大家说说看法写程序时的一个小问题?
怎么检查极端情况?问个SQL
Facebook phone screen问个算法题
A simple interview question这个拷贝构造函数有什么问题?
相关话题的讨论汇总
话题: char话题: return话题: null话题: loop话题: technical
进入JobHunting版参与讨论
1 (共1页)
l**n
发帖数: 88
1
Is there a problem with the following code?
char* strchr(char* s, char c)
{ for (char* p=s; *p; p++)
if (*p==c) return p;
return 0;
}
r****o
发帖数: 1950
2
My thinking:
1. need to check if s is NULL
2. need to know the length of s, so the loop is only done within the length
of s.

【在 l**n 的大作中提到】
: Is there a problem with the following code?
: char* strchr(char* s, char c)
: { for (char* p=s; *p; p++)
: if (*p==c) return p;
: return 0;
: }

g**u
发帖数: 583
3
I guess you need to modify the code, especially the logic to end the circle:
char *p;
for(p=s;p;p++)
{
if(c==*p)
return p;
}
return NULL;
n*******s
发帖数: 482
4
seems no problem ...
b**********r
发帖数: 46
5

logic error.

【在 l**n 的大作中提到】
: Is there a problem with the following code?
: char* strchr(char* s, char c)
: { for (char* p=s; *p; p++)
: if (*p==c) return p;
: return 0;
: }

k********n
发帖数: 1819
6
I didn't see you change anything

circle:

【在 g**u 的大作中提到】
: I guess you need to modify the code, especially the logic to end the circle:
: char *p;
: for(p=s;p;p++)
: {
: if(c==*p)
: return p;
: }
: return NULL;

r**u
发帖数: 1567
7
This is even worse than the original one. If s is not NULL, and if c is not
in s, then this loop will go over boundary and segment fault.
in Ansi C, you can't do something like:
for (char *p = s; *p; p++)
should be:
char *p;
for (p = s; *p; p++)
of course the top one is correct if you compiled it with g++.

circle:

【在 g**u 的大作中提到】
: I guess you need to modify the code, especially the logic to end the circle:
: char *p;
: for(p=s;p;p++)
: {
: if(c==*p)
: return p;
: }
: return NULL;

h**k
发帖数: 3368
8
*p 和 *p != '\0' 应该是等价的吧?
C会自动把*p转成bool。当然这么写的编程风格极差。
g**u
发帖数: 583
9

not
In fact, the loop will not go forever, after reaching the end, p=='\0',
therefore the loop will end here.
In fact I tested two case: case 1: string is empty, case 2: c not in s, both
return NULL pointer. please correct me if I made mistake somewhere.

【在 r**u 的大作中提到】
: This is even worse than the original one. If s is not NULL, and if c is not
: in s, then this loop will go over boundary and segment fault.
: in Ansi C, you can't do something like:
: for (char *p = s; *p; p++)
: should be:
: char *p;
: for (p = s; *p; p++)
: of course the top one is correct if you compiled it with g++.
:
: circle:

g**u
发帖数: 583
10

I did change a little bit, firstly the declaration is moved out of the loop,
which complies with C89,; another one is I compare the p to '\0', rather
than *p...

【在 k********n 的大作中提到】
: I didn't see you change anything
:
: circle:

1 (共1页)
进入JobHunting版参与讨论
相关主题
这个拷贝构造函数有什么问题?两道面试题,请大家说说看法
leetcode上wild match怎么检查极端情况?
这两个edit distance的codeFacebook phone screen
懒得写了,想练手的就写写贴在这里吧A simple interview question
说说某著名软件公司的onsite面试看到一个c的面试题,求教。
bloomberg onsite & offer问个C的基本问题
菜鸟求救 请大家看看我的代码有没有问题large file的一道题
请问strcpy()和memcpy()的写法问题两道F电面题
相关话题的讨论汇总
话题: char话题: return话题: null话题: loop话题: technical