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

Recommended Answers

All 8 Replies

Depends, what is the criteria for discarding characters.

Depends, what is the criteria for discarding characters.

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

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.

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.

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

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.

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;
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.