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

Edited 4 Years Ago by swissknife007: n/a

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.

Edited 4 Years Ago by gerard4143: n/a

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.

Edited 4 Years Ago by mhd_arif123: n/a

This article has been dead for over six months. Start a new discussion instead.