Thread: Simple strings

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    9

    Unhappy Simple strings

    How to i check a string to see if another string is inside it.
    eg.

    string1: "folder1\folder2\folder3\mytext.txt
    string2: "mytext.txt"

    How do see if string2 is present in string 1

    thankyou

  2. #2
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Lightbulb

    I assume that you want to use this for folders and files. Some sort of split, maybe? If this is it then you should use command "fnsplit".
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  3. #3
    Unregistered
    Guest
    char *str1 = "folder1\folder2\folder3\mytext.txt", *str2 = "mytext.txt";

    char *buffer = strstr( str1, str2 );
    if( !strcmp( buffer, str2 ) )
    puts( "string occurs :-)" );

    didn't compile it, but should work. note that we can compare buffer and str2 only, if the searched string is in the end of str1.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    44
    Originally posted by Unregistered
    char *str1 = "folder1\folder2\folder3\mytext.txt", *str2 = "mytext.txt";

    char *buffer = strstr( str1, str2 );
    if( !strcmp( buffer, str2 ) )
    puts( "string occurs :-)" );

    didn't compile it, but should work. note that we can compare buffer and str2 only, if the searched string is in the end of str1.
    Close but no cigar. I'd prefer...
    Code:
    char * buf= strstr(str1,str2);
    if (buf) {
      /* it's in there */
    } else {
      /* it isn't */
    }
    Ian Woods

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    9
    The situation is that, i will be passing a command line argument into the program (using argv[]) from unix and if the word 'encode' is passed into it then it will perform an encode function ive done or if the word decode is passed into the program it will do a decode function.

    cheers

  6. #6
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Cool

    What about this:

    Code:
    if (strcmp(argv[1], "-encode") == 0)
    {
       // your code
    }
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    44
    Originally posted by jackwoz
    The situation is that, i will be passing a command line argument into the program (using argv[]) from unix and if the word 'encode' is passed into it then it will perform an encode function ive done or if the word decode is passed into the program it will do a decode function.

    cheers
    This is a different situation to your original post. Assuming you want your command line something like this...

    myprog encode myfile

    then you can just strcmp with argv[1] (since argv has been split based on the white space). So, you could imagine something like this:

    Code:
    int main(int argc, char ** argv) {
      if (argc!=3) {
        /* show help message */
        return EXIT_FAILURE;
      }
    
      if (!strcmp(argv[1],"encode")) {
        /* do some encoding */
      } else if (!strcmp(argv[1],"decode")) {
        /* do some decoding */
      } else {
        /* it was neither encode or decode, so make an error message */
      }
    
    ...
    Ian Woods

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    9
    Cheers Ian.I seemed to have got things confused in my head.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple question about strings
    By panfilero in forum C Programming
    Replies: 2
    Last Post: 11-03-2005, 01:57 AM
  2. Radix Sort, Strings, and Linked Lists
    By dark paladin in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 03:24 PM
  3. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM
  4. strings strings strings str..
    By Linette in forum C++ Programming
    Replies: 1
    Last Post: 02-17-2002, 02:12 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM