How to write a function that goes through an array of
characters and eliminates any elements that are not letters
(Legal letters can be either in an uppercase or a
lowercase format.)
The function removeNonLetters should return the new
effective size of the array as the result.
Answer Posted / vivek
//This function returns true if valid character, else false
bool isCharacter(char unknownCharacter)
{
//Check whether the character falls between
// a ...z or A....Z
if( (unknownCharacter >= 'a' &&
unknownCharacter <= 'z') ||
unknownCharacter >= 'A' &&
unknownCharacter <= 'Z')
{
return true;
}
else
{
return false;
}
}
//This function removes non characters and returns array s
//effective size.
unsigned int removeNonCharacters(char *charArray, unsigned int arraySize)
{
//Lets decrement by non character as and when we identify
unsigned int effectiveArraySize = arraySize; .
//Algo: Traverse the array and in the mean time Identify
// non chars positions and move valid chars from right
// side of the array to fill those positions.
//used to traverse and mark valid chars.
unsigned int currentValidCharacterPosition = 0;
//Used to traverse and mark non valid character
unsigned int currentNonValidCharacterPosition = 0;
while( (currentValidCharacterPosition < arraySize) &&
(currentNonValidCharacterPosition < arraySize) )
{
if(isCharacter(charArray[currentValidCharacterPosition ])
{
//If we are here mean this is a valid character in
//the array, go to next character
currentValidCharacterPosition++;
}
else //This is a non character
{
//Mark the first non valid character position
currentNonValidCharacterPosition =
currentValidCharacterPosition ;
//move to findout valid character in the remaining
//array
while( (currentValidCharacterPosition <= arraySize) && (!isValidCharacter(characterArray[currentValidCharacterPosition]) )
{
--effectiveArraySize;
++currentValidCharacterPosition;
}//End While
//Check in the mean time whether we exhausted the
//array
if( currentValidCharacterPosition == arraySize)
{
return effectiveArraySize;
}//End If
//Move the valid character to fill the leftmost
//non valid character position.
characterArray[currentNonValidCharacterPosition]
= characterArray[currentValidCharacterPosition];
//Move on.
++currentValidCharacterPosition;
}//End Else (invalid character)
}//End While
return effectiveArraySize;
}
Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
a program using one dimensional array that searches a number if it is found on the list of given input numbers given by the user and locate its exact location in the list.
What do stl stand for?
What is a list in c++ stl?
What are the various types of stl containers?
How do you convert stl to steps?
How stl is different from the c++ standard library?
What are stl algorithms?
What is stl in oop?
Is stl open source?
If P is the population on the first day of the year, B is the birth rate, and D is the death rate, the estimated population at the end of the year is given by the formula: The population growth rate is given by the formula: B – D Write a program that prompts the user to enter the starting population, birth and death rates, and n, the number of years. The program should then calculate and print the estimated population after n years. Your program must have at least the following functions: 1. growthRate: This function takes its parameters the birth and death rates, and it returns the population growth rate. 2. estimatedPopulation: This function takes its parameters the current population, population growth rate, and n, the number of years. It returns the estimated population after n years Your program should not accept a negative birth rate, negative death rate, or a population less than 2. please answer my question ....
Do you like to Submit Questions in Bulk under Same Category?? Then use our Bulk ListerDo you like to Submit Questions in Bulk under Same Category?? Then use our Bulk Lister
please visit this site you'll find my question this is my homework please answer it if you can http://easyscience.org/ib/lofiversion/index.php/t36168.html
How do I convert a stl file?
When did c++ add stl?
Is there any error below, its a code to delete all entires from a map #include