GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Apr-2006, 20:23
sasukekun sasukekun is offline
New Member
 
Join Date: Apr 2006
Posts: 5
sasukekun is on a distinguished road

Need help with strings


I am a newbie at c++ programing, but we are using MS .Net version of C++. Anyway, I would appreciate it if someone could help give some tips on how to capitalize a whole string of array (Not just the first char of the word). We need to make a console that outputs 25 lines of array which i did with:

CPP / C++ / C Code:
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

//global variables
arrayTable[25];
int arraySize = 25;

//main body
int main ()
{

   return 0;
}

void arrayDisplay()
{
    // did my forloop as a function
}

string UserInputOutput ()
{
    //did my little functin to get the user input and spit it back out.

   return userText();
}
/*********************************************************
*From this bottom function I need help Converting a whole array line[1]       *
*from lower to upper case. I mean every char within that line not just first   *
*char of a word.                                                                                *
*********************************************************/
string caps (string& userText)
{
//initilized to allow the user to choose which array slot to convert from lower to upper characters.
	int choose = 0; 
        
	cout << "Capitalize All Characters on line: ";
	cin >> choose;

//create and iniitalize integer lineStart to begin with char 0 in the array slot.        
	int lineStart  = userText.begin();

//create and iniitalize integer lineStart to begin with char 0 in the array slot.
	int lineEnd = userText.end();

//creating a forloop so that I can bring the position holder across all chars in the array.
	for (int i = 0; i < lineEnd; i++)

//this part im not so sure of, but i tried using the numbers from the ascii table and compare it to the char that is held within the current position marker.
		if (lineStart < 123 && lineStart > 96)
		{
//this line convert current char held by its place holder from lower to upper.
			userText[i] = toupper(userText[i]);
//this line increment lineStart which is supposed to start at position 0.
			lineStart ++;
		}

		return userText;
}

Thanks in advance.
  #2  
Old 23-Apr-2006, 00:14
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need help with strings


Quote:
Originally Posted by sasukekun
. Anyway, I would appreciate it if someone could help give some tips on how to capitalize a whole string of array

Much of your code won't compile on my C++ compiler (GNU gcc), and you didn't ask for advice in general, so I'll just show a couple of ways (three, actually) using standard C++ to translate a string to upper case. Pick any of the three that you like and try it in your program.

CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s("The quick brown fox, etc. The number is BR-549");

    string s1 = s;
    string s2 = s;
    string s3 = s;

    cout << "s :  <" << s << ">" << endl;

    cout << "Before loop 1    --- ";
    cout << "s1:  <" << s1 << ">" << endl;
    for (string::iterator sIter = s1.begin(); sIter != s1.end(); sIter++) {
        *sIter = toupper(*sIter);
    }
    cout << "After  loop 1    --- ";
    cout << "s1:  <" << s1 << ">" << endl << endl;

    
    cout << "Before loop 2    --- ";
    cout << "s2:  <" << s2 << ">" << endl;
    for (unsigned i = 0; i < s2.length(); i++) {
        s2[i] = toupper(s2[i]);
    }
    cout << "After  loop 2    --- ";
    cout << "s2:  <" << s2 << ">" << endl << endl;

    
    cout << "Before transform --- ";
    cout << "s3:  <" << s3 << ">" << endl;
    transform(s3.begin(), s3.end(), s3.begin(), (int(*)(int))toupper);
    cout << "After  transform --- ";
    cout << "s3:  <" << s3 << ">" << endl << endl;

    return 0;
}

My output:

Code:
s : <The quick brown fox, etc. The number is BR-549> Before loop 1 --- s1: <The quick brown fox, etc. The number is BR-549> After loop 1 --- s1: <THE QUICK BROWN FOX, ETC. THE NUMBER IS BR-549> Before loop 2 --- s2: <The quick brown fox, etc. The number is BR-549> After loop 2 --- s2: <THE QUICK BROWN FOX, ETC. THE NUMBER IS BR-549> Before transform --- s3: <The quick brown fox, etc. The number is BR-549> After transform --- s3: <THE QUICK BROWN FOX, ETC. THE NUMBER IS BR-549>

Loop 1 shows use of iterators.

Loop 2 shows use array index[] notation. This is valid and is standard C++, but, no bounds checking is done. For this example, that's no problem since the loop obviously doesn't go outside the string. (Of the three methods that I show, this is the only that is available to C programmers.)

