Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help on CString

Help on CString

Scheduled Pinned Locked Moved C / C++ / MFC
helpiosquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    parth p
    wrote on last edited by
    #1

    Hi guys, I am working on C String and I'm having this weird error. here's the code

    #include #include #include //#include using namespace std;

    //Function Declarations
    void get_longest_word (char data[]);

    int main()

    {
    char * MyString;

    cout<<"--------------------------------------------------------------"<<endl;
    
    cout<<"\\nTask 1:\\n"<<endl;    
    ifstream taskonefile("CStringText.txt");
    if(! taskonefile.is\_open())
    {
        cout<<"Cannot Open File!"<<endl;
        exit(1);
    }
    int charCount;
    taskonefile.seekg(0, ios::end);
    charCount = taskonefile.tellg();
    taskonefile.seekg(0, ios::beg);
    charCount++;
    MyString = new char\[charCount\];
    
    taskonefile.get(MyString,charCount);
    cout<<MyString<<endl;
    
    
    cout<<"\\nTask 2:\\n"<<endl;
    
    
    cout<<"Longest word in the above paragraph: ";
    get\_longest\_word(MyString);
    
    
    cout<<"\\nTask 3:\\n"<<endl;
    
    int l\_num = 0;
    
    cout<<MyString<<endl;
    for(int i =0; i<=strlen(MyString); i++)
    {
    	if (MyString\[i\]=='l' || MyString\[i\]=='L')
    	{
            l\_num++;
    	}
    }
    cout << "Number of Ls" << l\_num;
    
    
    //EXIT+++++++++++++++++++++++++++++++++++++++++++++++++++
    cin.get();
    return 0;
    

    }

    //FUNCTIONS================================================

    //TASK 2 FUNCTION
    void get_longest_word (char data[])
    {
    char * token;
    char * longest_word = "a";

    token = strtok(data, " ,.-");
    
    
    while (token != NULL)
    {   
        if (strlen (token) >= strlen(longest\_word))
        {
            longest\_word = token;
        }
    	
        token=strtok(NULL, " ,.-");
    }
    
    cout<<longest\_word<<endl;
    

    }

    The problem is when I some to task 3 after calling void get_longest_word the content of MyString which is local to int main() becomes only the first character of the entire string... I have no clue what is happening wrong!

    - Stop thinking in terms of limitations and start thinking in terms of possibilities -

    K 1 Reply Last reply
    0
    • P parth p

      Hi guys, I am working on C String and I'm having this weird error. here's the code

      #include #include #include //#include using namespace std;

      //Function Declarations
      void get_longest_word (char data[]);

      int main()

      {
      char * MyString;

      cout<<"--------------------------------------------------------------"<<endl;
      
      cout<<"\\nTask 1:\\n"<<endl;    
      ifstream taskonefile("CStringText.txt");
      if(! taskonefile.is\_open())
      {
          cout<<"Cannot Open File!"<<endl;
          exit(1);
      }
      int charCount;
      taskonefile.seekg(0, ios::end);
      charCount = taskonefile.tellg();
      taskonefile.seekg(0, ios::beg);
      charCount++;
      MyString = new char\[charCount\];
      
      taskonefile.get(MyString,charCount);
      cout<<MyString<<endl;
      
      
      cout<<"\\nTask 2:\\n"<<endl;
      
      
      cout<<"Longest word in the above paragraph: ";
      get\_longest\_word(MyString);
      
      
      cout<<"\\nTask 3:\\n"<<endl;
      
      int l\_num = 0;
      
      cout<<MyString<<endl;
      for(int i =0; i<=strlen(MyString); i++)
      {
      	if (MyString\[i\]=='l' || MyString\[i\]=='L')
      	{
              l\_num++;
      	}
      }
      cout << "Number of Ls" << l\_num;
      
      
      //EXIT+++++++++++++++++++++++++++++++++++++++++++++++++++
      cin.get();
      return 0;
      

      }

      //FUNCTIONS================================================

      //TASK 2 FUNCTION
      void get_longest_word (char data[])
      {
      char * token;
      char * longest_word = "a";

      token = strtok(data, " ,.-");
      
      
      while (token != NULL)
      {   
          if (strlen (token) >= strlen(longest\_word))
          {
              longest\_word = token;
          }
      	
          token=strtok(NULL, " ,.-");
      }
      
      cout<<longest\_word<<endl;
      

      }

      The problem is when I some to task 3 after calling void get_longest_word the content of MyString which is local to int main() becomes only the first character of the entire string... I have no clue what is happening wrong!

      - Stop thinking in terms of limitations and start thinking in terms of possibilities -

      K Offline
      K Offline
      krmed
      wrote on last edited by
      #2

      strtok searches for the specified tokens within the string and when it finds a token, it replaces it with a NULL character, effectively shortining the original string to everything before the first token. Hope that helps.

      Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

      P 1 Reply Last reply
      0
      • K krmed

        strtok searches for the specified tokens within the string and when it finds a token, it replaces it with a NULL character, effectively shortining the original string to everything before the first token. Hope that helps.

        Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

        P Offline
        P Offline
        parth p
        wrote on last edited by
        #3

        Yes, I know that but I am using parameter by value and not reference. Right now I have re-initializing the MyString variable after I make call to any functions or procedures...

        - Stop thinking in terms of limitations and start thinking in terms of possibilities -

        C 1 Reply Last reply
        0
        • P parth p

          Yes, I know that but I am using parameter by value and not reference. Right now I have re-initializing the MyString variable after I make call to any functions or procedures...

          - Stop thinking in terms of limitations and start thinking in terms of possibilities -

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          p.a.r.t.h wrote:

          but I am using parameter by value and not reference.

          Yes, but you pass the pointer by reference. It means that the function will make a copy of the pointer, but the copy will still point at the same memory location. If you think of a pointer being a simple integer variable (for sake of simplicity) containing a number (which is an address), when you pass that variable to your function, you will get a copy but which still contains the same number (the same address). Thus, if you modify the content of the pointer (changing what is at the specified address), the changes will be visible in your main because the pointer points at the same address.

          Cédric Moonen Software developer
          Charting control [v1.5] OpenGL game tutorial in C++

          P 1 Reply Last reply
          0
          • C Cedric Moonen

            p.a.r.t.h wrote:

            but I am using parameter by value and not reference.

            Yes, but you pass the pointer by reference. It means that the function will make a copy of the pointer, but the copy will still point at the same memory location. If you think of a pointer being a simple integer variable (for sake of simplicity) containing a number (which is an address), when you pass that variable to your function, you will get a copy but which still contains the same number (the same address). Thus, if you modify the content of the pointer (changing what is at the specified address), the changes will be visible in your main because the pointer points at the same address.

            Cédric Moonen Software developer
            Charting control [v1.5] OpenGL game tutorial in C++

            P Offline
            P Offline
            parth p
            wrote on last edited by
            #5

            I see, thanks for that. But is there a way to avoid this?

            - Stop thinking in terms of limitations and start thinking in terms of possibilities -

            CPalliniC 1 Reply Last reply
            0
            • P parth p

              I see, thanks for that. But is there a way to avoid this?

              - Stop thinking in terms of limitations and start thinking in terms of possibilities -

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              You may copy the CString and do whatever you want with the copy without altering the original one. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups