SRRK
BAN USER1) An empty string.
2) String with only one space. Ex: " ".
3) String with multiple spaces in between words.
4) String with spaces at the starting. Single and multiple.
5) String with spaces at the end. Single and multiple.
6) Combinations of step 3 with 4 and 5.
7) String with alphanumeric.
8) Include special characters in step 7.
9) Strings which are palindromes.
10) String with only one character.
11) String with more than 255 characters.
12) String with combinations of uppercase and lowercase letters.
13) String with only special characters.
14) String with only uppercase and only lowercase letters.
15) String with only spaces. more than 2 spaces.
16) String with ASCII characters and with Unicode characters.
17) String with exactly single spaces in between letters.
18) String containing even number of letters in some words and odd number of letters in others.
class Program
{
public static int FindWordsContainingLetter(string sentence, char chr)
{
char[] sentArray = sentence.ToCharArray();
int j =0;
int cnt = 0;
for (int i = 0; i < sentArray.Length; i++)
{
if (sentArray[i] != ' ')
{
i++;
}
else if (sentArray[i] == ' ' && i - j > 1) //ignore single letters
{
if (FindCharHelper(sentArray, j, i - 1, chr)) //i is sitting at space so decrement before passing
{
cnt++;
}
}
}
return cnt;
}
public static bool FindCharHelper(char[] wordArray, int strt, int end, char chr)
{
bool found = false;
for (int i = strt; i < end; i++)
{
if (wordArray[i] == chr)
{
found = true;
break;
}
}
return found;
}
static void Main(string[] args)
{
FindWordsContainingLetter("Does this string contain words with letter i", 'i');
}
}
}
namespace FirstNonRepeatedCharacter
{
class Program
{
public static char FirstNonRepeatedChar(string str)
{
char ch = ' ';
char[] charArray = str.ToCharArray();
Dictionary<char, int> chrHash = new Dictionary<char, int>();
foreach (char chr in charArray)
{
if (!chrHash.ContainsKey(chr))
{
chrHash[chr] = 0;
}
else
{
chrHash[chr]++;
}
}
foreach (KeyValuePair<char, int> kvp in chrHash)
{
if (kvp.Value == 1)
{
ch = kvp.Key;
break;
}
}
return ch;
}
static void Main(string[] args)
{
FirstNonRepeatedChar("siadjtlsrpqjkrp");
}
}
}
namespace ZigZagReverseString
{
class Program
{
public static void ZigZagReverse(string str)
{
char[] charArray = str.ToCharArray();
int j = 0;
for(int i = 0, k=-1; i< charArray.Length; i++)
{
if(charArray[i] != ' ')
{
i++;
}
else if (charArray[i] == ' ' && ++k % 2 == 0)
{
ReverseHelper(charArray, j, i - 1);
j = i + 1;
}
else
{
j = i + 1;
}
}
string strng = new string(charArray);
}
public static void ReverseHelper(char[] chrArray, int start, int end)
{
while (start < end)
{
char tmp = chrArray[end];
chrArray[end] = chrArray[start];
chrArray[start] = tmp;
start++;
end--;
}
}
static void Main(string[] args)
{
ZigZagReverse("this string should be reversed zigzag");
}
}
}
class Program
{
public static string getDigitsFromPhoneString(string str)
{
char[] pnArray = str.ToCharArray();
StringBuilder strBldr = new StringBuilder();
foreach (char chr in pnArray)
{
int res;
if (chr != '-')
{
if (Int32.TryParse(chr.ToString(), out res))
{
strBldr.Append(chr);
}
else
{
char ch = getDigitfor(chr);
if(ch != ' ')
strBldr.Append(ch);
}
}
}
return strBldr.ToString();
}
public static char getDigitfor(char chr)
{
switch (chr)
{
case 'a':
case 'b':
case 'c':
return '2';
//Similarly for rest characters
default: return ' ';
}
}
static void Main(string[] args)
{
getDigitsFromPhoneString("1800abacba");
}
}
- SRRK June 16, 2013
@Saurabh, I added that because internally the characters of the words would be reversed.
- SRRK June 20, 2013Actually I should have been specific about the number; for ASCII: 255 and for Unicode: 2,147,483,647
That test case was mainly for load/crash/performance testing. Correct me if I am wrong.