Thread: How to pull 3 characters out of a larger string?

  1. #1
    Whoanswers
    Guest

    How to pull 3 characters out of a larger string?

    Say I have a string of characters, up to 100 in length. I want to search that string for the word 'the', and change it to a different 3 characters. Any thoughts on how i would go about looking at just 3 characters at a time, to find any instances of 'the' in the string?

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    search the string for a 't'. if you find it, see if the letter after the 't' is an 'h', then if it is, see if the letter after the 'h' is an 'e'. now you have found 'the'. you can change it now.
    My Website

    "Circular logic is good because it is."

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    38
    Use the string function strstr. It's a substring pattern match.
    Example: char *s = "theologist", *t = "the";
    p = strstr(s,t); // Will set p=0 because it's the address location where 't' is. If 'the' was further up the list it would return that value to p. Such as 'Netherlands' would return a 2.
    SilasP

  4. #4
    jamazon00
    Guest

    like this...

    #include <iostream.h>

    void main(void)
    {
    int loop = -1;
    char word[100] = "Hello the pig lives !";
    while (word[loop++] != '\0') //change "the" to "one"
    {
    if ((word[loop] == 't') || (word[loop] == 'T'))
    if ((word[loop+1] == 'h') || (word[loop+1] == 'H'))
    if ((word[loop+2] == 'e') || (word[loop+2] == 'E'))
    word[loop] = 'o'; word[loop+1] = 'n'; word[loop+2] = 'e';
    }
    cout << "\nFinsihed Product... " << word << endl;
    cout << "Press A key to continue... \n"';
    cin >> loop;
    }

    this should work but I haven't tested it. If you wanted to check and change more then one word the above method would be inefficient, it would be best to write a procedure to do it for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM