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
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
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.
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.
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); }
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Ž
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.
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.
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); }
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.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 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
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.
You mean
right? EOF is an int value, as you can discover by reading the FAQ.Code:int c;
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.