Thread: String manipulation issues

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    12

    String manipulation issues

    IDE - CodeBlocks
    Compiler - GNU GCC

    Hello,

    I ran into a C++ exercise has me stumped. A program is to ask for a sentance, then it splits it up into individual words and puts the sentance back together with ampersands inplace of the white space.

    My program is able to take the sentance and show it split into words. However, when it goes to show the sentance with the ampersands it errors and ends with "Process terminated with status -1073741819"

    I don't understand how I have errored. Any suggestions would be greatly apprecaited.

    My code:
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main(){
        char *p;
        char string[81];
        char string2[81];
    
        cout << "Input a string to parse: ";
        cin.getline(string, 81);
    
        p = strtok(string, ", ");   // gets first token
        strncpy(string2, p, 81);    // copies first token to string2
        strcat(string2, "&");       // concatenate string "&" to string 2
        cout << p << endl;          // print out token
    
        while(p != NULL){               // Do until end of sentance
            p = strtok(NULL, ", ");     // gets next token
            strncpy(string2, p, 81);    // copies token to string2
            strcat(string2, "&");       // concatenate string "&" to string 2
            cout << p << endl;          // print out token
        }
    
        cout << string2 << endl;        // print out string2
    
        return 0;

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Obviously, "p == NULL". Do this:
    Code:
    if (p)
        cout << p << endl;
    Devoted my life to programming...

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't use strtok, strncpy, strcat or char arrays.
    Use std::string and std::cin >> instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    12
    Thanks for the reply.
    The point of this exercise is to use <cstring> functions. The <string> functions have not been covered yet.

    This is working code that I am building on. It takes a sentance and prints out the words individually. I am trying to put the words back into a string with ampersands instead of white space.

    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
        char *p;
        char string[81];
    
        cout << "Input a string to parse: ";
        cin.getline(string, 81);
    
        p = strtok(string, ", ");   // gets first token
        cout << p << endl;          // print out token
    
        while(p != NULL){               // Do until end of sentance
            p = strtok(NULL, ", ");     // gets next token
            cout << p << endl;          // print out token
        }
    
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    12
    There is a subtle mistake in the while loop. You call strtok and then print out the result, but you do not check if the pointer is null before using cout with the pointer.

    Compare your code to the following, which does not result in an exception (note that the degenerate case of a user entering an empty or all-delimiter sentence is also covered):

    Code:
    #include <iostream>
    #include <cstring>
     
    using namespace std;
     
    int main()
    {
        char *p;
        char string[81];
     
        cout << "Input a string to parse: ";
        cin.getline(string, 81);
     
        p = strtok(string, ", ");
     
        while (p != nullptr)
        {
            cout << p << endl;
            p = strtok(nullptr, ", ");
            
        }
     
        cout << "Press any key to exit . . ." << endl;
        cin.get();
    
        return 0;
    }
    You should go through the program step-by-step (mentally or otherwise) to understand what it is doing. You should also use the keyword nullptr instead of NULL.
    Last edited by Time Traveler; 01-24-2012 at 08:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String manipulation
    By gadu in forum C Programming
    Replies: 3
    Last Post: 08-29-2008, 02:42 AM
  2. string vector to string pointer manipulation
    By stanlvw in forum C++ Programming
    Replies: 11
    Last Post: 07-16-2008, 01:43 AM
  3. A little help with string manipulation
    By InvertedSaint in forum C Programming
    Replies: 17
    Last Post: 11-05-2005, 09:14 AM
  4. string manipulation
    By SPEKTRUM in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 11:41 AM
  5. String manipulation
    By casanova0o7 in forum C++ Programming
    Replies: 2
    Last Post: 01-23-2002, 11:45 PM

Tags for this Thread