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