character strings

Discussion in 'Programmer's Corner' started by sall, Sep 12, 2011.

  1. sall

    Thread Starter New Member

    Jul 13, 2011
    10
    0
    Given a list of words we want to find all the words that end with a particular string which is stored in character array "endstr".For example "operating","ing" are valid inputs.For this we have a function "getword" which stores all the words in array wordlist that end with string stored in endstr into targetlist array.it is defined as under
    Code ( (Unknown Language)):
    1.  
    2. void getword (char wordlist [ ] [40], char endstr[],int n,char targetlist[ ][40)
    3.  
    4. { int i,count;
    5.   for(i=0;i<n;i++)
    6. { if (            )
    7.    {strcpy(targetlist[count],wordlist[i]);
    8.    count++;}}  
    9. [/i]
    what should be the condition written in if ( ) ?
     
  2. DumboFixer

    Active Member

    Feb 10, 2009
    219
    34
    Sounds very much like a homework assignment to me

    What do you think should be written there ?
     
  3. cheezewizz

    Active Member

    Apr 16, 2009
    82
    10
    *cough* ... strstr() ... *cough*
     
  4. sall

    Thread Starter New Member

    Jul 13, 2011
    10
    0
    is there any other soln.strstr is not taught to us.
     
  5. cheezewizz

    Active Member

    Apr 16, 2009
    82
    10
    don't take this the wrong way but the most important part of an education is learning how to learn if ya know what I mean. You need to able to find things out on your own without them being taught. Just nip along to http://www.cplusplus.com/reference/clibrary/cstring/strstr/ and read up a bit on it, I'm sure using their example there as a basis you could whip something up. If your teacher already has a solution in mind using a particular method, well, we've no idea what you've covered so far in your course so can't really guess as to what it could be.... good luck :D
     
  6. sall

    Thread Starter New Member

    Jul 13, 2011
    10
    0
    sir i went through strstr .there is this problem (as far as i understood) it will only tell me whether it is a substring or not.even if the string is a substring that will not necessarily entail that the string "ends" with that other string.i require a code in the if() that will test whether the string in the wordlist array ends with the string in endstr array.so strstr wont work i suppose.pls correct me if i am wrong.
     
  7. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,809
    834
    You know the length of endstr.
    You can get that many characters from the end of wordlist.
    What else do you need to know?
     
  8. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    You will have to write a small sub function that can compare the last three characters, with the search string characters, this function should return a boolean result, true if last 3 characters match the search string, false if not..... then in the If statement you could put a call to that function and check to see if returned value is true or false.
     
  9. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    I'm a fool for doing this but:

    Code ( (Unknown Language)):
    1.  
    2.   // No, changed my mind. You should be the one to figure this out.
    3.  
    But I suggest that you consider a solution with a strcmp() and more than one strlen().
     
    Last edited: Sep 13, 2011
Loading...