String process

I need help ... !

Page 1 of 1

1 Replies - 797 Views - Last Post: 13 December 2010 - 04:09 AM Rate Topic: -----

#1 Sirneedhelpalot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-November 10

String process

Posted 13 December 2010 - 12:48 AM

Hi everyone, I had a program and it seems to be running well without any errors. The point is to input a text in the program and gives an option of things you can do such as, toupper, reverse, inverse, etc. Please take a look because after you input the text none of the options work. Please I need quick help its urgent. Thanks.


 // Hyung WOok Kim
// CS 1
// Assignment 4
#include <iostream>
#include<string>

using namespace std;
void inverseString(string &str);//1
void reverseString(string &str);//2
void ToUpperCase(string &str);//3
void jumbleString(string str);//4
int countWords(string str);//5
int CountConsonants(string str);//6
int main()
{
	string inputStr;
	char inputOption='n';
	cout<<"Please Enter a word, a sentence, or a string of numbers:\n";
	//cin>>inputStr;
	getline (cin,inputStr);
	while(1)
	{
		cout<<"		\n\n\nUSE THIS MENU TO MANIPULATE YOUR STRING\n";
		cout<<"		----------------------------------------\n";
		cout<<"		1) Inverse String Enter 1\n";
		cout<<"		2) Reverse String Enter 2\n";
		cout<<"		3) To Uppercase Enter 3\n";
		cout<<"		4) Jumble String Enter 4\n";
		cout<<"		5) Count Number Word Enter 5\n";
		cout<<"		6) Count Consonants Enter 6\n";
		cout<<"		7) Enter a Different String Enter 7\n";
		cout<<"		8) Print the String Enter 8\n";
		cout<<"		Q) Quit Enter Q\n";
		cout<<"		\n\nplease select any of the option above:    \n";
		cin>>inputOption;
		
		if(inputOption=='1')
		{
			inverseString(inputStr);
		}
		else if(inputOption=='2')
		{
			reverseString(inputStr);
		}
		else if(inputOption=='3')
		{
			ToUpperCase(inputStr);
		} 
		else if(inputOption=='4')
		{
			 jumbleString(inputStr);
		} 
		else if(inputOption=='5')
		{
			cout<<"The number of Words in the string are "<<countWords(inputStr);
		} 
		else if(inputOption=='6')
		{
			cout<<"The number of consonants in the string are "<<CountConsonants(inputStr);
		}
		else if(inputOption=='7')
		{
			cout<<"Please Enter a word, a sentence, or a string of numbers:\n";
			cin>>inputStr;
		}
		else if(inputOption=='8')
		{
			cout<<inputStr;
		}
		else if(inputOption=='q' || inputOption=='Q')
		{
			break;
		}
		else
		{
			cout<<"\n\nWrong Option selected showing Main Menu again! please wait....\n\n";
		}
	}
	cout<<"\n\n		Program Closed:\n";
	while(1);
}
void inverseString(string &str)
{
	int length=str.length();
	int element;
	for(int i=0;i<length;i++)
	{
		element=static_cast<int>(str[i]);
		if(element>=65 && element<=90)
			element=element+32;
		else if(element>=97 && element<=122)
			element=element-32;
		str[i]=static_cast<char>(element);
	}
}
void reverseString(string &str)
{
	int length=str.length();
	int mid=0;
	if(length%2 !=0)
		mid=length/2+1;
	else
		mid=length/2;
	int j=length-1;
	char temp;
	for(int i=0;i<mid;i++)
	{
		temp=str[i];
		str[i]=str[j];
		str[j]=temp;
		j--;
	}
}
void ToUpperCase(string &str)
{
	int length=str.length();
	int element;
	for(int i=0;i<length;i++)
	{
		element=static_cast<int>(str[i]);
		if(element>=97 && element<=122)
			element=element-32;
		str[i]=static_cast<char>(element);
	}
}
void jumbleString(string str)
{
	int length = str.length();
	for (int i=0; i<length; ++i)
	{
		int index1 = (rand() % length);
		int index2 = (rand() % length);
		char temp = str[index1];
		str[index1] = str[index2];
		str[index2] = temp;
	}
	cout<<" jumbled version of the string is "<<str;

}
int countWords(string str)
{
	int count=0;
	int length=str.length();
	int i=0;
	while(i<length)
	{
		//eleminate space at the start
		if(i==0)
		{
			while(i<length && str[i]==' ')
				i++;
		}
		if(str[i]!=' ')
		{
			count++;
			while(i<length && str[i]!=' ')//unless find space
				i++;
		}
		while(i<length && str[i]==' ')//unless find new word
			i++;
	}
	return count;

}
int CountConsonants(string str)
{
	int count=0;
	int length=str.length();
	ToUpperCase(str);
	for(int i=0;i<length;i++)
	{
		char ch=str[i];
		if((ch!='A')&&(ch!='E')&&(ch!='I')&&(ch!='O')&&(ch!='U'))
			count++;
	}
	return count;
}



Is This A Good Question/Topic? 0
  • +

Replies To: String process

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: String process

Posted 13 December 2010 - 04:09 AM

Give us details of your problem.

This
"after you input the text none of the options work"
is not accurate.

Here is your program working fine.
Please Enter a word, a sentence, or a string of numbers:
the dog
		


USE THIS MENU TO MANIPULATE YOUR STRING
		----------------------------------------
		1) Inverse String Enter 1
		2) Reverse String Enter 2
		3) To Uppercase Enter 3
		4) Jumble String Enter 4
		5) Count Number Word Enter 5
		6) Count Consonants Enter 6
		7) Enter a Different String Enter 7
		8) Print the String Enter 8
		Q) Quit Enter Q
		

please select any of the option above:    
5
The number of Words in the string are 2	



So, what, exactly, is the problem with your code.
Give full details with examples of
( a ) the input
( b ) the expected output
( c ) the actual output
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1