C++ Library Functions and manipluation of charater strings.

Questions about the two

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1475 Views - Last Post: 14 December 2008 - 05:27 AM Rate Topic: -----

#1 ShakeZula.219  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-November 08

C++ Library Functions and manipluation of charater strings.

Posted 07 December 2008 - 08:58 PM

I am creating a program that uses 6-library functions and demonstrates the manipulation of character strings. So my question is...
What are some of the most basic functions that will make this as easy as possible.
Also what is the best way to incorporate these 6 functions into a character string.

This is for school and I believe we just need to show our basic understanding of functions and character strings. I don't want to pick 6 random functions since I may be doing more work then I need. I am not quite sure what it means to manipulate a character string. Something like inputting a couple numbers incrementing or decrementing/ formatting them ?

Thanks for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Library Functions and manipluation of charater strings.

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: C++ Library Functions and manipluation of charater strings.

Posted 07 December 2008 - 09:07 PM

Which library? The STL? And C style string right?
Was This Post Helpful? 0
  • +
  • -

#3 ShakeZula.219  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-November 08

Re: C++ Library Functions and manipluation of charater strings.

Posted 07 December 2008 - 09:31 PM

I assume its STL and yes it is C-strings. My teacher does not speak the best English so hes kinda hard to understand. I had him explain it a little more and he mentioned that it could be done with an array.

This post has been edited by ShakeZula.219: 07 December 2008 - 09:33 PM

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: C++ Library Functions and manipluation of charater strings.

Posted 07 December 2008 - 09:34 PM

strcpy()
strncpy()
strcmp()
strlen()
etc...

C String library
Was This Post Helpful? 1
  • +
  • -

#5 ShakeZula.219  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-November 08

Re: C++ Library Functions and manipluation of charater strings.

Posted 07 December 2008 - 10:28 PM

This is what i have so far
 #include <iostream>
using namespace std;

// Function prototypes
void doubleNum(int &);
void getNum(int &);

int main()
{
   int value;
   
   // Get a number and store it in value.
   getNum(value);
   
   // Double the number stored in value.
   doubleNum(value);
   
   // Display the resulting number.
   cout << "That value doubled is " << value << endl;
   return 0;
}

//Body of getNum
void getNum(int &userNum)
{
   cout << "Enter a number: ";
   cin >> userNum;
}

//Body of doubleNum
void doubleNum (int &refVar)
{
   refVar *= 2;
}



The program takes a number and doubles it.
I think this counts as two functions. I was wondering how I would save the number in an array so I could add some other functions. Like A function for increment and decrement.

Any other ideas of other functions i could use?

Also how would Add a character string to this?
Was This Post Helpful? 0
  • +
  • -

#6 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: C++ Library Functions and manipluation of charater strings.

Posted 07 December 2008 - 11:03 PM

char FirstString[] = "Hello ";
char SecondString[] = "Word!";

strcat(FirstString, SecondString);



That is going to copy "World!" into the end of "Hello ", causing the the first string (FirstString) to become "Hello World!"
Was This Post Helpful? 0
  • +
  • -

#7 ShakeZula.219  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-November 08

Re: C++ Library Functions and manipluation of charater strings.

Posted 08 December 2008 - 09:33 AM

I am kind of confused if I am doing this correct. This is the problem we have to complete.

"Design an application that demonstrate use of six-Library function and demonstrate the manipulation of character strings."

Can anyone clear up what a Library Function is? And how you can use that to manipulate character Strings. I went to the link your provided KYA but we haven't studied those functions in school. So I wonder if he would really have us design a program with functions we haven't used?

Any help is appreciated thanks.
Was This Post Helpful? 0
  • +
  • -

#8 ShakeZula.219  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-November 08

Re: C++ Library Functions and manipluation of charater strings.

Posted 08 December 2008 - 11:55 AM

This is the new code, using 5 functions

// This program prints a simple form letter reminding a customer
// of an overdue account balance.
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;

// Function Prototypes
void printLetter(char *, char *, char *, char *, char *);
void getInfo(char *, char *, char *, char *, char *);
void getSal(char *);
void printline(const char *, int&);

// Strings that make up the form letter
const char part1[] = "Dear ";
const char part2[] = "Our records show that your account has a"
					 " balance of $";