The transform function is preferred by many experienced C++ programmers, since the function itself goes through the string and applies tolower() to each element. And, I presume, that the code would have been optimized as much as possible by the library function. The complicated-looking type casting on "toupper" is required, since there are actually a couple of toupper functions in the standard C++ library. In fact, in some compiler libraries the toupper() that we want has been implemented as a macro, so that no type information is associated by its name. (So just writing the identifier "toupper" by itself as the fourth argument to the transform() function would not give enough information to the compiler to let it know what function we need here.)

We use the cast to tell the compiler that the use of "toupper" in this instance (the name "toupper" by itself) is supposed to be a pointer to an int function whose agument is an int. (Therefore the compiler picks the int toupper(int) function that is the same as the C standard library toupper() function.)


Regards,

Dave
Last edited by davekw7x : 23-Apr-2006 at 01:07.
  #3  
Old 23-Apr-2006, 21:41
sasukekun sasukekun is offline
New Member
 
Join Date: Apr 2006
Posts: 5
sasukekun is on a distinguished road

Re: Need help with strings


okay i must be invoking it wrong then. My current codes are:

CPP / C++ / C Code:
// practice.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include "CinReader.h"

using namespace std;

CinReader reader;

//string functions
string delTab (string  txt );
string delEndSpace (string txt);
string delFrontSpace (string txt);
string delSpaces (string txt );
string print (string txt);
string edit (string txt);
string caps (string& userText);



//other funciton types
void pause ();
void puaseBlock ();
void printArray ();
void getText (string& userText);
bool exit ();
int checkSelect3 ();
int menu ();

//global varables
string line[25];
int arraySize = 25;



int _tmain(int argc, _TCHAR* argv[])
{
	menu();

		return 0;
}

int menu()
{
	int select = 0; 
	string out = "";
	string newtxt = "";
	string userText = "";
	string symbolEquals(60,'=');
	string empty(25,' ');
	string empty2(14,' ');
	string empty3(30,' ');
	string empty4(11,' ');

	do
	{
		printArray ();
		cout << " " << symbolEquals << endl;
		cout << "|" << empty << "-==Menu==-" << empty << "|"  << endl;
		cout << "|" << empty4 <<"<1> Enter Text :: <2> Edit :: <3> Quit" 
		     << empty4 << "|" <<endl;
		cout << " " << symbolEquals << endl;

		cin >> select;

		switch ( select )
		{
			//enter text
		case 1:					
			getText (userText);
			break;

		case 2:	
			newtxt = caps (newtxt);

			break;
		case 3:
			if (exit() == true)
				select = 4;
			break;
		}
	}
	while (select !=4);

	return select;	
}

bool exit()
{
	bool quit = false;
	int select = 0;

	cout << "Are you sure you wish to quit? \n"
		<< "[1]=> Save (Not yet Functional)\n"
		<< "[2]=> Exit\n"
		<< "[3]=> Return\n"
		<< "Enter your choice: ";
	cin >> select;

	//Save progress and Exit
	if (select == 1)
	{
	}
	//discard progress and Exit
	else if (select == 2)
	{
		quit = true;
	}
	else if (select == 3)
	{
		checkSelect3 ();
	}

	return quit;
}

int checkSelect3 ()
{
	bool quit = false;
	int select = 0;
	cout << "\n\n";
	menu();

	return quit;
}
void printArray ()
{	
	for (int i = 0; i < 25; i++)
		cout << i + 1 << "=> " << line[i] << endl;
}
void getText (string& userText)
{
         int chooseLine = 0;
	cout << "Choose line to use: ";
		cin >> chooseLine;
	cout << "Enter your text: ";
		cin >> userText;
		userText = print (userText);
	line[chooseLine - 1] = userText;
}

//need help in this function for find and replace. Not sure if i did it right or not, but i am not getting errors and how can i invoke it to work properly.
string edit (string txt)
{
	string newtxt = txt;

	cout << "Choose line to edit: ";
		int chooseLine = reader.readInt(true, 1,25);
	cout << "Find: ";
		string userText = reader.readString();
	cout << "Replace with: ";
		string replaceTxt = reader.readString();
		
	while ((chooseLine = line[chooseLine  -1].find(userText)) != string::npos)
		line[chooseLine -1].replace(chooseLine,userText.length(),replaceTxt);

	return newtxt;
}
string delTab (string txt)
{
	int Position = txt.length();
	string newtxt = txt;

	for (int i = 0; i < Position; i++)
	{
		if (newtxt[i] == '\t')
			newtxt[i] = ' ';
	}

	return newtxt;
}

