ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
im just learning to program in C. when i read the book that i have i can understand the code and what it does. but if you ask me to create a program similar to the one in the book i wouldnt know where to start.
i guess my question is. is there any sites for programming logic in C? or does this stuff just flow right in once i get comfortable with the language? its kind of scary. =\
im trying to follow an exercise in a book i have. it asks to copy input to output and replace each string of one or more blanks with a single blank. now i know i need to test for blank spaces. but how would i go about testing for more blank spaces? im not asking for the answer. just a kick in the right direction. =)
Originally posted by TheLinuxDuck So, it's looking for a string that may or may not contain blank spaces as well as other text, or will the strings contain only blank spaces?
it will contain characters and spaces. im trying to replace the one or more spaces with a single space only without neglecting the characters that come after the multiple space.
Quote:
Originally posted by kev82 i would think about it like this, you want to copy every charcter thats not a space preceeded by a space. hope that helps.
i wrote something that neglects the spaces completely but i dont know how to putchar() something right after a space?
hint:
a loop
stores the character that getchar() got before in a variable
then compare the variable
ie.
if the last char whas a space then print nothing....
what book?
this sounds like an execrise from K&R...
Originally posted by shellcode what book?
this sounds like an execrise from K&R...
it is k&r exercise1-9. =)
i declared 2 ints c,d and started loop:
while((c = d = getchar()) != EOF)
and tried various if statement to compare but i always get errors when i try to compile.
[edit] and by reading the book. it hasnt even covered if statements so it must be possible to accomplish this exercise without if statements right?[/edit]
i know i need to compare c and d for spaces and if c is a space and d is a space, then neglect d. but then dont i need to test if the next c is a space and neglect that? am i on the right track or am i way off?
given the while statement i entered here wont my string look like this regarding variables.
cdcdcdcdcdc ...etc?
man i hope i get the hang of this logic stuff. its the first chapter and it takes this long to complete one exercise lol. thanks in advance for your help.
how about storing the current character from getchar() in one variable and having another variable that stores the character from the previous iteration then use the values of these characters to determine if you should display the current one.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.