const char part3[] = " and a past due amount of $";
const char part4[] = "Your last payment was on ";
const char part5[] = "Since we haven't heard from you in some"
					 " time, would you please take a moment to send"
					 " us a check for the past due amount? We value"
					 " your business and look forward to serving you"
					 " in the future.\n\n";
const char part6[] = "Sincerely,\n";
const char part7[] = "The Management\n\n";
const char part8[] = "P.S. If you've already sent your payment, ignore"
					 " this reminder.";

int main()
{
   char salutation[4];	// To hold the salutation
   char lastName[16];	 // Customer's last name
   char lastPayment[11];  // Date of last payment
   char balance[9];	   // Account balance
   char pastDue[9];	   // Amount past due
   char again;			// To hold Y or N

   do
   {
	  // Call getInfo to get input from the user
	  getInfo(salutation, lastName, balance, pastDue,
			  lastPayment);
	  cout << "\n\n";
	  // Now print the form letter
	  printLetter(salutation, lastName, balance, pastDue,
				  lastPayment);
	  cout << "\n\nDo another letter? (Y/N) ";
	  cin >> again;
   } while (toupper(again) == 'Y');
   return 0;
}

//*****************************************************************
// Definition of function getInfo.								*					  *
//*****************************************************************

void getInfo(char *sal, char *lname, char *bal, char *due,
		   char *lastPay)
{
   getSal(sal);
   cout << "Last Name: ";
   cin >> lname;
   lname[0] = toupper(lname[0]);
   cout << "Account balance: ";
   cin >> bal;
   cout << "Past due Amount: ";
   cin >> due;
   cout << "Date of last payment (MM/DD/YYYY): ";
   cin >> lastPay;
}

//****************************************************************
// Definition of function getSal.								*
													*
//****************************************************************

void getSal(char *sal)
{
   int choice;

   do
   {
	  cout << "salutation:\n";
	  cout << "\t1) Mr.\n";
	  cout << "\t2) Ms.\n";
	  cout << "Select one: ";
	  cin >> choice;
   } while (choice != 1 && choice != 2);

   if (choice == 1)
	  strcpy(sal, "Mr.");
   else
	  strcpy(sal, "Ms.");
}

//************************************************************
// Definition of function printLetter.						*	*
//*************************************************************

void printLetter(char *sal, char *lname, char *bal, char *due,
				 char *lastPay)
{
   int position;

   // Print the salutation part of the letter
   position = 0; // Start a new line.
   printline(part1, position);
   cout << sal << " " << lname << ":" << endl << endl;

   // Print the body of the letter
   position = 0; // Start a new line.
   printline(part2, position);
   cout << bal;  // Print account balance.

   // Add length of balance to position.
   position += strlen(bal);
   printline(part3, position);
   cout << due << ". ";	 // Print past due amount
   position += strlen(due)+ 2;

   // Add length of due and the period and space at the
   // end of the sentence to position.
   printline(part4, position);
   cout << lastPay << ". "; // Print date of last payment.

   // Now Add length of lastPay and the period and space at the
   // end of the sentence to position.
   position += strlen(lastPay) + 2;
   printline(part5, position);

   // Print the closing.
   position = 0; // Start a new line.
   printline(part6, position);
   position = 0; // Start a new line.
   printline(part7, position);

   // Pring the PS reminder.
   position = 0; // Start a new line.
   printline(part8, position);
}

//*******************************************************************
  function printline.								*

//*******************************************************************

void printline(const char *line, int &startCount)
{
   int charCount = 0;

   if (startCount >= 70) // If the line is already at
   {					 // or past the right margin...
	  cout << "\n";	  // Sart a new line.
	  startCount = 0;	// Reset startCount.
   }


   while (line[charCount] != '\0')
   {
	  if (startCount >= 60 && line[charCount] == ' ')
	  {
		 cout << "		  \n";  // Print right margin.
		 charCount++;			 // Skip over the space.
		 startCount = 0;
	  }
	  if (startCount == 0)
	  {
		 cout << "		  ";  // Print left margin.
		 startCount = 10;
	  }
	  cout.put(line[charCount]); // Print the character.
	  charCount++;			   // Update subscript.
	  startCount++;			  // Update position counter.
   }
}




I have 4 functions, any other 2 functions I could easily add to this? Thanks
Was This Post Helpful? 0
  • +
  • -

#9 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: C++ Library Functions and manipluation of charater strings.

