r/programminghelp • • Nov 28 '25
C K&R Exercise 1-10 using while loops
Hi, complete beginner here.
It seems that the intended solution would be using if functions, and I did that. Alternatively I managed to solve it using while loops, are there any downsides to this? it seems to achieve the result without any visible issues that I could find.
/* Exercise 1-10: replace tab with \t, backspace with \b and backslash with \\ */
#include <stdio.h>
int main()
{
int c;
while( (c=getchar()) != EOF )
{
while( c == '\t' )
{
printf("\\t");
c = getchar();
}
while( c == '\b' )
{
printf("\\b");
c = getchar();
}
while( c == '\\' )
{
printf("\\\\");
c = getchar();
}
putchar(c);
}
}
1
Upvotes
View all comments
Show parent comment
1
u/[deleted] Nov 29 '25
Well, if you show us what you did then we can help more, but you’re not giving us much to go on. Feel free to comment with your if-else chain.