473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

C program that converts tabs to spaces

10
I've been working on this little program that will convert a tab into spaces. I have it set up so it can find the tab, but i am having trouble with converting it. Any help would be great!

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h> 
  3.  
  4. int main() 
  5. int c, d; 
  6.  
  7. while((c=getchar()!= EOF){ 
  8. d=0; 
  9.  
  10. if(c=='t'){ 
  11. putchar('\n'); //I did this to see if it will read the tab 
  12. d=1 
  13.  
  14. if(d==0) 
  15. putchar(c); 
  16. return 0; 
  17.  
  18.  
  19.  
  20.  
  21.  
Sep 7 '07 #1
5 2934
sicarie
4,677 Expert Mod 4TB
I've been working on this little program that will convert a tab into spaces. I have it set up so it can find the tab, but i am having trouble with converting it. Any help would be great!

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h> 
  3.  
  4. int main() 
  5. int c, d; 
  6.  
  7. while((c=getchar()!= EOF){ 
  8. d=0; 
  9.  
  10. if(c=='t'){ 
  11. putchar('\n'); //I did this to see if it will read the tab 
  12. d=1 
  13.  
  14. if(d==0) 
  15. putchar(c); 
  16. return 0; 
  17.  
  18.  
  19.  
  20.  
  21.  
Have you tried 'if (t=='\t')'?
Sep 7 '07 #2
JosAH
11,448 Expert 8TB
Have you tried 'if (t=='\t')'?
Or "if(c == '\t')" for that matter ;-) The op should first do the simple math: suppose
before reading a character, m characters have been output already. Suppose n
is the size of a tab(stop). If the current character is not a tab, m is simply incremented
by 1 and the character is output; otherwise (a tab was read) so n-m%n
spaces have to be output and m must be incremented by that same number.

kind regards,

Jos
Sep 7 '07 #3
sicarie
4,677 Expert Mod 4TB
Or "if(c == '\t')" for that matter ;-)
Argh.... wow, that was careless of me. Thanks, Jos.
Sep 7 '07 #4
Will2k
10
I'm not sure what "if(c == '\t')" would do.

Also I understand what you were saying about the math. have the tab character (which is 8 spaces) minus the number of characters already counted.

So if I have something like

char t='\t';

how would I be able to make t-c=f?
Sep 7 '07 #5
JosAH
11,448 Expert 8TB
I'm not sure what "if(c == '\t')" would do.
Erm, it tests whether or not character c is tab?

Also I understand what you were saying about the math. have the tab character (which is 8 spaces) minus the number of characters already counted.

So if I have something like

char t='\t';

how would I be able to make t-c=f?
What is 'c'? What is 'f'? All you have to do is keep track of a current position 'm'
and a tabstop size 'n'; for more details read my previous reply.

kind regards,

Jos
Sep 7 '07 #6

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

Similar topics

17
by: bearophileHUGS | last post by:
Hello, I know this topic was discussed a *lot* in the past, sorry if it bores you... >From the Daily Python-URL I've seen this interesting Floating Point Benchmark:...
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...
8
by: someone | last post by:
I tried to make a program that's called "Don't Shout", that reads from a file "input.dat", and all letters should be lower-case, expect for the first letter, and any letter after a period should be...
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...
0
by: 14Dallas | last post by:
Hi, I have been working with another programmer to write this code. I haven't written code in years - DBase III and Pascal - anyways, back to my code question. The program opens a file and...
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
7
by: montyphyton | last post by:
Some recent posts about Python programming style got me thinking. Since we have the PEP 8 which gives some guidelines about the style to be used, do we have any program that can check for...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.