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



How to write a function that goes through an array of characters and eliminates any elements that ..

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

How to write a function that goes through an array of characters and eliminates any elements that ..

Answer / vivek

Please bear with indentation, treat charArray and characterArray as same.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More STL Interview Questions

Question 1)Read the data from one file and remove all the duplicated words and generate an output file containg only using words.Program should also print number of occurance of each words on standard output.(Program must be use STL and iostream object). Question 2)Write a program to convert the lower case contents of file to upper case using STL. Question 3)What is the output of this problem. int i=12; int &r =i; r+r/4; int +p =&r; int *p = &r; P+=r; return 1; Answer :a)12 b)17 c) 30 d)24 E)15 Question 4) #include #include #include void main() { char srcstr[30],desstr[30]; int i,len; clrscr(); cout<<"\nenter the string\n"; cin>>srcstr; len=strlen(srcstr); for(i=0;srcstr[i]!='\0';i++) { desstr[--len]=srcstr[i]; } desstr[i]='\0'; cout<<"\nreversed string is\n"; for(i=0;desstr[i]!='\0';i++) { cout< } getch(); } Answer : A) string output b)tuptuo gnirts c) string d)output Question 5) Class Test { static const Tk=LEN; int q; public: foo(intx =LEN):q(x){}; TGet Q() const { return q:} void call test Foo<long :9> foo(10); std::const<< foo.Getq(); Answer: 7,8,9,10,11 question 6) class A { int a; char b; }; class B:public A { char b; int a; }; What is the size? Answer a)5 bytes for class A and 5 bytes class B b)4 bytes for class A and 8 bytes class B. c).... d)....

2 Answers   Bally Technologies,


What is stl in oop?

0 Answers  


What two types of containers does the stl provide?

0 Answers  


What is the Difference between CArray and CList?

1 Answers   ProdEx Technologies, Siemens,


What is the use of stl?

0 Answers  






Explain stl.

0 Answers  


draw a flowchart that accepts two numbers and checks if the first is divisible by the second.

0 Answers   Appin, NIIT,


how to overload << and >> operator in c++

3 Answers   Wipro,


Write a program in C++ returning starting locations of a substring using pointers

1 Answers  


Describe the elements of Microsoft Word screen. Write down steps for creating, saving, retrieving, editing and printing a document.

2 Answers  


write a program that input four digit number and find how many 7 that number contains

4 Answers  


In what cases using of a 'template' is a better approach then using of a 'base class'?

6 Answers   Lucent, Mind Tree, Wipro,


Categories