1

So, I am trying to remove a newline that is the last char in an array. The issue is, that in earlier lines I need the newline break, so when I create the array, I leave it in there, so it looks like this:

fgets(input, 100, stdin);

char *array[10];
int i = 0;

array[i] = strtok(input, " ");

while(array[i] != NULL)
    array[++i] = strtok(NULL, " ");

but later on, when I run

execvp("echo", array);

It has an extra line break that I can't get rid of, like this (what it would look like from console):

(input) echo "hello"
(output) hello
(output)
*Continues code from here*

I am just wondering if anyone has any ideas on how I could remove the linebreak so, that when I run the execvp, there would not be an extra line?

Oka
  • 23,367
  • 6
  • 42
  • 53
bobChicken
  • 11
  • 1

2 Answers2

0

This is the code snippet I use to remove the newline (before strtok).

fgets(in_line, LINE_LENGTH, stdin);
if ((strlen(in_line) > 0) && (in_line[strlen(in_line) -1] == '\n') )
    in_line[strlen(in_line)-1] = '\0';
J27avier
  • 121
  • 4
0

To remove a newline that is the last char in an array

input[100-1] = 0;

To remove a newline at the end of a string fill by fgets()

input[strcspn(input, "\n")] = 0;
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256