954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

I want to store only some of the input

I am implementing a program where I want to store only certain part of the input string.
for eg if the input string is
0123456789
I want to discard 4 AND 7
and store
01235689
Please my program requires an input string of 10000 characters so running time is very crucial for me.
Also while inputting the string I really don't know how many characters will be there.
Does anybody have any suggestions?
Any help will be greatly appreciated

swissknife007
Junior Poster in Training
74 posts since Oct 2008
Reputation Points: 13
Solved Threads: 0
 

Depends, what is the criteria for discarding characters.

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 
Depends, what is the criteria for discarding characters.


I already told you the criteria.
I dont want 4s and 7s in my answer

swissknife007
Junior Poster in Training
74 posts since Oct 2008
Reputation Points: 13
Solved Threads: 0
 
I already told you the criteria. I dont want 4s and 7s in my answer


I mean I don't want to store 4s and 7s present in the input.

swissknife007
Junior Poster in Training
74 posts since Oct 2008
Reputation Points: 13
Solved Threads: 0
 

I would investigate a function like

void *memchr(const void *s, int c, size_t n);

The memchr() function scans the first n bytes of the memory area
pointed to by s for the character c. The first byte to match c
(interpreted as an unsigned character) stops the operation.

The memchr() and memrchr() functions return a pointer to the match‐
ing byte or NULL if the character does not occur in the given mem‐
ory area.

Copied from the manpages on GNU/Linux.

With this function you can leap through valid areas of data to store.

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

I would investigate a function like

void *memchr(const void *s, int c, size_t n);

The memchr() function scans the first n bytes of the memory area pointed to by s for the character c. The first byte to match c (interpreted as an unsigned character) stops the operation.

The memchr() and memrchr() functions return a pointer to the match‐ ing byte or NULL if the character does not occur in the given mem‐ ory area.

Copied from the manpages on GNU/Linux.

With this function you can leap through valid areas of data to store.


Sorry but you didnt understand my question.i dont want to stop reading once i encounter 4 or 7.I want to continue reading the rest of the string

swissknife007
Junior Poster in Training
74 posts since Oct 2008
Reputation Points: 13
Solved Threads: 0
 
Sorry but you didnt understand my question.i dont want to stop reading once i encounter 4 or 7.I want to continue reading the rest of the string

Yes I understood your question. You use the function memchr to find the occurrence of characters(or pointers to the occurrences) to find ranges(pointer ranges) of valid data.

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

How are you handling the input/output? Given that you can read from stdin and write to stdout why not just filter the input?

int main () {
   int c = 0;
   while (EOF != (c = fgetc (stdin))) {
      switch (c) {
         case '4': case '7': break;
         default: fputc (c, stdout);
      }
   }
   return 0;
}
L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
 
I am implementing a program where I want to store only certain part of the input string. for eg if the input string is 0123456789 I want to discard 4 AND 7 and store 01235689 Please my program requires an input string of 10000 characters so running time is very crucial for me. Also while inputting the string I really don't know how many characters will be there. Does anybody have any suggestions? Any help will be greatly appreciated


Step 1 : you have to store those characters (that you want to discard) in a data structure
Step 2 : extract a single character from the input string and match it with the list of discardable characters.
Step 3 : if match is not found , copy the extracted character to a new place(datastructure). else leave it.
continue above steps , till the end of input string.

mhd_arif123
Newbie Poster
7 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: