Redian新闻
>
懒得写了,想练手的就写写贴在这里吧
avatar
懒得写了,想练手的就写写贴在这里吧# JobHunting - 待字闺中
b*m
1
某公司recruiter发来题目,看着没兴趣啊,不过有想C++练手的可以写着玩玩:
void string_reverse1(char *string)
{
/* your code here */
}
char *string_reverse2(const char *string)
{
/* your code here */
}
avatar
Q*e
2
现在recruiter都发考题了?
void string_reverse1(char *string)
{
int len = 0;
char * str = string;
char * head = string;
char * tail;
len = 0;
while (*str++) len++;

tail = head + len;
while (head < tail) {
swap(*head, *tail);
head++; tail--;
}
}

【在 b***m 的大作中提到】
: 某公司recruiter发来题目,看着没兴趣啊,不过有想C++练手的可以写着玩玩:
: void string_reverse1(char *string)
: {
: /* your code here */
: }
: char *string_reverse2(const char *string)
: {
: /* your code here */
: }

avatar
b*m
3

嗯,有些公司确实这么做,还有online考试的……

【在 Q*******e 的大作中提到】
: 现在recruiter都发考题了?
: void string_reverse1(char *string)
: {
: int len = 0;
: char * str = string;
: char * head = string;
: char * tail;
: len = 0;
: while (*str++) len++;
:

avatar
Q*e
4
//caller has to free memory allocated
char *string_reverse2(const char *string)
{
int len;
char * str = string;
char * res;
len = 0;
while (*str++) len++;
str--;
res = (char*) malloc(len + 1);
//sanity check here
while (len > 0) {
*res++ = *str--;
len--;
}
*res = '\0';
return res;
}

【在 b***m 的大作中提到】
:
: 嗯,有些公司确实这么做,还有online考试的……

avatar
b*m
5

突然对const有些迷糊了:const char *定义的参数,是说这个参数string变量本身不
能被修改,还是它对应的字符串空间的内容不能被修改?

【在 Q*******e 的大作中提到】
: //caller has to free memory allocated
: char *string_reverse2(const char *string)
: {
: int len;
: char * str = string;
: char * res;
: len = 0;
: while (*str++) len++;
: str--;
: res = (char*) malloc(len + 1);

avatar
Q*e
6
const char * string; // 内容只读
char * const string; //指针只读
const char * const string; //两者都只读

【在 b***m 的大作中提到】
:
: 突然对const有些迷糊了:const char *定义的参数,是说这个参数string变量本身不
: 能被修改,还是它对应的字符串空间的内容不能被修改?

avatar
l*a
7
是字符串空间里的内容不能被修改。

【在 b***m 的大作中提到】
:
: 突然对const有些迷糊了:const char *定义的参数,是说这个参数string变量本身不
: 能被修改,还是它对应的字符串空间的内容不能被修改?

avatar
b*m
8

唉,用了那么多年C++,一直还是觉得C++很坑爹啊!

【在 Q*******e 的大作中提到】
: const char * string; // 内容只读
: char * const string; //指针只读
: const char * const string; //两者都只读

avatar
b*m
9
QuickTime你的第二个函数是不是会把'\0'拷贝到res的第一个字符位置哦?
avatar
Q*e
10
随便写了写, 没有检查, 用test case看看呢
给个test case

【在 b***m 的大作中提到】
: QuickTime你的第二个函数是不是会把'\0'拷贝到res的第一个字符位置哦?
avatar
b*m
11
哦,不会,在进入while loop之前,str已经减1了一次了。

【在 Q*******e 的大作中提到】
: 随便写了写, 没有检查, 用test case看看呢
: 给个test case

avatar
l*a
12
Please try nu

【在 Q*******e 的大作中提到】
: 随便写了写, 没有检查, 用test case看看呢
: 给个test case

avatar
s*k
13
char * str = string;?
编译能过吗?貌似不能assign const char* to char*吧

【在 b***m 的大作中提到】
: 哦,不会,在进入while loop之前,str已经减1了一次了。
avatar
b*m
14
应该可以的。也许需要强制转换?

【在 s********k 的大作中提到】
: char * str = string;?
: 编译能过吗?貌似不能assign const char* to char*吧

avatar
s*k
15
至少我记得不行,可以const char* = char*, 但是不能反过来。
刚用VC编译了一下也报错了。

【在 b***m 的大作中提到】
: 应该可以的。也许需要强制转换?
avatar
b*m
16
加强制转换呢?

【在 s********k 的大作中提到】
: 至少我记得不行,可以const char* = char*, 但是不能反过来。
: 刚用VC编译了一下也报错了。

avatar
s*k
17
可以,另外这个程序也有bug,返回的res已经是string末尾的指针,不是头指针

【在 b***m 的大作中提到】
: 加强制转换呢?
avatar
b*m
18

嗯,这个我看到了,我的版本是这样:
// caller has to free memory allocated
char *string_reverse2(const char *string)
{
if( !string ) return NULL;
int len = strlen(string);
char *res = (char *)malloc(len + 1);
if( !res ) return NULL;

char *str = (char *)string + len - 1;
while( str >= string )
{
*res = *str;
res++;
str--;
}
*res = '\0';
return res;
}

【在 s********k 的大作中提到】
: 可以,另外这个程序也有bug,返回的res已经是string末尾的指针,不是头指针
avatar
s*k
19
你这个不是一样吗?也是res在string end?

【在 b***m 的大作中提到】
:
: 嗯,这个我看到了,我的版本是这样:
: // caller has to free memory allocated
: char *string_reverse2(const char *string)
: {
: if( !string ) return NULL;
: int len = strlen(string);
: char *res = (char *)malloc(len + 1);
: if( !res ) return NULL;
:

avatar
b*m
20

我擦,删代码删多了……

【在 s********k 的大作中提到】
: 你这个不是一样吗?也是res在string end?
avatar
Q*e
21
gcc 报告warning
str_reverse2.c: In function `string_reverse2':
str_reverse2.c:7: warning: initialization discards qualifiers from pointer
target type

【在 s********k 的大作中提到】
: 至少我记得不行,可以const char* = char*, 但是不能反过来。
: 刚用VC编译了一下也报错了。

avatar
c*s
22
This is my implementation for these two functions, and a driver to run them.
cat string_reverse.c
#include
#include
#include
void swap(char* c1, char* c2);
// -----------------
void string_reverse1(char* string) {
if (string == NULL)
return;
char* head = string;
char* tail = head;
// find the tail
while (*tail) ++tail;
--tail;
// while loop to do the swap
while (head < tail)
swap(head++, tail--);
}
// -----------------
// the caller of this function is responsible to free the memory
// of the newly created string.
char* string_reverse2(const char* string) {
if (string == NULL)
return NULL;
const char* tail = string;
int len = 0;
// determine the length of the string and its tail
while (*tail) {
++tail;
++len;
}
--tail;

// malloc a new string
char* r = (char*)malloc(sizeof(char)*(len+1));
if (r == NULL) {
printf("r == NULL\n");
exit(-1);
}

// while loop to copy the string backward
char* head = r;
while (tail >= string) {
*head = *tail;
++head;
--tail;
}
*head = '\0';

return r;
}
// -----------------
int main(int argc, char** argv) {
// use string_reverse2
int argc_in = argc;
while (argc_in-- != 0) {
char* reversed = string_reverse2(argv[argc_in]);
printf("arg[%d] = %s, reversed = %s\n", argc_in, argv[argc_in], reversed
);
free(reversed); reversed = NULL;
}
char* empty = "";
printf("empty = %s ", empty);
string_reverse1(empty);
printf("reversed = %s\n", empty);
printf("**************************************\n");
// use string_reverse1
argc_in = argc;
while (argc_in-- != 0) {
printf("arg[%d] = %s ", argc_in, argv[argc_in]);
string_reverse1(argv[argc_in]);
printf("reversed = %s\n", argv[argc_in]);
}
empty = "";
printf("empty = %s ", empty);
string_reverse1(empty);
printf("reversed = %s\n", empty);
return 0;
}
void swap(char* c1, char* c2) {
assert(c1 != NULL && c2 != NULL);
char tmp = *c1;
*c1 = *c2;
*c2 = tmp;
}
avatar
l*8
23
char * string_reverse1(char *string)
{
if (string) {
for(int i=0, j=strlen(string)-1; i < j; i++, j--)
swap(string[i], string[j]);
}
return string;
}
char *string_reverse2(const char *string)
{
return string_reverse1(strdup(string));
}

【在 b***m 的大作中提到】
: 某公司recruiter发来题目,看着没兴趣啊,不过有想C++练手的可以写着玩玩:
: void string_reverse1(char *string)
: {
: /* your code here */
: }
: char *string_reverse2(const char *string)
: {
: /* your code here */
: }

avatar
c*s
24
nice!

【在 l*********8 的大作中提到】
: char * string_reverse1(char *string)
: {
: if (string) {
: for(int i=0, j=strlen(string)-1; i < j; i++, j--)
: swap(string[i], string[j]);
: }
: return string;
: }
: char *string_reverse2(const char *string)
: {

avatar
a*1
25
如果 输入 是 char * s = "hello world"; 的话
是不是 只能用 第二个了???? 因为 s 里的字符改变不了。。
char *string_reverse2(const char *string)
{
/* your code here */
}
avatar
T*s
26
a recruiter in FB sent a link to my linked in account
i checked in and see some document on line
i browsed them and email him some feedback
then he called me to arrange interview but i rejected

【在 Q*******e 的大作中提到】
: 现在recruiter都发考题了?
: void string_reverse1(char *string)
: {
: int len = 0;
: char * str = string;
: char * head = string;
: char * tail;
: len = 0;
: while (*str++) len++;
:

avatar
a*n
27
这是考的就是IN-PLACE REVERSE 和 非IN-PLACE REVERSE吧。。。。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。