string delEndSpace (string txt)
{
	int Position = txt.length() -1;
	string newtxt = "";

		while (isspace(txt[Position]))
	{
		Position --;
	}

	newtxt = txt.substr(0,Position + 1);

	return newtxt;
}
string delFrontSpace (string txt)
{
	int Position = 0;
	string newtxt = txt;

		while (isspace(newtxt[Position]))
	{
		Position ++;
	}
	
		newtxt = newtxt.substr(Position);

	return newtxt;
}
string delSpaces (string txt)
{	
	string space = " ";
	string twoSpace = "  ";
	string newtxt = txt;

	int Position = newtxt.find(twoSpace,0);


	while (Position != string::npos)
	{
		newtxt.replace(Position, twoSpace.length(), space);
		Position = newtxt.find(twoSpace);
	}
	
	return newtxt;
}
string caps (string& userText)
{
	string newtxt = "";
	int choose = 0;
	cout << "Capitalize All Characters on line: ";
	cin >> choose;
	string::iterator begin(newtxt.begin());

	for (unsigned int i = 0; i < newtxt.length(); i++)
		{
			newtxt[i] = toupper(newtxt[i]);
		}

		return newtxt;
}
string print (string txt)
{	
	string newtxt = "";

	newtxt = delTab (txt);
	newtxt = delFrontSpace (newtxt);	
	newtxt = delEndSpace(newtxt);
	newtxt = delSpaces (newtxt);

//puaseBlock();

return newtxt;
}
void pause ()
{
string pause = "";
		cin >> pause;
}
void puaseBlock()
{
	pause();
}

Now i know the findReplace function doesnt do its job, but it all runs without error. Same goes for the Capitalization of the array string. I am pretty sure my invoking is all wrong for these funtions. All the other invokation works because i had help with those from an instructor, but I cant seem to understand how to invoke these properly. There is just too many different ways to do invoking that i do not understand at all.
  #4  
Old 24-Apr-2006, 09:33
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need help with strings


Quote:
Originally Posted by sasukekun
okay i must be invoking it wrong then.

Since I have no way of compiling your program there's not much I can to about overall functionality, however, there is a way for you to get to the bottom of things yourself. (It's faster, I claim, and more edifying than posting a request for help and waiting for a helpful response).

My suggestion when implementing a new function is to create a test program consisting of a main() function that does nothing but invoke the new function. Test it until you think it is correct. Then try to integrate into your real program.

If the test program seems to work OK, but the function doesn't seem to be doing its job in the real program, then put some print statements before the function call and inside the function to see what the program is seeing. Make the program tell you what it is working on and what it is doing.

For example:

CPP / C++ / C Code:

// in main
    case 2: 
      cout << "Case 2: calling caps(" << newtxt << ")" << endl;
      newtxt = caps (newtxt);
      cout < <"caps returned newtxt = " << newtxt << ">" << endl;
      break;
.
.
.
// The function
string caps (string& userText)
{
  cout << "In caps(): userText = <" << userText << ">" << endl;
  string newtxt = "";
  int choose = 0;
  cout << "Capitalize All Characters on line: ";
  cin >> choose;
  string::iterator begin(newtxt.begin());


  for (unsigned int i = 0; i < newtxt.length(); i++)
    {
      cout << "i = " << i << " Before toupper: newtxt[" << i << "] = " << newtxt[i] << endl;
      newtxt[i] = toupper(newtxt[i]);
      cout << "i = " << i << " After  toupper: newtxt[" << i << "] = " << newtxt[i] << endl;
    }
    cout << "returning newtxt = <" << newtxt << ">" << endl;
    return newtxt;
}


Regards,

Dave
  #5  
Old 24-Apr-2006, 11:51
sasukekun sasukekun is offline
New Member
 
Join Date: Apr 2006
Posts: 5
sasukekun is on a distinguished road
Cool

Re: Need help with strings


Thanx Dave. Your the best, I have been doin little tests in its own main as an independant cpp file to make it work first, but i just couldnt invoke it properly into the larger project. But alast i got it working. I just missed a simple &ampersign to the other function so i can draw the current values within the userText variable. Anyway, thanks a bunch, now all i just need to work on the find and replace function. Which i also am not sure of how it works and all, but its always better to try till you can think no more before asking for more help eh
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings tripping me up once more Elsydeon C++ Forum 5 04-Dec-2005 18:41
need help - questions about strings Benayoun C Programming Language 6 24-Jan-2005 03:15
array of pointers to strings mirizar C++ Forum 5 21-Jan-2005 11:24
C++ style strings and STL dexter C++ Forum 14 04-Jan-2005 08:46
I am reviewing Arrays and need help converting some strings to arrays jenmaz C Programming Language 22 23-Nov-2004 00:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:38.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.