tags:

views:

83

answers:

6

How can one compare a string from the middle (or some other point but not the start) to another string? like i have a string str1[]="I am genius"; now if i want to find a word in it how should i compare it with the word? for example the word is am. Here is what i did.Its a bit stupid but works perfectly :D

#include<stdio.h>

  #include<string.h>
  void print( char string[]);
  int  main()
{
  int i;
  char string1[20];
  printf("Enter a string:");
  gets(string1);
  print(string1);
  return 0;
  getch();
}
void print(char string[])
{
    int i,word=1,sum=0,x;

    for(i=0;    ;i++)
    {
    sum++;

    if(string[i]==' ')
    {
        printf("Word#%d:%d\n",word,sum-1);
        sum=0;
        word++;
    }/* if ends */
     if(string[i]=='\0')
     {    // program sai kaam karnay k liye ye code yahan bhi paste hona chahyey
        printf("Word#%d:%d\n",word,sum-1);
        sum=0;
        word++;
        break;
    }
    }/* for ends*/


}
+4  A: 

Use strncmp():

strncmp( whereToFind + offsetToStartAt, patternToFind, patternLength );
sharptooth
fahad
Pass "str + 5" there.
sharptooth
Isnt passing address a better option?
fahad
It makes no difference really.
sharptooth
caf
thanks alot :) :)
fahad
A: 

You can use parse the string into words and store them in new char arrays/pointers.

Or

Suppose the string you want to find is "am" stored in ptr *str2.

  1. You start comparison using the index[] from str1 till you find a matching char for index 0 from str2
  2. Once you find a match increment both pointers till you reach end of str2 to compare entire string.
  3. If there is no match then continue to find char at index 0 in str2 in str1 from the place where you entered step 2.

Alternatively

You have to use a two dimensinal array.

char str[3][10] = { "i","am","2-darray"};

Here str[1] will contain "am". Thats assuming you want to get indvidual words of a string.

Edit: Removed the point diverting from OP

Praveen S
This is bad advice (making a trivial problem into a difficult one) and seems not to answer the actual question...
R..
@R.. Yeah it was not even close to the OP. Edited it now.
Praveen S
A: 

One option you be to use

size_t strspn( char *s1, const char *s2) /* from #include <string.h> */
*returns the length of the longest substring of s1 that begins at the start of s1 and consists only of the characters found in s2.

If it returns ZERO than there is no substring.

BaumS
This is **NOT** what the question was asking for. `strspn` treats `s2` as a **set** of bytes and returns the number of leading bytes in `s1` which are found in the set `s2` (it stops as soon as a character not in the set is found).
R..
+4  A: 

If you wish to find a substring in a string, use the function strstr():

char *p = strstr(str1, "am");

if (p != NULL)
{
    // p now points to start of substring
    printf("found substring\n");
}
else
{
    printf("substring not found\n");
}
Amardeep
+1  A: 

Since this is homework I am assuming you can't use standard functions, so I can think of two solutions:

  1. Split all of the words into a link list, then just compare each string until you find your word.
  2. Just use a for loop, start at the beginning, and you can use [] to help jump through the string, so instr[3] would be the fourth character, as the index is zero-based. Then you just see if you are at your word yet.

There are optimizations you can do with (2), but I am not trying to do your homework for you. :)

James Black
Please don't encourage thinking like in solution 1... This sort of "let's replace a simple easy-to-process data structure with a complicated one" is why there's so much bloated crap these days. Sure it may not matter for a homework exercise, but it's a bad road to get started down...
R..
thumbs up for Mr R :D
fahad
@R - Option 1 was just to give some option, it would be more complicated, but it will be useful to know how to do this later, as, if speed is important, you can build a binary tree, with each letter as a separate node, but that depends on what he is trying to do in this assignment.
James Black
+2  A: 

If you want to compare the remainder of string s1 starting at index i1 to the remainder of string s2 starting at i2, it's very easy:

result = strcmp(s1+i1, s2+i2);

If you want to see if the substring of s1 beginning at i1 matches the string s2, try:

result = strcmp(s1+i1, s2);

or:

result = strncmp(s1+i1, s2, strlen(s2));

depending on whether you want the whole remainder of s1 to match or just the portion equal in length to s2 to match (i.e whether s1 contains s2 as a substring beginning at position i1.

If you want to search for a substring, use strstr.

R..