473,326 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Searching in char*

spl
What is the best way to check any ASCII characters between 33 to 47
exists in a char*?
Mar 18 '08 #1
13 1292
spl wrote:
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 */
}

Mar 18 '08 #2
santosh wrote:
spl wrote:
>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) /* ... */
Oops. Didn't catch the char * bit. Use something like:

bool isASCIIpunct(const char *restrict str) {
bool rc = false;
while (*str++) {
if (*str >= 33 && *str <= 47) {
rc = true;
break;
}
}
return rc;
}

<snip>

Mar 18 '08 #3
spl
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.
Mar 18 '08 #4
spl wrote:
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.
Use a loop. See my other post.

Mar 18 '08 #5
spl wrote:
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.
Mar 18 '08 #6
spl
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?
Mar 18 '08 #7
spl wrote:
>
>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?
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.

Mar 18 '08 #8
spl wrote:
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...

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Mar 18 '08 #9
spl wrote:
>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?
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.
Mar 18 '08 #10
Morris Dovey wrote:
spl wrote:
>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.

Mar 18 '08 #11
santosh wrote:
A manual loop seems the best solution to me.
Ok. :-)

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Mar 18 '08 #12
On Mar 18, 10:08 am, spl <splender....@gmail.comwrote:
What is the best way to check any ASCII characters between 33 to 47
exists in a char*?
#include <string.h>

int checkFor33to47(const char *str)
{
char set[] = {33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 0};

char *result = strpbrk(str, set);
return result != NULL;
}
Mar 18 '08 #13
#include <string.h>
>
int checkFor33to47(const char *str)
{
char set[] = {33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 0};

char *result = strpbrk(str, set);
return result != NULL;
}
Maybe faster is to use a Look-Up-Table:

....
char const LUT_33TO47 [] =
{
0 /* 0*/,
0 /* 1*/,
0 /* 2*/,
...
0 /* 32*/,
1 /* 33*/,
1 /* 34*/,
...
1 /* 46*/,
1 /* 47*/,
0 /* 48*/,
...
0 /*255*/
};
....
if (LUT_33TO47[(unsigned int)c])
printf("Ok, character %d is between 33 and 47\n", c);
else
printf("No, character %d is _NOT_ between 33 and 47\n", c);
....

Regards

--
MaciekL
Mar 19 '08 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Richard Berg | last post by:
Hello, I need to search a byte array for a sequence of bytes. The sequence may include wildcards. For example if the array contains 0xAA, 0xBB, 0xAA, OxDD then I want to be able to search for...
2
by: yee young han | last post by:
I need a fast data structure and algorithm like below condition. (1) this data structure contain only 10,000 data entry. (2) data structure's one entry is like below typedef struct _DataEntry_...
14
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I took a C course some time ago, but I'm only now beginning to use it, for a personal pet project. My current stumbling-block is finding an efficient way to find a match between the beginning of a...
9
by: C3 | last post by:
I have to process some data in C that is given to me as a char * array. I have a fairly large number of substrings (well, they're not actually printable, but let's treat them as strings) that I...
35
by: Cor | last post by:
Hallo, I have promised Jay B yesterday to do some tests. The subject was a string evaluation that Jon had send in. Jay B was in doubt what was better because there was a discussion in the C#...
2
by: neilio2j | last post by:
hi, can anyone tell me how to search a given directory using C++? so far i have been successful in searching a file named, for tags"h:\\tags.cpp", whereas i would like to search for all files...
1
by: mitola | last post by:
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <conio.h> struct oseba //struktura osebe , ki jo kasneje uporabljamo v datoteki { char ime; char priimek;
9
by: shoes2908 | last post by:
Hi, again I am in a rush and can't seem to remember how to search for a certain string in a string vector... heres my code: cin >> worker_num; if (cin.fail()) {...
15
by: DavidW | last post by:
Hello, Is the function below the simplest way to produce an iterator to the next non-space in a string? (Or the upper-bound iterator if none is found). Searching for a sequence is overkill and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.