473,473 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

convert tabs to spaces

11 New Member
my assignment is to convert tabs to spaces (8) in C. we are supposed to use getchar() and not expand(). can someone get me started? it should be REALLY simple so i can grasp it!
Thanks
jp
Sep 8 '06 #1
4 5684
Banfa
9,065 Recognized Expert Moderator Expert
you will need to provide more detail because what you have given is not enough. Also since this is a course work assignment our general rule is that you have to post an attempt at solving the problem first and then we will help you with any problems you are having with it.
Sep 8 '06 #2
jmprince01
11 New Member
the assignment is as follows: write a c program that will read from stdin (using getchar()) and replace tabs by the number of blanks that will achieve the same spacing. (8 spaces in a tab)


here is my code, what i came up with:

#include <stdio.h>

int main(void)
{
char ch, tab;
int c;

while((c=getchar())!=EOF){
tab=" "; //tab = 8 spaces
puts(tab);
} //end of while loop

printf("\ta\n"); //wherever there is a \t, it should print 8spaces
printf("\t\ta\n");


return (0);
} // end of main
Sep 8 '06 #3
jmprince01
11 New Member
i am posting again hopefully some one can help?

the assignment is as follows: write a c program that will read from stdin (using getchar()) and replace tabs by the number of blanks that will achieve the same spacing. (8 spaces in a tab)


here is my code, what i came up with:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. char ch, tab;
  6. int c;
  7.  
  8. while((c=getchar())!=EOF){
  9. tab=" "; //tab = 8 spaces
  10. puts(tab);
  11. } //end of while loop
  12.  
  13. printf("\ta\n"); //wherever there is a \t, it should print 8spaces
  14. printf("\t\ta\n");
  15.  
  16.  
  17. return (0);
  18. } // end of main
  19.  
Sep 10 '06 #4
Banfa
9,065 Recognized Expert Moderator Expert
Sorry about the delay the email notification of posts on this site is not currently working correctly.

OK a couple of tips

puts adds a newline so is going to mess up the output by adding newlines where they are not required.

your code will not change the output of the printf statements, if you explicitly write a tab then a tab is what you will get.

I think you will find that the easiest way to solve this problem is to consider what should be output for a given input

Assume that the input is

Hello<tab>World !

Your program needs to read and output the 5 leters of Hello. The next character it reads will be a tab character, tab stops occur at every 8 characters, since we have already output 5 characters (of hello) we need to output 3 spaces to take us up to the next tab stop before we carry on reading data.

Your tab variable is miss used, it is of type char but you try to assign a string to it (which you couldn't do in C/C++ even if it was of type array of char) andpass it to a function expecting const char * (pointer to an array of characters containing azero terminate string).
Sep 11 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Kevin Auch | last post by:
Hi, I'm working on a little application which analyse source code file from VS.NET. But I'm in front of a big problem. The source generated by VS.NET is automatically "indented", but when I...
3
by: ReidarT | last post by:
I have accidentally enabled spaces and tabs and ... in code window. How do I disable this? reidarT
135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
2
by: Laurence | last post by:
In VS.2005, using VB.NET My editor is behaving strangely. If I press the tab key, the line moves over the appropriate number of spaces. When I click on another line, the line that I had tab'd...
15
by: Tom Plunket | last post by:
The documentation for dedent says, "Remove any whitespace than can be uniformly removed from the left of every line in `text`", yet I'm finding that it's also modifying the '\t' characters, which...
35
by: Ben | last post by:
Hi, I have a python script on a unix system that runs fine. I have a python script on a windows system that runs fine. Both use tabs to indent sections of the code. I now want to run them on the...
16
by: Alan Isaac | last post by:
I am brand new to pylint. As a tab user, I want the tabs warning turned off. How? Larger question: where is the config file format documented? Thanks, Alan Isaac
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.