2

I'm stuck on a piece of code I have been working on. I need to receive N number of inputs using argc and argv[] parameters. Then the N number of inputs will allow the user to enter that many sentences. For every sentence, my code should reverse every word in the sentence. Currently, my code will take in the N value and sentence, but won't print the reverse sentence. Instead, it prints a blank line.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define SIZE 80

void get_input(char *line){
 fgets(line, SIZE, stdin);
 char *ptr = strchr(line, '\n');
 if (ptr){
     *ptr = '0'; }
}

 void reverse(char *line){
 char copy[SIZE];
 char word[SIZE];
 memset(copy, 0, SIZE);
 int line_len = strlen(line);
 int word_len = 0;
 int i;
 for(i=line_len; i<=0; --i){
     if(line[i] == ' ' && word_len > 0){
        memset(word, 0, SIZE);
        strncpy(word, line + i + 1, word_len);
        strcat(copy, word);
        strcat(copy, " ");
        word_len = 0;
     }else if(isalnum(line[i]) || line[i] == '\'')
          {word_len++;}
  }
  if(word_len>0){
     memset(word, 0, SIZE);
     strncpy(word, line, word_len);
     strcat(copy, word);}
     strcpy(line, copy);
}

int main(int argc, char *argv[]){
 int N = (int)strtol(argv[1], NULL, 10);
 if(N<0){
         printf("ERROR: Please provide an integer greater than or equal to 0\n");
         return 0;
        }
 if(N>SIZE){ printf("ERROR: Please provide an integer less than or equal to 80\n");
             return 0;
           }
     char line[SIZE];
     int i;
     for(i=0;i<N;i++){
                      get_input(line);
                      reverse(line);
                      printf("%s\n", line);
                     }
 return 0;
}

Example Input:

1

The fox jumped over a log

Example desired output:

log a over jumped fox The

Current output:

2
  • 2
    "my code does everything correctly but does not print the reverse sentence. Instead, it prints a blank line. " - since that's the only thing it was supposed to do, surely it doesn't do everything correctly? Commented Apr 4, 2016 at 2:02
  • Made the edit in my post Commented Apr 4, 2016 at 2:06

4 Answers 4

2

You put <= instead of >=

for(i = line_len; i <= 0; --i) {
Sign up to request clarification or add additional context in comments.

4 Comments

What does *=< mean?
x >= z: this means X is higher than or equal than z. x <= z: this means z is higher than or equal than x.
Okay but *=< isn't an operator.
My mistake, I meant <=.
0

A simple silly mistake:

for(i=line_len; i<=0; --i){

What does this loop do? Well, first it sets i to line_len. Then it loops while i is less than or equal to zero, and at the end of each loop it decreases i by one.

This will loop for a long time (until i overflows) if line_len is zero, and otherwise it won't loop at all (because i will be greater than 0).

You probably meant >= instead of <=.

You also might've meant i=line_len-1 instead of i=line_len, but it's hard to tell.

1 Comment

Yup, that is what I have been missing this whole time thanks
0

EDITS: Changed the i<=0 in the for loop to i>=0.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define SIZE 80

void get_input(char *line){
 fgets(line, SIZE, stdin);
 char *ptr = strchr(line, '\n');
 if (ptr){
     *ptr = ' '; }
}

void reverse(char *line){
 char copy[SIZE];
 char word[SIZE];
 memset(copy, 0, SIZE);
 int line_len = strlen(line);
 int word_len = 0;
 int i;
 for(i=line_len; i>=0; --i){
     if(line[i] == ' ' && word_len > 0){
        memset(word, 0, SIZE);
        strncpy(word, line + i + 1, word_len);
        strcat(copy, word);
        strcat(copy, " ");
        word_len = 0;
     }else if(isalnum(line[i]) || line[i] == '\'')
          {word_len++;}
 }
  if(word_len>0){
     memset(word, 0, SIZE);
     strncpy(word, line, word_len);
     strcat(copy, word);}
     strcpy(line, copy);
}

int main(int argc, char *argv[]){
 int N = (int)strtol(argv[1], NULL, 10);
 if(N<0){
         printf("ERROR: Please provide an integer greater than or equal to 0\n");
         return 0;
        }
 if(N>SIZE){ printf("ERROR: Please provide an integer less than or equal to 80\n");
             return 0;
           }
     char line[SIZE];
     int i;
     for(i=0;i<N;i++){
                      get_input(line);
                      reverse(line);
                      printf("%s\n", line);
                     }
 return 0;
}

Comments

0

Very good attempt at the reversal. However, you may be making it a bit more difficult than it need be. If you are simply reversing the order of the arguments, you can either print them out in reverse, or if you want to build a string out of the reversed arguments, simply concatenate them in reverse order with strcat. There are a lot of ways to do it, but a fairly straight forward approach is:

#include <stdio.h>
#include <string.h>

#define MAXC 1024

int main (int argc, char **argv) {

    char string[MAXC] = "";
    int i, len = 0;

    for (i = argc - 1; i && len + strlen (argv[i]) + 2 < MAXC; i--) {
        strcat (string, argv[i]);
        strcat (string, " ");
        len = strlen (string);
    }
    printf ("%s\n", string);

    return 0;
}

Which simply starts with the last argument index argc - 1 and then so long as the argument index is greater than 0 (i) and you are within your allowed number of characters len + strlen (argv[i]) + 2 < MAXC (+2 to allow for the nul-terminator and the ' '), concatenate the argument to your string strcat (string, argv[i]); and add a space strcat (string, " ");

(you can add if (i > 1) before strcat (string, " "); to eliminate the trailing space).

Example Use/Output

$ ./bin/str_revargs The fox jumped over a log
log a over jumped fox The

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.