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
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.
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
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: -
#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
-
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).
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
by: ReidarT |
last post by:
I have accidentally enabled spaces and tabs and ... in code window.
How do I disable this?
reidarT
|
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...
|
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...
|
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...
| |
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...
|
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
|
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...
|
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,...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |