Click here to Skip to main content
16,022,875 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
write a program that parses a sentence and replaces each word with the following: first letter, number of distinct characters between first and last character, and last letter. For example, Smooth would become S3h. Words are separated by spaces or non-alphabetic characters and these separators should be maintained in their original form and location in the answer.

can u please provide the solution for this?

What I have tried:

C#
static void Main(string[] args)
{
    //String s = "smooth is good";
    //string s1 = new string(s.Distinct().ToArray());
  //  int k = s1.Length;
   // int c = k / 2;

    //Console.WriteLine(s1);
    // Console.WriteLine("the representation of " + s + " is : " + s.First() + "" + "" + c + s.Last());
    string str = "Smooth is good";
    string[] strArr = null;
    int k = str.Length;
     int c = k / 2;
    //int count = 0;

    if (str.Contains(" "))
    {
        char[] splitchar = { ' ' };
        strArr = str.Split(splitchar);

        Console.WriteLine("the representation of " + str + " is : " + str.First() + "" + "" + c + str.Last());
    }
    Console.ReadLine();
}
Posted
Updated 10-Jan-22 14:13pm
v2
Comments
Patrice T 23-Mar-16 2:36am    
HomeWork !
There is nothing in your code.
What about starting some real work ?
Member 123CC 23-Mar-16 2:46am    
can i know how it will

C#
class Program
   {
      public void TheCountOfDistinctChars(string[] args)
       {

       }
       static void Main(string[] args)
       {
           //string str = "Smooth is good";
           string[] strArr = new[] { "smooth","is", "good" };
           List<string> newsentence = new List<string>();
           //string line = string.Join(" ", newsentence.ToArray());
           //Console.WriteLine(line);
           StringBuilder builder = new StringBuilder();
           foreach (string word in strArr)
           {
               newsentence.Add(word);

               string newword = null;
               if (word.Length > 2)
               {
                   int distinctCount = 0;
                   int k = word.Length;
                   int samecharcount = 0;
                   int count = 0;

                   for (int i = 1; i < k - 2; i++)
                   {

                       if (word.ElementAt(i) != word.ElementAt(i + 1))
                       {

                           count++;

                       }
                       else
                       {
                           samecharcount++;

                       }
                   }
                   distinctCount = count + samecharcount;

                   //distinctCount = word.Distinct().Count();
                   //}
                       char firstChar = word[0];
                       char lastChar = word[word.Length - 1];
                       newword = String.Concat(firstChar, distinctCount.ToString(), lastChar);
                   //newsentence.Add(newword);
                   Console.Write(newword+" ");
                }

               else
               {

                   Console.Write(builder.Append(word).Append(" "));
               }

           }
           Console.ReadKey();
       }
   }
 
Share this answer
 
Comments
Member 123CC 28-Mar-16 6:27am    
at last i got solution for this
RedDk 28-Mar-16 18:28pm    
I have question
You're very close to solve your problem... I'd suggest to improve Split function this way:
C#
char[] splitchar = new char[] {' '};
string[] strArr = str.Split(splitchar, StringSplitOptions.RemoveEmptyEntries);

Now, you have to loop through the array of words:
C#
foreach (string word in strArr)
{
   Console.WriteLine("{0}", word);
   //word.ToArray() -> returns chars in word
}

All you need to do is to write function which will return the number of distinct chars in a word, then to join each element into word.
C#
string finalStr = String.Concat(firstChar, TheCountOfDistinctChars.ToString(), lastChar);


Try!
 
Share this answer
 
after the "strArr = str..." you could add something like:
C#
List<string> newsentence = new List<string>();

foreach (string word in strArr)
 {
   if (word.Length > 2)
   {
     //ignore 2-letter words
     string newword = null;
     //Random rnd = new Random();
     //int random = rnd.Next(1, 10);
     int random = word.Length - 2; //length minus first and last char
     char frst = word[0];
     char last = word[word.Length - 1];
     newword = String.Concat(frst, random.ToString(), last);
     newsentence.Add(newword);
   }
   else
   {
     newsentence.Add(word);
   }
 }


You only need to join 'newsentence'..
[edit]: sorry, didn't read the question properly, 'random' should be number of chars inbetween.. so change as shown
 
Share this answer
 
v4
Comments
Patrice T 23-Mar-16 3:09am    
Looks like the result will be fun.
I would like to see.
Member 123CC 23-Mar-16 5:18am    
i didnt understand the logic is it will give the correct output
F-ES Sitecore 23-Mar-16 5:54am    
You shouldn't do people's homework for them. Giving them the solution doesn't help anyone.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900