Thread: Need help with section of Code

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    2

    Smile Need help with section of Code

    I need a piece of code that takes characters from a keyboard and filters out any blanks e.g. spaces. Any help would be greatly appreciated.

    Thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This isn't a difficult problem. Try it yourself first. We generally refuse to help people who don't make an honest attempt to do their own work.
    My best code is written with the delete key.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Do you want it done dynamically, or first you enter the string, then you delete the spaces.

    If it's the first, include conio.h, and use getch(), use conditions resulting from the output of getch() to state whether or not it is a space.

    If it's the second, you don't need to include the non-standard header. You can getline( cin, stringname ); (include <string>), shuffle through the characters of the string, and either assign the non space characters to a new string, or you can use the erase position in the initial string containing a space.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    2

    Progress

    My progress so far any help?
    Code:
    /*
     *	  AUTHOR:	<Anthony C Harkin, B31PY4, 23/8/2006>
     * 	  DESCRIPTION: 	<Dynamically Takes spaces from any locating>
     *	  INPUT: 		<Keyboard Characters>
     *	  OUTPUT: 	 <Characters not including spaces>
     */
    #include <conio.h>
    #include <stdio.h>
    int main()
    
    {
        float text;
        printf("Enter Required Txt: ");
        scanf("%f",&text);
        printf("You have entered the following text that has yet to be filtered: %f\n",text);
        return (0);
    }

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Well, first and foremost, use code tags. Next take out the non-standard <conio.h> which isn't even being use, third try using a datatype that can even have a space, fourth use a scanning method that can even scan a space into a single variable, fifth post some code that's relevant to the actual question you asked in your first post, and last post it in the correct forum because that looks more like C than C++.
    Sent from my iPadŽ

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Agreed - moved to the C board.

    To Harkin1987
    Lookup the fgets() function (numerous examples in the FAQ and in other posts) for reading in a line of text.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Here it is:
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)


    You might also want to use the isspace() function in <ctype.h> to help filter out spaces.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    11
    Code:
    /*
     *	  AUTHOR:	<Anthony C Harkin, B31PY4, 23/8/2006>
      //** DUDE IS THAT YOUR STUDENT NUMBER - YOU SHOULDN'T POST THAT ON THE NET! **
     * 	  DESCRIPTION: 	<Dynamically Takes spaces from any locating>
     *	  INPUT: 		<Keyboard Characters>
     *	  OUTPUT: 	 <Characters not including spaces>
     */
    #include <conio.h>
    /* look up a book on the basic structure of a piece of C code - they usually start with 
         #include <stdio.h > and
         #include <stdlib.h> 
    why do you want to use conia? 
    */
    #include <stdio.h>
    int main()
    
    {
        float text; /*learn your data types a float is a floating point number  - you'd most likely store text as a string or a character - look 
                      that up. If you store text as a number you're essentially storing the ASCII value associated with it. */
        printf("Enter Required Txt: ");
        scanf("%f",&text); 
        printf("You have entered the following text that has yet to be filtered: %f\n",text);
    /*Do you intend to return numbers? You have your theory down that a %f 
    will display the float value your sending to the printf statement*/
        return (0);
    }

    I need a piece of code that takes characters from a keyboard and filters out any
    blanks e.g. spaces. Any help would be greatly appreciated.
    Look up the use of an ASCII table that'll help you seperated all the alphanumeric characters (ie numbers and letters) from all the other symbols. If you specifically want to remove a certain character you could have a loop go through the each character entry and then identify the character you want replaced or removed.

    look up the the caesar cipher which rotates characters to encode letters - the theory for identifying characters is found here. Its worth a read.

    http://en.wikipedia.org/wiki/Caesar_cipher
    Last edited by Ken Fitlike; 08-26-2006 at 03:16 AM. Reason: reduced horizontal extent of code

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    11
    I see what you're trying to do there - are you trying to identify the certain ascii code related to your search for identfying the characters you want to remove.

    Heres more help -

    try doing the followin

    Code:
      .
      .
      char c
    
      while((c = getchar()) != EOF)
      {
      
        if(c == ' ') 
        {
           /*
           the ' ' essentially identifies a  character which is identified              
           by two ellipses. so the character a would look like 'a'. So here we're trying to          
           isolate the space character ' '
           */
    
           //add whatever
           // if you want to replace the space with something else or ...
    
    
       }
     
       if(c != ' ')
       {
          printf("%c", c); // would print everything other than spaces .
       }
    
    }

    good luck
    Last edited by trickae2; 08-25-2006 at 08:58 PM.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You mean
    Code:
    int c;
    right? EOF is an int value, as you can discover by reading the FAQ.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. How would you go back to a section of code?
    By XenoForce in forum C++ Programming
    Replies: 23
    Last Post: 10-20-2004, 10:51 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM