Last active
August 29, 2015 14:23
-
-
Save icemilo/4d8286bdb27354e51039 to your computer and use it in GitHub Desktop.
Cracking the Coding Interview Question 1.4
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
// | |
// main.c | |
// Question1.4 | |
// | |
// Created by JAE SEUNG KOO on 6/24/15. | |
// Copyright (c) 2015 JAE SEUNG KOO. All rights reserved. | |
// | |
#include <stdio.h> | |
#define LENGTH 18 | |
char * replaceStr(char *str, int strlen); | |
int main(int argc, const char * argv[]) { | |
char str[LENGTH] = "Mr John Smith "; | |
printf("Output : %s\n", replaceStr(str, strlen(str))); | |
return 0; | |
} | |
char * replaceStr(char *str, int strlen){ | |
int str_pos = 0; | |
int lastchr = strlen-1; | |
for(str_pos = lastchr; str[str_pos] <= 32; str_pos--); | |
for(int current_chr = str_pos; current_chr > 0; current_chr--, lastchr--){ | |
str[lastchr] = str[current_chr]; | |
if(str[current_chr] == 32){ | |
str[lastchr] = '0'; | |
str[--lastchr] = '2'; | |
str[--lastchr] = '%'; | |
} | |
} | |
return str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment