Hello..
Can someone help me with this tutorial.
only have a background of how to creat a dynamic array in 4 steps but not other than that. And the question seems complicated more than only that. So kindly please someone help me and wxplain it to me.

---------------------
write a program that reads in a sentence of up to 100 characters and outputs the sentence with spacing corrected and with letters corrected for capitalization. In other words, in the output sentence, all string of two or more blanks should be compressed to a single blank. The sentence should start with an uppercase letter but should contain no other uppercase letters. Don’t worry about proper names, if there first letter are changed to lowercase, that is acceptable. Treat a line break as if it was a blank, in the sense that line break and any number are blanks are compress to a single blank. Assume that the sentence ends with a period and contains no other period. For example the input

the Answer to life, the universe, and everything
IS 42.

Should produce

The answer to life, the universe, and everything is 42.


Note:
Following functions from might help:
1. str.insert(pos, str2)
2. str.erase(pos, length)

---------------
****Note that I haven't learnt the insert and erase functions yet.
I've no idea what will they do.

Recommended Answers

All 8 Replies

Post down your current code. This forum has a rule that we cannot give homework help until you show your effort. :)

Ok here is what i've done and not even sure whether these steps are right or not.

# include <iostream>
# include <string>
using namespace std;

int main()
{
 tepedef string* sentence;

 sentence a;

 a= new string[100];

 cout<<"Please enter one sentence.";
 
 getline(cin,a);







 delete [] a;

return 0;
}

Oh it's typedef up there ! a typo!

but as to this code i'm getting a compiler error that says>>
1>------ Build started: Project: 102 hw, Configuration: Debug Win32 ------
1>Compiling...
1>fofa.cpp
1>c:\users\fofa\documents\visual studio 2005\projects\102 hw\fofa.cpp(15) : error C2664: 'std::getline' : cannot convert parameter 2 from 'sentence' to 'std::basic_string<_Elem,_Traits,_Ax> &'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1>Build log was saved at "file://c:\Users\fofa\Documents\Visual Studio 2005\Projects\102 hw\Debug\BuildLog.htm"
1>102 hw - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I made some code for this but it is missing a check for the length of input and a newline character remover ( I don't know how you are going to enter a newline character through user input ).

I made 4 functions for this where 3 of them are used in 1 (the main function). See comments.

#include <iostream>
#include <string>

using namespace std;

//makes uppercase to lowercase
void makeLower(string &in, int index)
{
	if( in[index] >= 'A' && in[index] <= 'Z' )
		in[index] += 'a' - 'A';
}
//makes lowercase to uppercase
void makeUpper(string &in, int index)
{
	if( in[index] >= 'a' && in[index] <= 'z' )
		in[index] += 'A' - 'a';
}

//makes 2 or more spaces into 1
void checkSpaces(string &in)
{
	for( int i = 0; i < in.length(); i++ )
		if( in[i] == ' ' && in[i+1] == ' ' && i != in.length()-1)
		{
			in.erase(i,1);
			i--;
		}
}

//checks the string and does the above functions
void check(string &in)
{
	checkSpaces(in);
	bool peroid = true;
	for( int i = 0; i < in.length(); i++ )
	{
		if( (in[i] >= 'a' && in[i] <= 'z') || (in[i] >= 'A' && in[i] <= 'Z') )
		{
			if( peroid )
			{
				makeUpper(in, i);
				peroid = false;
			}
			else
				makeLower(in, i);
		}
		if( in[i] == '.' )
			peroid = true;
	}	
}

int main()
{
	/*
	you can use a char array for getting user input so
	it limits it to 100 characters and then convert that
	to a string for checking
	*/
	string in;
	getline(cin, in);
	check(in);
	cout << in << endl;
	
	system("PAUSE"); //don't think you need this for VC++ but I need it for dev-C++
	return 0;
}

I hope this helps

thanks for this informative posting. its so useful for me.

lol ! Ok you kind of solved everything for me.. Thanks very much for that. But i really would like you to explain it to me.. How did you do it step by step if you don't mind.

Oh also is this a dynamic array??
Cause you didn't include the basic steps that i've studies for dynamic array. It must be another method. If so, how can i do it to be a dynamic array?

Oh I guess this can't be solved using dynamic arrays after i've revised it.
But can someone explain the coding. I want to know how please!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.