LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-07-2003, 09:28 PM   #1
centr0
Member
 
Registered: Feb 2003
Location: Earth
Distribution: Slackware 9.1
Posts: 134

Rep: Reputation: 15
Unhappy programming logic || scared


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. =\
 
Old 07-07-2003, 09:40 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Just keep doing it :)

It's like getting familiar with a foreign
language ;) ... you'll get the hang of it.

Cheers,
Tink
 
Old 07-08-2003, 01:16 PM   #3
centr0
Member
 
Registered: Feb 2003
Location: Earth
Distribution: Slackware 9.1
Posts: 134

Original Poster
Rep: Reputation: 15
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. =)
 
Old 07-08-2003, 01:35 PM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
i would think about it like this, you want to copy every charcter thats not a space preceeded by a space. hope that helps.
 
Old 07-08-2003, 01:35 PM   #5
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
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?
 
Old 07-08-2003, 06:00 PM   #6
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
Practice, practice and more practice!
 
Old 07-09-2003, 12:01 AM   #7
centr0
Member
 
Registered: Feb 2003
Location: Earth
Distribution: Slackware 9.1
Posts: 134

Original Poster
Rep: Reputation: 15
Quote:
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?
 
Old 07-09-2003, 01:26 AM   #8
shellcode
Member
 
Registered: May 2003
Location: Beverly Hills
Distribution: Slackware, Gentoo
Posts: 350

Rep: Reputation: 32
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...
 
Old 07-09-2003, 04:17 AM   #9
centr0
Member
 
Registered: Feb 2003
Location: Earth
Distribution: Slackware 9.1
Posts: 134

Original Poster
Rep: Reputation: 15
Quote:
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.

Last edited by centr0; 07-09-2003 at 04:19 AM.
 
Old 07-09-2003, 03:47 PM   #10
shellcode
Member
 
Registered: May 2003
Location: Beverly Hills
Distribution: Slackware, Gentoo
Posts: 350

Rep: Reputation: 32
the first chapter is the fastest (if you know no C) since it tries to introduce you to C quickly.

The K&R book is the best but its not really meant for beginners that much......

you only need:

while ((c = getchar()) != EOF)

but youll want another variable to take care of the first getchar()

this is also a good site for C questions:
www.cprogramming.com


have fun! once you learn it you wont regret you spent your time..

Last edited by shellcode; 07-09-2003 at 03:50 PM.
 
Old 07-09-2003, 09:06 PM   #11
centr0
Member
 
Registered: Feb 2003
Location: Earth
Distribution: Slackware 9.1
Posts: 134

Original Poster
Rep: Reputation: 15
Code:
main()
{
   int c,d;

   while((c = getchar()) != EOF)
   {
       d = getchar();
       if(d == ' ')
               putchar(c);
       else{
               putchar(c);
               putchar(d);
       }
    }
}
with the help of all who have responded i figured how to get 2 spaces and replace it with one. i have pasted the code here to see what you guys think.

is there a better way to do this?

how can i replace more than 2 spaces with a single space? or is this asking too much since im a beginner? =)
 
Old 07-09-2003, 09:39 PM   #12
shellcode
Member
 
Registered: May 2003
Location: Beverly Hills
Distribution: Slackware, Gentoo
Posts: 350

Rep: Reputation: 32
Quote:
Originally posted by centr0

is there a better way to do this?
yes there is...consider a loop which places the result of getchar() in a variable as the last thing it does

Quote:
this asking too much since im a beginner? =)
nah, keep trying.....

have fun!
 
Old 07-10-2003, 04:22 AM   #13
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
partitioning - SCARED!!! marta Linux - Newbie 6 06-05-2004 08:36 AM
Scared of Slack? Vector is there for you. Megamieuwsel Linux - Distributions 2 10-10-2003 01:18 AM
i'm scared......lol hatchetman Linux - Newbie 8 12-02-2002 09:42 PM
scared GT I.N.C Linux - Newbie 6 07-05-2002 09:34 AM
M$ getting scared acid_kewpie General 42 01-17-2002 10:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration