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

How to find a string in another string?

I'm sure you've seen this a million times but, I'm having some trouble with my code.


Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6.  
  7. main (void){
  8.  
  9. char* longString = " This is where a string with some shit goes; where else would it go?";
  10. char* shortString = " where";
  11. char* ptr;
  12. int i = 0;
  13.  
  14.  
  15.  
  16. ptr = strstr(longString, shortString);
  17.  
  18. printf("The word is: %c", ptr);
  19.  
  20. ptr = longString;
  21.  
  22. while(ptr!=0){
  23.       ptr = strstr(ptr,shortString)
  24.       i++;}
  25.  
  26. printf("The word appears %d times.", &i);
  27.  
  28.  
  29.  
Feb 8 '11 #1
4 1969
Banfa
9,065 Expert Mod 8TB
Try using %s on line 18, other than that tell us what the code actually does and what you expect it to do.
Feb 9 '11 #2
Oralloy
988 Expert 512MB
If you're doing pattern matching, you'll have to break out the regex library.

As Banfa observes - we need to know what you're trying to achieve in order to help you.
Feb 9 '11 #3
Bassically I am asking if shotString is in longString and then I want to know how many times. The part I am having difficulty with is this.
Expand|Select|Wrap|Line Numbers
  1.  ptr = longString;
  2.  
  3.  while(ptr!=0){
  4.        ptr = strstr(ptr,shortString)
  5.        i++;} 
I am not a hundered percent on how to count how many instances shortString is after i've found out that it is present in longString.
Feb 9 '11 #4
Banfa
9,065 Expert Mod 8TB
Your problem there is that you increment i even when ptr is 0 which it will be on the final iteration of the loop so i ends having a value that is 1 too large.
Feb 9 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Mr. B | last post by:
I want to return the name of the drawing file (DWG) from the end of a string. The string will be of varying lengths... as well as the drawing file name itself. I could do it the long way by...
74
by: Michael | last post by:
As if we needed another string reversal question. I have a problem with the following code, that I believe should work. int StringReverse(char* psz) { char *p = psz; char *q = psz +...
2
by: Paweł | last post by:
Hello! I'm looking for efficient code or site where I can find code for finding one string in another string. String which I search should have "wild" characters like '?' for any one char and...
4
by: googlegroups | last post by:
Hi, I wonder if anyone can help? How do I see if one string is found within another string? or how many instances of one string, is found within another. ie. a="Sheree smells of poo"...
8
by: MM | last post by:
Hello there I need a way to efficiently check if a string I specify can be found in another string. For example, if I look for the string "searchString" in the string "jlh ahs dalskd" then it...
4
by: Jason Gleason | last post by:
What's the most efficient way to get the number of occurences of a certain string in another string..for instance i'm using the following code right now... private int CharacterCounter(String...
22
by: Terry Olsen | last post by:
I have an app that makes decisions based on string content. I need to make sure that a string does not contain only spaces or newlines. I am using the syntax 'Trim(String)" and it works fine. I...
2
by: Bob Johnson | last post by:
Using C#/2.0 I'm writing a small "data translator" utility app that reads data out of a MS Access database and inserts it into a SQL Server database. The source db lists a bunch of names of people...
4
by: SM | last post by:
Hello, I have a simple question, but can't find the answer. I have a string that contains a path to a file I want to add another string to the end of that string So, if i have : path =...
7
by: mjbauer95 | last post by:
I have a function that I want to be able to use to find out if a string is in a string. int in(char string, char finder){ int i = 0; int j = 0; int r = 0; int flag = 0; while (string...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
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...
0
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,...

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.