Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

c++: Questions regarding strings and cases


  • Please log in to reply

#1
finalcloud13

finalcloud13

    New Member

  • Member
  • Pip
  • 6 posts
In my entry-level computer science course, the professor wants us to write a little program that will classify a message as spam or not. Some of the instructions that are giving me trouble are the following:

1. "Enter the body of the email. Press enter on an empty line to finish." I don't understand how to do this. To make myself clear here, I mean that the user has to press Enter on an empty line in order to finish writing the body part. Example:

//This is what I want, but don't have.
Hi mom. <-- the user is able to hit the Enter key on this line
<-- and again on this line to submit the body.

//This is what I have, but don't want.
Hi mom. <-- the user is only able to press Enter once before the program takes the input.


2. What do I do to check if a string has least one word, and more than 90% of the words in the subject line consist of only uppercase letters?

More questions to come soon :) Thanks in advance.
  • 0

Advertisements


#2
Gerardo555

Gerardo555

    New Member

  • Member
  • Pip
  • 4 posts
#include <string>

#include <iostream>



#define LINELENGTH 200



int main()

{	

	// 1. "Enter the body of the email. Press enter on an empty line to finish."

	std::string body;

	char buff[LINELENGTH+1]; // include null character



	while(true)

	{

		std::cin.getline(buff,LINELENGTH);

		if(strcmp(buff,""))

			body.append(buff);

		else

			break;

	}





	

	// 2. What do I do to check if a string has least one word, and more than 90% 

	//	of the words in the subject line consist of only uppercase letters?

	std::string temp;

	size_t wsPos0 = 0, wsPos1 = 0;

	int countWords = 0, countUppercase = 0;

	bool isUpper;



	while(wsPos1 != std::string::npos)

	{

		wsPos1 = body.find(" ",wsPos0);



		isUpper = true;

		temp = body.substr(wsPos0,wsPos1-wsPos0);

		if(!temp.empty() && strcmp(temp.c_str(),"\n"))

		{

			countWords++;

			for(int ii=0; ii<temp.length(); ii++)

			{

				if(!::isupper(temp[ii]))

				{

					isUpper = false;

					break;

				}

			}

			if(isUpper)

				countUppercase++;

					

		}

		wsPos0 = wsPos1+1;

	}



	std::cout << (countUppercase*100)/countWords << std::endl;

	

	return 0;

}

  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP