Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7975685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:42:40+00:00 2026-06-04T08:42:40+00:00

How can I remove the ‘\n’ from each string in this array? I know

  • 0

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]);
    }   
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:42:41+00:00Added an answer on June 4, 2026 at 8:42 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know I can remove the extra stuff from each element individually like so
I want to know is there any way that I can remove duplicates from
How would I remove a view from this dialog? I know I can remove
How I can remove whitespace from a string, but only if the white space
I know I can remove the last element from a set: s.Remove(s.MaximumElement) But if
Is there a regex pattern that can remove .zip.ytu from the string below? werfds_tyer.abc.zip.ytu_20111223170226_20111222.20111222
Is there any way that you can remove IPs from a string? The input
In Django you can remove items from a QuerySet by doing this as documented
I see that I can remove all of the UIPageViewController gestures, but what if
Anyone has a regex that can remove the attributes from a body tag for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.