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


Please Help Members By Posting Answers For Below Questions

write a program that will accept a number and print.its equivalent in words the maximum input number is 9999

2595


Explain stl.

982


how to use C++?

2154


Write a C/C++ program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the total number of disk writes by MySQL.

2067


In what scenario does the Logical file and Physical file being used?

2425






How does an stl file work?

747


What is stl in c++ with example?

731


What is stl stand for?

811


How connect plc and pc through software

2033


How do you convert stl to steps?

718


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

1740


How do I convert a stl file?

644


What is a stl vector?

707


What are the components of stl?

715


What is the use of stl?

727