473,809 Members | 2,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2526
"magix" <ma***@asia.com writes:
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_Keit h) 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.com wrote:
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.com wrote:
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("positio n 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
12635
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 character 2 and replace it with a different character.... etc.... until completing the string? thanks....
7
10046
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 the number of occurance of "," in text ???? return 0;
7
2614
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 has something to do with the strequal() function... Btw, thanks to all of you who answered last time! code: ------------------------------------------------------------------------- #include <stdio.h>
32
14918
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 ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
6
1883
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
2139
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 error if the length is less than the position of the character you want. Ex. Dim x As String = 'ABCD'
11
16261
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 occurance index. Can anyone advise on this? Thanks, Simon
5
2496
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 was found was displayed with about 2 lines before and 2 lines behind the search word itself. Let us say you look for "peanut butter" an the word is found in a larger text about sandwiches, even when it is on the 40th line of the text you would get...
2
1492
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 am looking for a way to know how many characters the string has from 0 to the 30th occurance of "\n" within the main string I have considered a string.split or indexOf but can't wrap my head around how to determine the character location of the...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10375
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.