Help with Sentence fix string program

This is a discussion on Help with Sentence fix string program within the C++ Programming forums, part of the General Programming Boards category; Look at last post....

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    6

    Help with Sentence fix string program

    Look at last post.
    Last edited by daywalker-; 10-31-2007 at 07:01 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    First, you have to come up with an algorithm on how you will you do this. Will you read in each word one at a time? Read in one character? Read in an entire line? How can you identify which characters need capitalization (or need it removed)? How can you handle a space before punctuation, since you have to check the next character to see whether the space should be output.

    While it's not the best idea, I'll be honest and say that I recently worked on something very similar and ended up diving into coding and coming up with the algorithm as I went. It was easier for me to visualize as I got a little code down. You can always do it that way, too.

    To start coding, begin with an empty main. Then add the code to output the initial prompt to cout. Compile it and run it to make sure it works. That's your code start. Then start implementing your algorithm, compiling and checking the output along the way.

    For example, maybe you want to use cin.get() in a loop to read one character at a time. You can start by adding the code to break the loop when a period is read in. Compile that and test it and make sure it stops on the period.

    Keep doing these steps and pretty soon you will have aworking program. Feel free to post any specific problems you find along the way.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    I use a like 3-4 for loops, to get rid of extra spaces and leading spaces, and for only the first letter being capitalized and the I's. How can i include all these under 1 for loop, with ifs?

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    like
    Code:
    int strshiftleft(string,str & int 0)
    {
    
    	for(int i=0, i < (str.length() ); i++)
    	{
    		if( (i+1) != str.length() )
    		{
    			str[i]=str[i+1];
    
    		}
    			str.resize(str.length()-1);

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    Could u print out some code that could do that?
    -Capitalize the first letter.. while the rest are lowercase
    -no space in between
    -no leading space so " It" ---> "It"
    -and no spacing before a punctuation "Hi ." ---> "Hi."
    I have no idea for these.. really..
    please help

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    That's what you have to figure out. Use toupper and tolower to change the capitalization of a single character.

    To remove a space, you need to identify the spot where it shouldn't be and remove it from your string or just skip it from the output.

    You have to use logic to figure it out, that's what the assignment is testing.

    I would read character by character (like the assignment recommended) and keep a couple of variables to remember what the previously read characters are.

    If you know what a finite state machine is, I would write one for this, it will help a lot.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    This assignment is a bit over for my whole class, no one is understanding.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    Can someone tell me how i can revise the functions in this????



    Code:
    void correctionspace(string& sent, int offset)//function for space
     {
    	for(int i = offset; i < sent.length(); i++)
    	{
    		sent[i]=sent[i+1];
    	}
    	sent.resize( sent.length() - 1 );// putting string to the left 
     }
    
     int main()
     {
     
    	int i;
    	string sent = "";
    	char temp;
    	
    
    
    
    	cout << "Type in a sentence:";
    
    	do
    	{
    	temp = cin.get();
    	sent = sent + temp;
    	}while(temp != '.');// sta
        int ispunct(temp) ;
    	cin.sync();
    
     	 while (isspace(sent[0]))
    	 {
           correctionspace(sent, 0);
    	 }
    
    	 for(i=1;i<int(sent.length());i++)
    		 
    	 {
       
    
    		 sent[i]=tolower(sent[i]);
    
    	   
    	 }
    	 for(i=1;i<int(sent.length());i++)
    	 {
    		 if( isspace(sent[i]) && isspace(sent[i+1]) )
    		 {
    		  correctionspace(sent, i);
    		i = 0;
    		 }
    	   
    	 }
    
    	 for(i=0;i<(sent.length());i++)
    	 {
    
    		 if( isspace(sent[i]) && (sent[i+1] == '.') )
    		 {
    			 sent.erase(i,1);
    		
    		i = 0;
    		 }
    	   
    	 }
    
    	sent[0]=toupper(sent[0]);
    
    		 	 cout<< "The correct punctuation of what has been entered is:"
    		 <<sent<<endl;
    	 return 0;
     }

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > This assignment is a bit over for my whole class, no one is understanding.
    Well then the whole class needs to take it up with the teacher right now, because it isn't going to get any easier.

    > Can someone tell me how i can revise the functions in this?
    Revise how?
    You need to tell us things like
    - what error messages you get (if any)
    - what input you provide and output you get, and how that differs from what you expect.

    Show us that you're doing more than just dumping your code on the board and hoping.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    You really need to study the string class and it's member functions. There is quite a bit that can help you out there if you know it exists. For instance:

    Code:
    string sent = "";
    char temp;
    	
    cout << "Type in a sentence:";
    
    do
    {
        temp = cin.get();
        sent = sent + temp;
    }while(temp != '.');
    There is a function called getline that will help here:
    Code:
    string sent;
    
    cout << "Type in a sentence:";
    
    getline(cin,sent,'.');  // Read everything up till the first '.' char
    sent += '.';            // Add the '.' back that was removed from the above getline call


    Code:
    int ispunct(temp) ;
    cin.sync();
    Don't know what you're trying to accomplish here, but should be removed from the code.


    Code:
    while (isspace(sent[0]))
    {
        correctionspace(sent, 0);
    }
    Ok, so you're trying to basically remove any leading spaces right? The string container has a find_first_not_of member function that is useful here. Find the first non-space character in the string and if it isn't the first character of the string (index 0) then you've got leading space that needs to be removed. In that case, call the substr (or erase) function with the index/value returned from the find_first_not_of function:
    Code:
    std::string::size_type indx = sent.find_first_not_of(" ");  // Find first non-space char
    if( indx > 0 )
    {
        sent = sent.substr(indx);  // Reset sent to start from the first non-space
        //sent.erase(0,indx);      // Also a possibility
    }
    This avoids the repeated and wasteful looping. Essentially this does a single "shift" no matter how many leading space chars there are while your code does a shift of the whole string for every single leading space.

    Something similar can be done with removing the trailing spaces from the string (those prior to the last '.' character). There is a find_last_not_of member function that you could combine with a call to the erase (or substr) member function that's handy for that. Things become especially easy if you don't add back that end '.' (as I did in the getline example) until the very end (you know it's there logically but you don't have to physically add it back until later) so you don't have to work around it.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21