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 359213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:22:21+00:00 2026-05-12T12:22:21+00:00

I need to expand tabs in an input line, so that they are spaces

  • 0

I need to expand tabs in an input line, so that they are spaces (with a width of 8 columns). I tried it with a previous code I had replacing the last space in every line greater than 10 characters with a ‘\n’ to make a new line. Is there an way in C to make tabs 8 spaces in order to expand them? I mean I am sure it is simple, I just can’t seem to get it.

Here’s my code:

int v = 0;
int w = 0;
int tab;
extern char line[];

while (v < length) {

   if(line[v] == '\t')
      tab = v;

   if (w == MAXCHARS) {
      // THIS IS WHERE I GET STUCK
      line[tab] = ' ';
      // set y to 0, so loop starts over
      w = 0;
   }
   ++v;
   ++w;
}
  • 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-05-12T12:22:21+00:00Added an answer on May 12, 2026 at 12:22 pm

    This isn’t really a question about the C language; it’s a question about finding the right algorithm — you could use that algorithm in any language.

    Anyhow, you can’t do this at all without reallocating line[] to point at a larger buffer (unless it’s a large fixed length, in which case you need to be worried about overflows); as you’re expanding the tabs, you need more memory to store the new, larger lines, so character replacement such as you’re trying to do simply won’t work.

    My suggestion: Rather than trying to operate in place (or trying to operate in memory, even) I would suggest writing this as a filter — reading from stdin and writing to stdout one character at a time; that way you don’t need to worry about memory allocation or deallocation or the changing length of line[].

    If the context this code is being used in requires it to operate in memory, consider implementing an API similar to realloc(), wherein you return a new pointer; if you don’t need to change the length of the string being handled you can simply keep the original region of memory, but if you do need to resize it, the option is available.

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

Sidebar

Related Questions

I need a div to expand to the whole page width of the HTML
I need a macro to expand to a c++ comment, is that possible? I've
Hi this may be unusual, but I need to expand this code to a
I need to expand the expandedlistview only on click on a button in that
I need to expand an error message. I am running a Cucumber test on
im using expanded listview in my project, i need to expand the listview only
I need to be able to programatically expand users' search expressions such as: X
I need to add some accordian style expand/collapse handles to a series of parent
Need some regular expressions help. So far I have my code working to allow
Hay i need to hand implemeneting a voting system into a model. I've had

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.