void removeWhitespace(char\* str) {
int current \= 0;
int ahead \= 0;
while (str\[ahead\] !\= '\\0') {
while (str\[ahead\] \=\= ' ') ahead++;
str\[current\] \= str\[ahead\];
current++;
ahead++;
}
}
Implementing strcpy
char *strcpy ( char *dest, const char *src );
strcpy is short for string copy, which means it copies the entire contents of src into dest. The contents of dest after strcpy will be exactly the same as src such that strcmp ( dest, src ) will return 0.
size\_t strlen ( const char \*s ); ```strlen will return the length of a string, minus the termating character ('\\0'). The size\_t is nothing to worry about.
[Read More]
Implementing strcat
char \*strcat ( char \*dest, const char \*src );
```strcat is short for string concatenate, which means to add to the end, or append. It adds the second string to the first string. It returns a pointer to the concatenated string. Beware this function, it assumes that dest is large enough to hold the entire contents of src as well as its own contents.Implementing strcmp
int strcmp ( const char \*s1, const char \*s2 );
```strcmp will accept two strings. It will return an integer. This integer will either be:
Negative if s1 is less than s2.
Zero if s1 and s2 are equal.
Positive if s1 is greater than s2.
First non repeating element in the array
Question: Write an algorithm to find the first non-repeated character in a string. For example, the first non-repeated character in the string ‘abcdab’ is ‘c’.
Answer: Seems trivial enough right? If a character is repeated, we should be able to search the string to determine if that character appears again. So if we go about doing this for every character in the string, our worst case run time would O(n^2). Here is some code to show this logic.
[Read More]
Find anagrams in the array of Strings
Question: You are given an array of strings and you are asked to display all the anagrams within the array. For those who don’t know, two words are anagrams if they contain the same characters.
We have already discussed how to check if 2 strings are anagrams here.
Answer: The brute force solution would be to take each string and compare it to every other string in the array one character at a time ( O(n^4) solution, assuming there are n strings with maximum n characters each).
[Read More]
Implementing a strstr() / indexOf function
strstr() find the substring index into the larger string and returns the index of first occurrence. Below is the code implementation in java. However note that strstr() equivalent in java is indexOf() which do the same thing.
public static int strStr(String large, String possibleSubStr){ for(int i \= 0; i < large.length(); i++ ) { for(int j \= 0; j < possibleSubStr.length() && i+j < large.length(); j++ ) { if(possibleSubStr.charAt(j) !\= large.
[Read More]
Combination of all the possible characters in the string
Question: Write an algorithm to print all possible combinations of characters in a string.
Note : By combination it means number of different strings possible taking 1 or 2 …or n characters from string, so we dont have to worry about various character permutation. So for beta, when you get eta, you don’t have to get its anagrams like ate, tae etc. For a string of length n, there are nCn + nCn-1 + … + nC1 combinations.
[Read More]
Concatenate N strings with good optimization
You have n strings with their lengths. You are given an add(string s1,string s2) which would concatenate the string s2 with s1 and return s3. Optimize the cost of concatenation of all these strings into one big string.
Eg: 1,3,2 are the lengths of given strings.
1+3=4
4+2=6
total cost=10
Optimize this total cost?
So here goes the algo:
Make a min heap out of the elements given.
Pop the smallest and the second smallest, add them and again insert that back to the heap.
[Read More]
Some Stack Question
1)How do you implement 2 stacks using only one array.Your stack routines should not indicate an overflow unless every slot in the array is used?
**Solution:**given an Array,start the first stack S1 from left end and other stack S2 from the right end.while S1 gets grows towards right ,S2 grows towards left. (By the way we can implement n stacks in 1 array, eg. of 3 stacks in 1 array is given in question 3 below)
[Read More]