473,386 Members | 1,873 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,386 software developers and data experts.

search for occurance of character in certain position in a string

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
3 2495
"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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: MSUTech | last post by:
Hello, What is the best way to check each character within a string? For doing something like encryption, where you check character 1 and replace it with a different character.. then check...
7
by: news.hku.hk | last post by:
how to count the occurance of a character in a string ? e.g. #include <iostream> #include <string> using namespace std; int main (){ string text = "abc, def, ghi, jkl, mno"; // how to count...
7
by: spike | last post by:
Im writing a program to search for a string in a binary file. And it works. The problem is: It is sooo slow! how can i make it faster? It takes 27 seconds just to search a 5 meg file. I guess it...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
6
by: Soeren S. Joergensen | last post by:
Hi, I've got to byte arrays, and want to search first array for the position of the first occurance of the other. How is this best implemented ? Kr. Soren
6
by: KJ | last post by:
How can you look for a character in a certain position with VB.Net and if the string length is less than the position have it return and empty string instead of an error? Substring returnns an...
11
by: Simon Says | last post by:
Hi all, I've an arraylist A1 that contains {0,1,2,3,4,5,9,8,7} I've another arraylist A2 that contains {4,5,9} I would like to search whether if A2 is present in A1 and maybe return the first...
5
by: Martien van Wanrooij | last post by:
I have been using phpdig in some websites but now I stored a lot of larger texts into a mysql database. In the phpdig search engine, when you entered a search word, the page where the search word...
2
by: Maxington | last post by:
The problem I am left with is that I need to split a string into substrings and determine the character location of the 30th occurance of "\n" string. I have a string that has "\n" within it and I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.