Profile

ys2310

Author:ys2310
2008年春にNew York Cityにあるふる〜い大学を卒業。


Categories


new postings


new comments


new trackbacks


monthly archeive


FC2ブログ 転職
DATE: CATEGORY:スポンサー広告
上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。
| BLOG TOP |
DATE: CATEGORY:C/C++ puzzle
Q: What, if trying to copy a string of 10 bytes to another string declared as 5 bytes, happens?

Answer:
char *a=(char *)malloc(sizeof(char)*5);
char b[5];
char *str="Hello World";

cout<cout<
分かるようにcopy 先が足りない場合は、あたかも割り当てられてるようにcopy している。
結果としては、どちらもHello Wolrdが出力されるが、memory動作は不安定である。

From Bloomberg phone interview.

| BLOG TOP |
DATE: CATEGORY:C/C++ puzzle
Q: Remove characters from a string inplace. (without using extra memory)

Answer:
void remspaces(char *s)
{
int i, j = 0;

for (i = 0; s[i]; i++)
{
if (isspace(s[i]))
continue;

s[j++] = s[i];
}

s[j] = 0;
}

Comment:
replace "isspace" with any characters you want to ignore.

| BLOG TOP |
DATE: CATEGORY:C/C++ puzzle
Q: Write code to find the sqrt of a number without using any library functions

A:
{double SquareRoot(int num) {
if(num == 0)
return 0;
if(num == 1)
return 1;

double guess = num/2, oldguess = 0;
while(guess != oldguess) {
oldguess = guess;
guess = (num/guess + guess)/2;
printf("%f %f\n", oldguess, guess);
}
return guess;
}}

negative number doesn't work yet.
need improvment

[Quote]:http://www.careercup.com/question/?q=ca3b3d82-866d-4fc1-b990-249ae1a100c1

| BLOG TOP |
DATE: CATEGORY:C/C++ puzzle
Q: How will you find the longest palindrome in a string? I.e. if the string is XMADAMYX, Your code should print MADAM.

Answer:
| BLOG TOP |
DATE: CATEGORY:C/C++ puzzle
Q: Remove a fixed character from a C-string

Answer:
example1 getting rid of char 'o' from string "Hello World"

char *str = "Hello World";
char *str2 = (char *)calloc(20,sizeof(char));

for (int i=0; (*str)!='\0'; str++,i++)
{
    if ((*str) == 'o')       
        *(str2+i) = *++str;               
    else       
        *(str2+i) = *str;                   
}
cout<<str2<<endl;
| BLOG TOP |

copyright © Manhattan life all rights reserved.Powered by FC2ブログ