By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,948 Members | 1,929 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,948 IT Pros & Developers. It's quick & easy.

search for occurance of character in certain position in a string

P: n/a
How can I search for occurance of a character in certain position of a
string
I checked function strchr, but doesn't option to specify position.

Thanks.

Regards,
Magix
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"magix" <ma***@asia.comwrites:
How can I search for occurance of a character in certain position of a
string
I checked function strchr, but doesn't option to specify position.
If you're checking a certain position, it's not really a search, is it?

It sounds like you're looking for something that will tell you, for
example, whether or not the character 'C' occurs in the third position
(position 2) in the string "ABCDE", regardless of whether it also
occurs anywhere else. If so, all you need is a single comparison. If
not, please clarify what you're asking for.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #2

P: n/a
"magix" <ma...@asia.comwrote:
How can I search for occurance of a character
in certain position of a string
if (strlen(s) n && s[n] == ch)
/* yes, char ch at position n in s */

--
Peter
Jun 27 '08 #3

P: n/a
On May 20, 7:29*pm, "magix" <ma...@asia.comwrote:
How can I search for occurance of a character in certain position of a
string
I checked function strchr, but doesn't option to specify position.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

long location(char *string, int ch)
{
char *where = strchr(string, ch);
long index = -1;
if (where) {
index = where - string;
}
return index;
}
int main(void)
{
char string[] = "Hello, my name is skinner the
grinner! What's yours?";
long loc;
loc = location(string, '!');
if (loc >= 0) {
printf("position is %ld\n", loc);
putchar(string[loc]);
} else {
puts("Not found.");
}
return 0;
}
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.