String manipulation - Practical real time asp.net interview question asked in an interview

Click here for all C# Interview Questions
Click here for all WCF Interview Questions
Click here for all SQL Server Interview Questions
Click here for all ASP.NET Interview Questions

Click here for Written Test or practical ASP.NET Interview Questions asked in MNC's

Questions:
Please write a sample program that parses the string into a series of substrings where the delimiter between the substrings is "^*!%~" and then reassembles the strings and delimiters into a single new string where each of the substrings is in the reverse order from the original string. The method must return the final string.

Original String:
Token A^*!%~Token B^*!%~Token C^*!%~Token D^*!%~Token E

Output String:
Token E^*!%~Token D^*!%~Token C^*!%~Token B^*!%~Token A

The code sample below shows how to solve the above question:
using System;
using System.Text;
namespace GenericsSample
{
class Program
{
static void Main()
{
string strOriginalString = "Token A^*!%~Token B^*!%~Token C^*!%~Token D^*!%~Token E";
string[] strSeperator = new string[1];
strSeperator[0] = "^*!%~";

string[] strArrayIndividualStrings = strOriginalString.Split(strSeperator, StringSplitOptions.RemoveEmptyEntries);

int intLengthOfStringArray = strArrayIndividualStrings.Length;

StringBuilder sbOutputString = new StringBuilder();
for (int i = (intLengthOfStringArray - 1); i >= 0; i--)
{
sbOutputString.Append(strArrayIndividualStrings[i] + strSeperator[0]);
}
Console.WriteLine("Original String : " + strOriginalString);
Console.WriteLine("Output String : " + sbOutputString.ToString());
Console.ReadLine();
}
}
}

Explanation of the above sample program:
1.
We take the original string into a string variable strOriginalString. I named this variable as strOriginalString. str indicates that the variable is of string datatype. This will give a good impression to the person who reviews your code bcos you are following the coding standards.

2. I then store the string delimiter "^*!%~" in strSeperator variable. strSeperator is of string array data type. This is bcos the split function expects string array or character array as seprator.

3. I then split the strOriginalString into a string array using the split function.

4. I created a variable sbOutputString to store the Output string. sbOutputString data type is StringBuilder.

5. I then loop thru the array from the highest index to 0 and retrieve the individual strings and append to the sbOutputString. As the output string is changing as we loop thru the array it is good to use StringBuilder rather than System.String. Strings of type System.Text.StringBuilder are mutable where as strings of type System.String are immutable. If a string is manipulated many times always use StringBuilder over System.String.

6. Two good things to remember from this example are, follow the coding standards in naming the variables and always use StringBuilder class over Strings where we manipulate a particular string many times.

Click here for all C# Interview Questions
Click here for all SQL Server Interview Questions
Click here for all WCF Interview Questions
Click here for all ASP.NET Interview Questions


Click here for Written Test or practical ASP.NET Interview Questions asked in MNC's

