473,325 Members | 2,828 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,325 software developers and data experts.

Finding if a string is in a string in C

I have a function that I want to be able to use to find out if a string is in a string.

Expand|Select|Wrap|Line Numbers
  1. int in(char string[], char finder[]){
  2.     int i = 0;
  3.     int j = 0;
  4.     int r = 0;
  5.     int flag = 0;
  6.  
  7.     while (string[i] != '\0') {
  8.         if (flag==0){ // If a part of finder has not yet been found in string
  9.             if (string[i]==finder[0]){ // check if it is the first letter of finder is this string
  10.                 flag=1;
  11.             }
  12.         } else { // The letter has been found
  13.             if (finder[j]!='\0'){
  14.                 j++;
  15.                 if (finder[j]==string[i]){
  16.                     r=1;
  17.                 } else {
  18.                     r=0;
  19.                 }
  20.             }
  21.         }
  22.         i++;
  23.     }
  24.     return r;
  25. }
  26.  
It compiles fine but then I get a Segmentation fault.
On GDB it says: Program received signal SIGSEGV, Segmentation fault.
0x08048539 in in ()
Jan 2 '09 #1
7 2335
gpraghuram
1,275 Expert 1GB
HI,
I think there is s array overwrite in the code.
In K&R C Answer book there is a similar code, take a lok into it if needed for reference.

Raghu
Jan 2 '09 #2
JosAH
11,448 Expert 8TB
Have a look: Knuth Morris Pratt algorithm - Wikipedia, the free encyclopedia.

kind regards,

Jos
Jan 2 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
Presumably this is for a class otherwise you would just use strstr.

However, you might consider doing strncmp for corresponding string[i] and
finder[i].
Jan 2 '09 #4
OK.
I didn't realize that strstr was a function, I can just use that.
Jan 2 '09 #5
Now I am using strstr.
But I still get a Segmentation Fault.

My code is this:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main(int argc, char **argv) {
  5.     int i;
  6.     char string[] = "123456789";
  7.     char variables[][1024] = {"7"};
  8.  
  9.     for(i = (sizeof(variables) / 4) - 1; i > -1; --i) {
  10.         if (strstr(string,variables[i])){
  11.             printf("%c",variables[i]);
  12.         }
  13.     }
  14.  
  15.     return 0;
  16. }
  17.  
Jan 2 '09 #6
looker
18
I don't understand why you try to make thing complicated.
check it here strstr - C++ Reference
However, if you're required to find the occurrence of a string in another one, you should better consider Knuth Morris Pratt algorithm​​​​​​​​​​​​​​​​​​​​​ what he said above.
In your previous code, I found something wrong with your function. You should never trust these two strings inputted, No one guarantees that those strings are NULL terminated; because it could be possibly that you write string in the memory and another process could override the NULL character with whatever it wants. It is because that function will stop when it reach the NULL character, but what happen if there is no NULL character, or that NULL character was stored at nowhere ?
If you want to write such kind of function, you had better put the size of strings inputted. This will be more safer and can trust.
Jan 3 '09 #7
weaknessforcats
9,208 Expert Mod 8TB
The for loop should just range from i = 0 to i<1024.

Then the printf() parameter should be %s rather than %c since every element of variables is a string.

Then you need to check that variables[i] os not null befor you call strstr. Otherwise you crash.
Jan 3 '09 #8

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

Similar topics

4
by: lecichy | last post by:
Hello Heres the situation: I got a file with lines like: name:second_name:somenumber:otherinfo etc with different values between colons ( just like passwd file) What I want is to extract...
1
by: Tristan | last post by:
Im trying to expand a search util by uing regular expression to allow common search criteria such as +-* and phrases "". My understanding of ereg(string pattern, string string, ) is that the...
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...
5
by: Mike | last post by:
Hi I've written a module to find the first occurrence of a record in a query (that is my forms datasource) that matches four specified criteria. No matter what I try it doesn't seem to find ...
6
by: SSG | last post by:
Hai All! I need the optimized code for finding the string length. i dont want to use strlen function......... can anyone know reply........ By S.S.G
14
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...
2
by: ElkGroveR | last post by:
Hi there! I'm using PHP to create a simple, dynamic MySQL SELECT query. The user chooses a selection from a HTML Form SELECT element's many options and submits the form via a POST action. ...
2
by: Extremest | last post by:
Here is the code I have so far. It connects to a db and grabs headers. It then sorts them into groups and then puts all the complete ones into another table. Problem I am having is that for some...
0
by: NSF12345 | last post by:
Iv developed a small program that looks for a file over our network, and copy it to the location of another computer. Im using the "If FileExists("\\oldpc\main share\Folder\file.txt") Then" way of...
3
nomad
by: nomad | last post by:
When I run choice == 2 I'm suppose to a an out.println back finding a product. but I get Null for all the values. Can someone help me with this. class Computer_listing { private String id; ...
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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.