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.
Answers were Sorted based on User's Feedback
Answer / 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 |
Answer / vivek
Please bear with indentation, treat charArray and characterArray as same.
| Is This Answer Correct ? | 0 Yes | 0 No |
how to making game in c++ ?
what's the difference between abstract class and concreate class? what's the meaning of standard template library(STL)?
What are the symptoms of stl?
Write a program in C++ returning starting locations of a substring using pointers
What do you mean by stl?
Why should a c++ programmer be interested in stl?
Give the output of the following program main() {char *p='a'; int *i=100/*p; } what will be the value of *i= 1
How Find, Replace and Go To commands ca be used to substitute one character string for another? Explain with the heIp of an example.
how can u do connectivity in c++ language? plz send me connectivity code in c++ ?
Explain References in C++
What is the stl, standard template library?
What is meant by stl in c++?