Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Jul 13, 2020
1 parent ff15052 commit 0b15449
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 58 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
./.idea
.idea
cmake-build-debug
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ add_subdirectory(graphs)
add_subdirectory(interview)
add_subdirectory(forking)
add_subdirectory(assembly)
add_subdirectory(experiments)
6 changes: 6 additions & 0 deletions experiments/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.5)
project(reverse_proj)

set(SOURCE_FILES reverse_words.c)
add_executable(reverse ${SOURCE_FILES})

78 changes: 23 additions & 55 deletions experiments/reverse_words.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,67 +14,35 @@ int main(int argc, char **argv) {
return 0;
}

bool reverseWords(char str[]) {
bool reverseWords(char str[]) {
char *buffer;
int slen, tokenBegin, tokenEnd, writePos, copyPos;

int slen, tokenReadPos, wordReadPos, wordEnd, writePos = 0;
slen = strlen(str);

/* Position of the last character is length - 1 */
tokenReadPos = slen - 1;
buffer = (char *)malloc(slen + 1);
if (! buffer)
return false;

tokenBegin = tokenEnd = slen - 1;
writePos = copyPos = 0;

while (tokenBegin >= 0) {
if (str[tokenBegin] == ' ') {
buffer[writePos++] = str[tokenBegin--];
} else {
while (tokenBegin >= 0 && str[tokenBegin] != ' ')
--tokenBegin;
copyPos = tokenBegin + 1;
while (copyPos <= tokenEnd)
buffer[writePos++] = str[copyPos++];
if (!buffer) return false; /* reverseWords failed */
while (tokenReadPos >= 0) {
if (str[tokenReadPos] == ' ') { /* Non-word characters */
/* Write character */
buffer[writePos++] = str[tokenReadPos--];
} else { /* Word characters */
/* Store position of end of word */
wordEnd = tokenReadPos;
/* Scan to next non-word character */
while (tokenReadPos >= 0 && str[tokenReadPos] != ' ') tokenReadPos--;
/* tokenReadPos went past the start of the word */
wordReadPos = tokenReadPos + 1;
/* Copy the characters of the word */
while (wordReadPos <= wordEnd) {
buffer[writePos++] = str[wordReadPos++];
}
}
}

/* null terminate buffer and copy over str */
buffer[writePos] = '\0';
strlcpy(str, buffer, slen + 1);
strncpy(str, buffer, slen + 1);
free(buffer);

return true;
return true; /* ReverseWords successful */
}

//bool reverseWords(char str[]) {
// char *buffer;
// int slen, tokenReadPos, wordReadPos, wordEnd, writePos = 0;
// slen = strlen(str);
// /* Position of the last character is length - 1 */
// tokenReadPos = slen - 1;
// buffer = (char *)malloc(slen + 1);
//
// if (!buffer) return false; /* reverseWords failed */
// while (tokenReadPos >= 0) {
// if (str[tokenReadPos] == ' ') { /* Non-word characters */
// /* Write character */
// buffer[writePos++] = str[tokenReadPos--];
// } else { /* Word characters */
// /* Store position of end of word */
// wordEnd = tokenReadPos;
// /* Scan to next non-word character */
// while (tokenReadPos >= 0 && str[tokenReadPos] != ' ') tokenReadPos--;
// /* tokenReadPos went past the start of the word */
// wordReadPos = tokenReadPos + 1;
// /* Copy the characters of the word */
// while (wordReadPos <= wordEnd) {
// buffer[writePos++] = str[wordReadPos++];
// }
// }
// }
// /* null terminate buffer and copy over str */
// buffer[writePos] = '\0';
// strlcpy(str, buffer, slen + 1);
// free(buffer);
// return true; /* ReverseWords successful */
//}
4 changes: 2 additions & 2 deletions forking/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.5)
project(threads_exp_proj)
project(forking)

set(SOURCE_FILES main.c)
add_executable(threads_exp ${SOURCE_FILES})
add_executable(forking ${SOURCE_FILES})

0 comments on commit 0b15449

Please sign in to comment.