Posted 08 December 2008 - 01:21 PM

Could've swore I wrote a very long and elaborated post here last night that I (thankfully) copied and pasted to a textfile... I'll assume I did and somebody (without justification OR notification) removed it.

At anyrate, you created a bunch of functions rather than using the predefined functions in the library (like strcpy, strcmp, strcat, etc etc).
Was This Post Helpful? 0
  • +
  • -

#10 ShakeZula.219  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-November 08

Re: C++ Library Functions and manipluation of charater strings.

Posted 08 December 2008 - 03:07 PM

I think KYA listed a bunch of the predefined library functions. I am having trouble combining 6 of them together in one simple program.

Hey Hyper could you possible e-mail me your post. C3vettezo6@msn.com
I cant send PM's here or else i would do that.
Thanks Hyper
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: C++ Library Functions and manipluation of charater strings.

Posted 08 December 2008 - 03:11 PM

Or repost it. No reason why it should be deleted unless you cussed a lot or something.

What do you mean combine? Just use them?
Was This Post Helpful? 0
  • +
  • -

#12 ShakeZula.219  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-November 08

Re: C++ Library Functions and manipluation of charater strings.

Posted 08 December 2008 - 03:21 PM

well, Since I have to use 6 diff functions. I am not sure if I should just use 4 different functions of copying Or somehow use 6 Functions for Searching. Thanks
Was This Post Helpful? 0
  • +
  • -

#13 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: C++ Library Functions and manipluation of charater strings.

Posted 08 December 2008 - 03:24 PM

Ok.

Have the user enter a string.

1. Copy it to another. strcpy()

2. Tell them the length of the string. strlen()

3. Have them enter another string, append it to the end of the first. strcat()

4. Search for a substring in it. strstr()

5. Have them enter a letter to search for and give them the first occurrence of it in the string. strchr()

6. Compare two user entered strings or whatever. strcmp()


Done

edited for typos

This post has been edited by KYA: 08 December 2008 - 03:24 PM

Was This Post Helpful? 0
  • +
  • -

#14 ShakeZula.219  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-November 08

Re: C++ Library Functions and manipluation of charater strings.

Posted 08 December 2008 - 03:41 PM

Awsome KYA, Thanks for the help
Was This Post Helpful? 0
  • +
  • -

#15 ShakeZula.219  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-November 08

Re: C++ Library Functions and manipluation of charater strings.

Posted 08 December 2008 - 10:39 PM

Hey guys, This is what I have so far.
Its a program that asks the user for their first name.
it uses strcmp to find if the string is equal to my name Dan, if it is then it outputs "That's my name theif". If not than it outputs "That names stupid".
Then the program tells the user the number of characters in there first name. Then it asks for the last name and uses a function to combine them. I used another function to copy one string to another and then output the whole name. I have 4 library Functions in use now, I need to add to more.
Any simple functions you guys think I could add too this easily??


#include <iostream> //For cout
#include <cstring>  //For the string functions

using namespace std;

void main(void)
{
  char name[50];
  char lastname[50];
  char fullname[100]; // Big enough to hold both name and lastname
  char allname[100]; //so I can use  strcpy

  cout<<"Please enter your first name: ";
  cin.getline ( name, 50 );

  if ( strcmp ( name, "Dan" ) == 0 ) // Equal strings
	cout<<"That's my name theif!.\n";
  else									// Not equal
	cout<<"That names stupid.\n";


  // Find the length of your name
  cout<<"Your name is "<< strlen ( name ) <<" letters long\n";
  cout<<"Enter your last name: ";
  cin.getline ( lastname, 50 );
  fullname[0] = '\0';			// strcat searches for '\0' to cat after
  strcat ( fullname, name );	 // Copy name string into full name string
  strcat ( fullname, " " );	  // We want to separate the names by a space
  strcat ( fullname, lastname ); // Copy lastname onto the end of fullname
  strcpy (allname, fullname);	// Copy The whole string "Fullname" into "Allname"
  cout<<"Your full name is "<< allname <<"\n";
  cin.get();
}

//"STRCPY" Copies one string to another.
//"STRLEN"  This function counts
// and returns the number of characters in a string.
//"STRCAT"  add the characters of one string to the end of other string
//"STRCMP"  which returns a zero
//if 2 strings are equal, or a non zero number if the
//strings are not the same.





Let me know Thanks!!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2