Quiz
Write a C++ function (you decide on the name) that takes a character array
(i.e., string) as an argument. The function trims any leading and trailing
whitespace characters from the given string and it also replaces any occur-
rences of space characters with underscores. You decide how the function
"returns" the modified string. Note: Do not use any "string function" that
may be available; i.e., do it yourself!
Write a C++ program that demonstrates your function performs correctly.
Accomplish this either as a team or as an individual.
Although the joy experienced and knowledge acquired while doing this problem
should be reward enough, I'll go ahead and assign 25 points to this exercise.
*********************************************************
* Some Help For You *
*********************************************************
Function Prototype: void My_Stringer (char[], char[]);
Function: void My_Stringer (char init_str[], char new_str[]) {
Pseudocode:
// Find where the given string begins, bypassing any whitespace.
Set Index to 0
While (init_str[Index] == Space Character OR init_str[Index] == Tab Character)
Increment Index
End While
Set Start_Index to Index
// Find where the given string ends.
// Find where the given string ends, bypassing any trailing whitespace.
// Create new string (i.e., new_str), replacing space characters with underscores.
End Function
// Programming Environment: Bloodshed
// Program Description: This program takes input from user and it stores it in
// an array. It uses this characters stored in an array to
// manupulate any space character and tab characters in
// between the other characters in to underscore as well as
// it trims any leading and trailing whitespace characters
// from the string using function.
// Date/Time: 3/29/2010 4:41:41 AM
//------------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "extras.h"
#include "Generic_Class.h"
using namespace std;
//---------------------------------------------------------------------------
// Named Constants
const int MAX_ELEMENTS = 256;
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Structure Declarations
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Function Prototypes
void My_Stringer (char[], char[]);
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
int main (void) {
cout.setf (ios::fixed, ios::floatfield);
cout.setf (ios::showpoint);
cout.precision (2);
char Init_Str[MAX_ELEMENTS]; // For manupulation.
cout << endl << "Enter Anything you want and I will convert it: ";
cin.getline(Init_Str, MAX_ELEMENTS);
char New_Str[MAX_ELEMENTS]; // Array declared to store the manupulated array.
My_Stringer(Init_Str, New_Str);
cout << endl << New_Str; // Displaying the manupulated string array which
//--------------------------our function passed after processing it.
//---------------------------------------------------------
cout << endl << endl << "\aDepress Any Key to Continue...";
_getch ();
return 0;
}
//------------------------------------------------------------------------
// Function Definitions
void My_Stringer (char init_str[], char new_str[]) {
// This function defination takes the address/argument of the the actual
// function in main and performs the calculation so that we could access the
// manupulated output from main.
// First we find the start index ignoring any space and tab characters from
// the beginning of the array till we found the characters except space and
// tab. Then, we find the end of the character that the user has last entered.
// Now, we move backwards from the position where the string ends searching
// for characters excpet space and tab as we have to ignore them.
// Once we find that character then we set that index of memory location as
// our end index so that we get the starting and ending point where we have
// to manupulate.
// In this range of start index and end index, we now convert any space and
// tab character in to underscore and leave it as it is if we encounter
// other characters excpet them. At the same time we transfer the
// manupulated characterd in to a new array. Hence displaying this array
// in the main would display the manupulated array.
// Lastly, we put the (end of character / string terminator) to the
// character array we created in main to make it a string array or to
// terminate the array.
int index = 0;
while (init_str[index] == ' ' || init_str[index] == '\t') {
++ index;
}
int start_index = index; // Start boundry.
index = 0;
while (init_str[index] != '\0') {
++ index; // After the while loop, we are at the end of user entered
//-----------character.
}
-- index; // It moves one index back than the index that has end of
//-----------character which allows us to compare the space and tab user
//-----------enters after the last characters setting the ground to ignore
//-----------trailing whitespace characters.
while (init_str[index] == ' ' || init_str[index] == '\t') {
--index; // From the end of the characters entered by user we move
//----------backwards to find space and tabs to get the end index.
}
int end_index = index; // Store it to other variable so that we can use
//------------------------index again.
int j = 0; // For our new array.
// Since, we have our boundry, we now use the for loop in our bounrdy of
// concern to put underscore where there is space and tabs. We skip the
// tabs and space in the trailing and leading section of our array by
// assigning the values to our new array in this boundry only.
for (int index = start_index; index <= end_index; ++index, ++j) {
if (init_str[index] != ' ' && init_str[index] != '\t' ) {
new_str[j] = init_str[index];
} else {
new_str[j] = '_';
}
}
new_str[j] = '\0'; // We are ending the array after loading all the
//--------------------manupulation so that when we disply the new array
//--------------------it won't load unnecessary memory loctions.
}
//------------------------------------------------------------------------
Write a C++ function (you decide on the name) that takes a character array
(i.e., string) as an argument. The function trims any leading and trailing
whitespace characters from the given string and it also replaces any occur-
rences of space characters with underscores. You decide how the function
"returns" the modified string. Note: Do not use any "string function" that
may be available; i.e., do it yourself!
Write a C++ program that demonstrates your function performs correctly.
Accomplish this either as a team or as an individual.
Although the joy experienced and knowledge acquired while doing this problem
should be reward enough, I'll go ahead and assign 25 points to this exercise.
*********************************************************
* Some Help For You *
*********************************************************
Function Prototype: void My_Stringer (char[], char[]);
Function: void My_Stringer (char init_str[], char new_str[]) {
Pseudocode:
// Find where the given string begins, bypassing any whitespace.
Set Index to 0
While (init_str[Index] == Space Character OR init_str[Index] == Tab Character)
Increment Index
End While
Set Start_Index to Index
// Find where the given string ends.
// Find where the given string ends, bypassing any trailing whitespace.
// Create new string (i.e., new_str), replacing space characters with underscores.
End Function
// Programming Environment: Bloodshed
// Program Description: This program takes input from user and it stores it in
// an array. It uses this characters stored in an array to
// manupulate any space character and tab characters in
// between the other characters in to underscore as well as
// it trims any leading and trailing whitespace characters
// from the string using function.
// Date/Time: 3/29/2010 4:41:41 AM
//------------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "extras.h"
#include "Generic_Class.h"
using namespace std;
//---------------------------------------------------------------------------
// Named Constants
const int MAX_ELEMENTS = 256;
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Structure Declarations
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Function Prototypes
void My_Stringer (char[], char[]);
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
int main (void) {
cout.setf (ios::fixed, ios::floatfield);
cout.setf (ios::showpoint);
cout.precision (2);
char Init_Str[MAX_ELEMENTS]; // For manupulation.
cout << endl << "Enter Anything you want and I will convert it: ";
cin.getline(Init_Str, MAX_ELEMENTS);
char New_Str[MAX_ELEMENTS]; // Array declared to store the manupulated array.
My_Stringer(Init_Str, New_Str);
cout << endl << New_Str; // Displaying the manupulated string array which
//--------------------------our function passed after processing it.
//---------------------------------------------------------
cout << endl << endl << "\aDepress Any Key to Continue...";
_getch ();
return 0;
}
//------------------------------------------------------------------------
// Function Definitions
void My_Stringer (char init_str[], char new_str[]) {
// This function defination takes the address/argument of the the actual
// function in main and performs the calculation so that we could access the
// manupulated output from main.
// First we find the start index ignoring any space and tab characters from
// the beginning of the array till we found the characters except space and
// tab. Then, we find the end of the character that the user has last entered.
// Now, we move backwards from the position where the string ends searching
// for characters excpet space and tab as we have to ignore them.
// Once we find that character then we set that index of memory location as
// our end index so that we get the starting and ending point where we have
// to manupulate.
// In this range of start index and end index, we now convert any space and
// tab character in to underscore and leave it as it is if we encounter
// other characters excpet them. At the same time we transfer the
// manupulated characterd in to a new array. Hence displaying this array
// in the main would display the manupulated array.
// Lastly, we put the (end of character / string terminator) to the
// character array we created in main to make it a string array or to
// terminate the array.
int index = 0;
while (init_str[index] == ' ' || init_str[index] == '\t') {
++ index;
}
int start_index = index; // Start boundry.
index = 0;
while (init_str[index] != '\0') {
++ index; // After the while loop, we are at the end of user entered
//-----------character.
}
-- index; // It moves one index back than the index that has end of
//-----------character which allows us to compare the space and tab user
//-----------enters after the last characters setting the ground to ignore
//-----------trailing whitespace characters.
while (init_str[index] == ' ' || init_str[index] == '\t') {
--index; // From the end of the characters entered by user we move
//----------backwards to find space and tabs to get the end index.
}
int end_index = index; // Store it to other variable so that we can use
//------------------------index again.
int j = 0; // For our new array.
// Since, we have our boundry, we now use the for loop in our bounrdy of
// concern to put underscore where there is space and tabs. We skip the
// tabs and space in the trailing and leading section of our array by
// assigning the values to our new array in this boundry only.
for (int index = start_index; index <= end_index; ++index, ++j) {
if (init_str[index] != ' ' && init_str[index] != '\t' ) {
new_str[j] = init_str[index];
} else {
new_str[j] = '_';
}
}
new_str[j] = '\0'; // We are ending the array after loading all the
//--------------------manupulation so that when we disply the new array
//--------------------it won't load unnecessary memory loctions.
}
//------------------------------------------------------------------------
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.