What is the best way to check any ASCII characters between 33 to 47
exists in a char*?
For maximal portability use the ispunct() function. Otherwise use
something like:
if (ch >= 33 && ch <= 47) /* ... */
This is of course specific to ASCII encoding. Using ispunct may be
better but that might return true for punctuation other than those
within ASCII 33 to 47, in which case you have perform further checks.
Another possibility is a switch statement:
switch (ch) {
case '!':
case '"':
case '#':
/* and so on */
default:
/* not matching */
}
No, this is fine only, if I search a single character, but mine is 256
character string, in that I have to find any occurance of the Ascii
characters btw 34 to 47.
No, this is fine only, if I search a single character, but mine is 256
character string, in that I have to find any occurance of the Ascii
characters btw 34 to 47.
No, this is fine only, if I search a single character, but mine is 256
character string, in that I have to find any occurance of the Ascii
characters btw 34 to 47.
Insufficiently specified... Do you need simply to determine whether
at least one character in the range is present, or identify all
instances of characters in that range?
Either way, it's hardly a challenging task, and as far as I can see,
there's little benefit in trying to use any standard functions.
Insufficiently specified... Do you need simply to determine whether
at least one character in the range is present, or identify all
instances of characters in that range?
at least one character in the range is present...
I feel Santosh solution would be fine... but if the string goes very
lengthy, let us say more than 1000 character, then loop will go for
1000 time. So performance vice it will be slow?
>Insufficient ly specified... Do you need simply to determine whether
>at least one character in the range is present, or identify all
>instances of characters in that range?
>
at least one character in the range is present...
>
I feel Santosh solution would be fine... but if the string goes very
lengthy, let us say more than 1000 character, then loop will go for
1000 time. So performance vice it will be slow?
Given a string there is no way other than a linear search for this
problem. I think you'll find performance to be acceptable unless the
strings are *really* large, like say dozens of megabytes. If your
string *is* really that large then it may indicate that you may need to
rethink your data structures to avoid this problem in the first place.
Also I should point out that the solution I presented involving a switch
statement does not meet your stated requirements.
I feel Santosh solution would be fine... but if the string goes very
lengthy, let us say more than 1000 character, then loop will go for
1000 time. So performance vice it will be slow?
Probably. The search could also be done using strchr(), which
/might/ result in a faster search depending on compiler
optimization and/or machine architecture - on the other hand a
really smart compiler might be able to arrive at those same
optimizations by examining the loop code...
>Insufficient ly specified... Do you need simply to determine whether
>at least one character in the range is present, or identify all
>instances of characters in that range?
>
at least one character in the range is present...
>
I feel Santosh solution would be fine... but if the string goes very
lengthy, let us say more than 1000 character, then loop will go for
1000 time. So performance vice it will be slow?
There is no alternative to a linear search in this context. One way or
another, each character in the array will need to be examined until you
either find one of your candidates or you reach the end.
As Morris suggests, it's possible that in some contexts the str***()
functions may have been implemented using some neat platform-specific
functionality that will make them quicker than a hand-coded loop, but
generally, I'd suggest that you just take the simple approach.
>I feel Santosh solution would be fine... but if the string goes very
>lengthy, let us say more than 1000 character, then loop will go for
>1000 time. So performance vice it will be slow?
>
Probably. The search could also be done using strchr(), which
/might/ result in a faster search depending on compiler
optimization and/or machine architecture - on the other hand a
really smart compiler might be able to arrive at those same
optimizations by examining the loop code...
Actually strcspn() or strpbrk() would be more suitable for OP's stated
requirements except for the fact that he wants to restrict the search
to ASCII characters from 33 to 47. A manual loop seems the best
solution to me.
Comment