2

How can I remove the '\n' from each string in this array?

I know that I can do something like this for a simple C-String, but I failed at using it in this case

cmd[strcspn(cmd, "\n")] = '\0';

I am also not sure if that would be the propper way or not.

The String will never contain any space or \n in the middle. They are also of a static length (6).

#include <stdlib.h>

unsigned char cmd[][6] = { 
    {"r123\n"},
    {"r999\n"},
    {"l092\n"},
    {"l420\n"}};

void main(void) {
    int i;

    for(i = 0; i < (sizeof(cmd) / sizeof(cmd[0])); i++) {
        printf("%s\n", cmd[i]);
    }   
}
4
  • Your solution is perfectly fine Commented May 24, 2012 at 8:43
  • 1
    You should provide more information to get more useful answer. Will the length of these strings be always 5 (6 with '\0') ? Do you want to remove '\n' also if it's in the middle of this string ? ... Commented May 24, 2012 at 8:46
  • I concur with LihO. If you want a general solution for the problem, the you should say so. On the other hand, if you want a solution that works for a very specific case (e.g. always 5 strings, always length 5, ...) then you should specify the details so we know what we can use. Commented May 24, 2012 at 9:02
  • 1
    Okay, I will do that next time, promised. I got the solution for this case already. The strings have static length and there won't be any whitespace or '\n' character in the middle of the string. Commented May 24, 2012 at 9:15

4 Answers 4

2

Just do it by hand, it's easy!

If it's guaranteed to be only the last char in every word, and it's guaranteed to be there, than like this:

for (i = 0; i < elem_number; ++i){
    cmd[i][strlen(cmd[i])-1] = 0;
}

If, on the other hand, you are unsure how many whitespace characters there will be at the end, but you know they will only be there at the end (there might be 0 in this case!) than this:

for (i = 0; i < elem_number; ++i){
    for (j = 0; cmd[i][j] != 0; ++j){
        if (isspace(cmd[i][j]))
             cmd[i][j] = 0;
     }
}

Voila!

If there will be whitespaces in the middle, then you have to define the desired behaviour: cut only the trailing whitespaces, cut the string in many little ones, or something completely different.

Oh, and one other sidenote: everyone else seems to be using char = '\0'. In C, '\0' and 0 are equivalent, i.e. if ('\0' == 0) { ... } evaluates to true.

Sidenote 2: I used elem_number because I did not know if the number of elements is a parameter or hardcoded / know in advance. Substitute with what is appropriate.

Sign up to request clarification or add additional context in comments.

2 Comments

Although '\0' and 0 are equal, you should use '\0' in this case. With '\0' you explicitly say "I'm using terminating NULL character here."
I think it's such common knowledge that it's equal, it has more of an individual style choice by now.
1

Setting a character in a char array to \0 will truncate the string at that character. So in your example setting the 5th character will do the job.

cmd[i][4] = '\0';

If the intended string can be less than 4 in length then don't hard-code to 4 but rather strlen(cmd[i])-1

1 Comment

that seems pretty fast forward :D
0

Maybe you can use strrchr? Use in a loop if the string may contain several linebreaks.

Comments

-1
    for(i = 0; i< sizeof(cmd)/sizeof(unsigned char[6]);i++)
        *strchr(cmd[i], '\n') = '\0';

5 Comments

this is a non-general solution with the string length hardcoded, without any written explanation for the exact special case it is intended for. Also, the Q author seems to be a little bit inexperienced, I don't think he would benefit much with a copy-paseable solution without an explanation.
This it shall be! Thank you very much!
unsigned char[6] ? The author didn't specify it that's just an example array to demonstrate with or if it will always be [6]. Oh, and, my mistake, array size, not str length
origin unsigned char cmd[][6]
I know where you get it from, it's just that I find general solutions working on a broad spectrum of example codes better than one targeted for a very specific, narrow problem. Spamming the comment section now.

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.