Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C++ string functions

  • 04-04-2006 02: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, Registered Users 2 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, Registered Users 2 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