Created
April 17, 2012 03:03
-
-
Save bxshi/2403101 to your computer and use it in GitHub Desktop.
CareerCup 150 1.5 @1point3acres
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* CareerCup 150 4/16-4/22 1.5 | |
* | |
* slaink@1point3acres | |
* | |
*/ | |
/* | |
* 简单来说,就是一个字符替换成三个字符的问题 | |
* 从后往前遍历,如果找到了空格,空格后的字符串向右移动两个char,然后修改。 | |
* 这样的话空间复杂度1,时间复杂度的话不会算……应该和空格数相关吧 | |
* | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int replace_whitespace(char *string); | |
int main(int argc, char **argv) | |
{ | |
char *string; | |
if(argc!=2) { | |
printf("usage:<program name> <string>\n"); | |
return 0; | |
} | |
string =malloc(sizeof(*argv[1])); | |
strcpy(string, argv[1]); | |
replace_whitespace(string); | |
printf("%s\n", string); | |
free(string); | |
return 0; | |
} | |
int replace_whitespace(char *string) | |
{ | |
const char *wspace = "\%20"; | |
size_t i; | |
int cnt=0; | |
i = strlen(string) - 1; | |
while(i>0) { /* iterator, find out whitespace in string */ | |
if(string[i] == ' ') { | |
cnt++; | |
string = realloc(string, sizeof(char) *2); | |
strcpy(&string[i+3], &string[i+1]); | |
string[i] = wspace[0]; | |
string[i+1] = wspace[1]; | |
string[i+2] = wspace[2]; | |
} | |
i--; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment