Skip to content
Snippets Groups Projects
Commit 8e62aa53 authored by Andrew To's avatar Andrew To
Browse files

Hw2: small fix

parent f4f730e3
No related branches found
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
......@@ -21,7 +21,7 @@ int setup_buff(char *buff, char *user_str, int len){
bool isSpace = false;
int count = 0;
while (*user_str != '\0' && count < BUFFER_SZ - 1) { // stop when reaching the end-of-buffer
while (*user_str != '\0' && count < BUFFER_SZ) { // stop when reaching the end-of-buffer
if (*user_str != '\t'){
if (*user_str != ' ' || isSpace == false) {
*buff = *user_str;
......@@ -42,7 +42,7 @@ int setup_buff(char *buff, char *user_str, int len){
}
}
int ret = count;
if (count > len){
if (count >= BUFFER_SZ){
return -1;
}
while (count <= len){
......@@ -51,7 +51,7 @@ int setup_buff(char *buff, char *user_str, int len){
count++;
}
return ret; //for now just so the code compiles.
return ret;
}
void print_buff(char *buff, int len){
......@@ -76,6 +76,11 @@ int count_words(char *buff, int len, int str_len){
if (str_len==0){
return 0;
}
if (str_len < 0){
return -1;
}
int words = 1;
for (int i=0; i<str_len; i++){
if (*buff == ' '){
......@@ -137,7 +142,6 @@ void word_print(char *buff, int len, int str_len){
printf(" (%d)\n",track);
}
// void replace(char *buff, *new_buff)
int main(int argc, char *argv[]){
......@@ -191,7 +195,7 @@ int main(int argc, char *argv[]){
buff= (char*)malloc(BUFFER_SZ);
user_str_len = setup_buff(buff, input_string, BUFFER_SZ); //see todos
if (user_str_len < 0){
printf("Error setting up buffer, error = %d", user_str_len);
printf("Error setting up buffer, error = %d\n", user_str_len);
exit(2);
}
......@@ -219,11 +223,9 @@ int main(int argc, char *argv[]){
printf("Need 3 arguments: sample string and 2 other strings\n");
exit(1);
} else {
// buff= (char*)malloc(BUFFER_SZ);
// old_string = argv[3];
// old_str_len = setup_buff()
// new_string = argv[4];
// new_str_len = setup_buff(buff, new_string, BUFFER_SZ);
// char *old_string = argv[3];
// char *new_string = argv[4];
printf("Not Implemented\n");
break;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment