Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ string functions

Options
  • 04-04-2006 2:33pm
    #1
    Closed Accounts Posts: 5


    I picked up programming as a hobby and it's been going pretty well so far.

    I'm writing a C++ program at the moment and I'm pretty stuck.

    I need to check to see if a number is contained within another number. The problem is the other number is too large for any number variable, sso the only way I could load it into the program was to read it from a text file and store it in a character array.
    [PHP]
    // load number
    char number[100000];
    fstream file_op("c:\\number.txt",ios::in);[/PHP]

    That worked fine so I decided to store the number I'm trying to find in a text file and put it into a character array, so i could compare the two.

    Problem is I can't seem to get any of the string comparison functions to work. I don't which ones to use or how to use them.

    C++ For Dummies says to use

    [PHP]int strnstr(string, pattern, n)[/PHP]

    but it doesn't seem to be in the header it specifies;

    [PHP]<strings.h>[/PHP]

    Any help would be greatly appreciated.


Comments

  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    A char array a million big to store a number, how big is this number?

    Surely a __int64 or LONG64 would suffice.

    strcmp (...) from <string.h> will compare two strings, if two strings are the same it will return 0.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit




  • Registered Users Posts: 1,481 ✭✭✭satchmo


    or strstr in <string.h>...
    char haystack[]="1234567890";
    char needle[]="345";
    if(strstr(haystack, needle)) //found
    else //not found
    


  • Closed Accounts Posts: 5 Lifeguard


    Thanks very much for all the help the and string functions worked well.

    There's only one problem - when I try to store the number from the text file into the array it stops when it encounters a space.

    The other problem is the number has some zeros in it.

    It is possible to get the array to read the number in full and distinguish between spaces and zeros?


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    How are you reading in the number from the text file?


  • Advertisement
  • Closed Accounts Posts: 5 Lifeguard


    The number I'm trying to load is e or exponentional, well at least the few hundred thousand digits.

    I managed to load the number using the following code, it reads up until a space into the buffer array, dumps that code in the larger array, reads up until a space and does the same thing, all the way up to a 1000 spaces.

    [PHP]// create array to store 'e'
    char e[100000];

    // open text file containing 'e'
    ifstream fin("e.txt");

    // load the first line of digits into the array
    fin >> e;

    // counter to keep track of digits already read
    int i = 0;

    for(int f = 0; f < 1000; f++)
    {
    char buffer[80];

    // count up to where the array stops
    for (; e; i++);

    // load the next line of digits, ignoring those already read
    fin.ignore(i, '\n') >> buffer;

    // add the buffer array to the larger array
    strcat(e, buffer);
    }[/PHP]


Advertisement