26 comments:

  1. I have been asked this kind of question for an asp.net interview.But i don't think this question can be solved using the pre defined string.split function. I think it needs some code from scratch.

    ReplyDelete
  2. This program is working fine. I checked it.

    ReplyDelete
  3. The way you have it would include an extra "strSeparater" in the sbOutputString.
    It needs to include this:
    int count = sbOutputString.Length;
    sbOutputString.Remove(count -5, 5);

    ReplyDelete
  4. Very Very good, But a small mistake, it gives output like
    Token E^*!%~Token D^*!%~Token C^*!%~Token B^*!%~Token A E^*!%~

    To correct above mistake
    int i;

    for (i = (intLengthOfStringArray - 1); i > 0; i--)
    {
    sbOutputString.Append(strArrayIndividualStrings[i] + strSeperator[0]);
    }

    sbOutputString.Append(strArrayIndividualStrings[i]);

    ReplyDelete
    Replies
    1. in the last line we have to write 0 instead of i.

      sbOutputString.Append(strArrayIndividualStrings[0]);

      Delete
  5. using System;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    string input = "Token A^*!%~Token B^*!%~Token C^*!%~Token D^*!%~Token E";
    string[] delimiter = {"^*!%~"};
    string[] temp = input.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
    Array.Reverse(temp);
    string output = String.Join(delimiter[0], temp);
    Console.WriteLine(output);
    }
    }
    }

    ReplyDelete
    Replies
    1. wow super boss easy method...

      Delete
    2. But most of company not allow for predefined function to use in your program Like array.reverse etc.

      Delete
  6. string original = "Token A^*!%~Token B^*!%~Token C^*!%~Token D^*!%~Token E";
    char[] separator = {'^','*','!','%','~' };
    string[] strparts;
    string NewString = string.Empty;
    strparts = original.Split(separator,StringSplitOptions.RemoveEmptyEntries);
    for (int i = strparts.Length-1; i > 0 ; i--)
    {
    NewString = NewString + strparts[i] + "^*!%~";
    }
    NewString = NewString.TrimEnd(separator);
    return NewString;

    ReplyDelete
  7. How would I do this if I were asked to write my own String.Split() method? I wrote a solution, but it is rather clunky.

    ReplyDelete
  8. since "^*!%~" is constant, we can use regex to split it.

    ReplyDelete
  9. string delimiter="^*!%~";
    string inpupt="Token A^*!%~Token B^*!%~Token C^*!%~Token D^*!%~Token E";
    string []lst=inpupt.Split(delimiter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
    System.Text.StringBuilder sbResult = new System.Text.StringBuilder();
    for (int i = lst.Length-1; i >= 0; --i)
    {
    if (i == 0)
    delimiter = string.Empty;

    sbResult.Append(lst[i]+delimiter);
    }
    Console.WriteLine(sbResult.ToString());

    ReplyDelete
  10. Pretty good idea, Thanks!!!

    ReplyDelete
  11. namespace ConsoleApplication3
    {
    class Test
    {
    public string SplitChar(string Value)
    {
    string[] val = new string[10];
    string[] val1 = new string[10];
    char c = new char();
    c = '^';
    val = Value.Split(c);
    for (int len = 0; len < val.Length; len++)
    {
    val1[len] = val[len].Substring(val[len].ToString().Length - 1, 1);
    }
    int j = 0;
    string res = "";
    for (int len = val.Length; len >= 0; len--)
    {
    if (val1[len] != null)
    {
    string cc = val[j].Insert(val[j].ToString().Length - 1, val1[len].ToString());
    cc = cc.Substring(0, cc.Length - 1);
    cc = cc + "^";
    res = res + cc;
    string a = val[j];
    string b = val1[len];
    j = j + 1;
    }

    }
    res = res.Substring(0, res.Length - 1);
    return res;
    }
    }

    class Program
    {
    static void Main(string[] args)
    {
    Test t1 = new Test();
    string res = t1.SplitChar("A^*!%~Token B^*!%~Token C^*!%~Token D^*!%~Token E");
    Console .WriteLine ("A^*!%~Token B^*!%~Token C^*!%~Token D^*!%~Token E");
    Console.WriteLine(res);
    Console.ReadLine();
    }
    }


    }

    ReplyDelete
  12. i like tis tips.........

    ReplyDelete
  13. public static string reversString(string originalString, string seperater)
    {
    string returnString = "";
    try
    {
    string[] splitString = originalString.Split(seperater.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
    StringBuilder sbReversString=new StringBuilder();
    for (int i = splitString.Length - 1; i >= 0; i--)
    {
    if (i == 0)
    seperater = string.Empty;

    returnString = returnString + splitString[i] + seperater;
    }
    }
    catch
    {
    returnString = originalString;
    }
    return returnString;
    }

    ReplyDelete
  14. //using LINQ
    using System;
    using System.Text;
    using System.Linq;
    namespace GenericsSample
    {
    class Program
    {
    static void Main()
    {
    const string strOriginalString = "Token A^*!%~Token B^*!%~Token C^*!%~Token D^*!%~Token E";
    var strSeperator = new string[1];
    strSeperator[0] = "^*!%~";

    var output = string.Join("^*!%~",
    strOriginalString.Split(strSeperator, StringSplitOptions.RemoveEmptyEntries)
    .OrderByDescending(x => x)
    .ToArray());

    Console.WriteLine("Original String : " + strOriginalString);
    Console.WriteLine("Output String : " + output);
    Console.ReadLine();
    }
    }
    }

    ReplyDelete
  15. string a = "Token A^*!%~Token B^*!%~Token C^*!%~Token D^*!%~Token E";
    string[] sep = new string[1];
    sep[0] = "^*!%~";
    string[] ap= a.Split(sep, StringSplitOptions.RemoveEmptyEntries);
    StringBuilder output=new StringBuilder();
    for(int i=ap.Length-1;i>=0;i--)
    {
    if (i != 0)
    {
    output.Append(ap[i] + sep[0]);
    }
    else
    {
    output.Append(ap[i]);
    }
    }
    Response.Write(output);

    ReplyDelete
  16. string original = "A^*!%~Token B^*!%~Token C^*!%~Token D^*!%~Token E";
    string[] separator = new String[1];
    separator[0] = "^*!%~Token";
    TextBox1.Text = original;
    string[] arr = original.Split(separator, StringSplitOptions.RemoveEmptyEntries);
    Array.Reverse(arr);

    StringBuilder sbOutputString = new StringBuilder();
    for (int i = 0; i<=arr.Length-1 ; i++)
    {

    if (i == arr.Length - 1)
    {
    sbOutputString.Append(arr[i]);
    }
    else
    {
    sbOutputString.Append(arr[i] + separator[0]);
    }
    }
    TextBox2.Text = sbOutputString.ToString();

    ReplyDelete
  17. using System;
    namespace MY_project
    {
    class Program
    {
    static void Main(string[] args)
    {
    string name = "ROSHAN,NITIN,ASHIH,BUDHA";
    string[] help = name.Split(',');
    Array.Reverse(help);
    string k = string.Join(",", help);
    Console.WriteLine(k);
    Console.Read();
    }
    }
    }

    ReplyDelete
  18. using System;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main()
    {
    string strOriginalString = "Token A^*!%~Token B^*!%~Token C^*!%~Token D^*!%~Token E";
    string[] seperator = new string[1];
    seperator[0] = "^*!%~";
    string[] strArrTemp = strOriginalString.Split(seperator, StringSplitOptions.RemoveEmptyEntries);
    int arrLength = strArrTemp.Length;
    StringBuilder strOutPut = new StringBuilder();
    for (int i = arrLength-1; i >=0; i--)
    {

    strOutPut.Append(strArrTemp[i] + ((i>0)? seperator[0]:null));
    }

    Console.WriteLine(strOriginalString);
    Console.WriteLine(strOutPut.ToString());

    Console.ReadLine();
    }
    }
    }
    -----------------------------------
    Hi guys, way told by roshan khoirom is best one,
    i am gonna add one more way, just for knowledge.

    ReplyDelete
  19. i have following logic for that its working but i don't know is it right way or not can u any one suggest me:-


    static void Main()
    {
    string strOriginalString = "Token A^*!%~Token B^*!%~Token C^*!%~Token D^*!%~Token E";
    string strOutput = "";
    int temp = strOriginalString[strOriginalString.Length - 1];
    for (int i = 0; i < strOriginalString.Length; i++)
    {
    if (strOriginalString[i] == ' ')
    {
    strOutput += " " + (char)temp;
    i++;
    temp--;
    }
    else
    strOutput += strOriginalString[i];

    }
    Console.Write(strOutput);
    Console.ReadLine();
    }

    ReplyDelete
  20. static void Main()
    {
    string strOriginalString = "Token A^*!%~Token B^*!%~Token C^*!%~Token D^*!%~Token E";
    string stroutputString = strOriginalString.ToString();
    stroutputString = stroutputString.Replace("A", "#1");
    stroutputString = stroutputString.Replace("B", "#2");
    stroutputString = stroutputString.Replace("D", "#3");
    stroutputString = stroutputString.Replace("E", "#4");
    stroutputString = stroutputString.Replace("#1", "E");
    stroutputString = stroutputString.Replace("#2", "D");
    stroutputString = stroutputString.Replace("#3", "B");
    stroutputString = stroutputString.Replace("#4", "A");
    Console.Write("OriginalString:-" + strOriginalString + "\r\n");
    Console.Write("OutputString:-" + stroutputString);
    Console.ReadLine();
    }

    ReplyDelete
  21. For loop should be tweaked to get the desired output.

    for (int i = (intLengthOfStringArray - 1); i >= 0; i--)
    {
    sbOutputString.Append(strArrayIndividualStrings[i]);
    if (i != 0)
    {
    sbOutputString.Append(strSeperator[0]);
    }
    }

    ReplyDelete

If you are aware of any other asp.net questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us