C#: Reverse the words in sentence - c#

Below code if for reverse word in a sentence, the word sequence in the sentence will be same but the word will be reversed
using System;
using System.Text;
namespace reverse_a_string
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a string: ");
string S = Console.ReadLine();
string[] sep = S.Split(" ");
StringBuilder Wrev = new StringBuilder(); //Word reverse
StringBuilder Srev = new StringBuilder(); //Sentence reverse
for (int j=0;j<sep.Length;j++)
{
for (int i = sep[j].Length - 1; i >= 0; i--)
{
Wrev.Append(sep[j][i]);
}
Srev.Append(Wrev);
Wrev.Clear();
Wrev.Append(" ");
}
Console.WriteLine(Srev);
}
}
}

For simple text, you can just use Split, Reverse, Concat and Join
var words = Console.ReadLine()
.Split()
.Select(x => string.Concat(x.Reverse()));
Console.WriteLine(string.Join(" ", words));
Output
Enter a string: asd sdf dfg fgh
dsa fds gfd hgf
For complicated Unicode, you will need to be more precise in the grouping of characters. However, you can take advantage of GetTextElementEnumerator
Returns an enumerator that iterates through the text elements of a
string.
Given
public static IEnumerable<string> ToElements(this string source)
{
var enumerator = StringInfo.GetTextElementEnumerator(source);
while (enumerator.MoveNext())
yield return enumerator.GetTextElement();
}
Usage
var words = Console.ReadLine()
.Split()
.Select(x => string.Concat(x.ToElements().Reverse()));

static void Main(string[] args)
{
//method 1
Console.Write("Enter a string: ");
string sentence = Console.ReadLine();
string[] words = sentence.Split(" ");
StringBuilder destination = new StringBuilder(); //Sentence reverse
foreach (string word in words)
{
destination.Append(string.Concat(new string(word.Reverse().ToArray()), " "));
}
Console.WriteLine(destination);
//method 2 but need import System.Linq namespace.
var reversedWords = string.Join(" ", sentence.Split(' ').Select(x => new String(x.Reverse().ToArray())));
Console.WriteLine(reversedWords);
}

Let's start from definition; assuming that
Word is a non-empty sequence of letters and apostrophes
we can implement a simple solution with a help of regular expressions (and a pinch of Linq - Reverse()): all we have to do is to Replace each word with its Reversed representation.
Code:
using System.Linq;
using System.Text.RegularExpressions;
...
private static string WordReverse(string value) =>
Regex.Replace(value ?? "", #"[\p{L}_]+", m => string.Concat(m.Value.Reverse()));
Demo:
string[] tests = new string[] {
"Text (на русском)",
"Simple test.",
"Another \"text\": punctuation!"
};
Console.Write(string.Join(Environment.NewLine, tests
.Select(test => $"{test,-30} => {WordReverse(test)}")));
Outcome:
Text (на русском) => txeT (ан мокссур)
Simple test. => elpmiS tset.
Another "text": punctuation! => rehtonA "txet": noitautcnup!

Related

Breaking Format and String provided into a list of string array

I have a format( data Type - string ) say of the form {1}.{0}.{2}#xyz.com. Also, I have a string following the same format say bht.aay.ccch#xyz.com. How can I break the string into the format provided into a list or a string array or any other data structure ( Number provided in the format is the index where the corresponding strings should be stored). Delimiter can be anything like , or /
Example :-
1) Format - {0}.{1}#xyz.com
String - name0.name1#xyz.com
String array[]= { "name0", "name1"}
2) Format - {1}.{0}.{2}#xyz.com
String - pos0.pos1.pos2#xyz.com
String array[]= { "pos1", "pos0", "pos2"}
3) Format - {0}.{1}
String - name0.name1
String array[] = { "pos0", "pos1"}
using System;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConAppCore
{
class Program
{
static void Main(string[] args)
{
Test("{0}.{1}#xyz.com", "name0.name1#xyz.com");
Test("{1}.{0}.{2}#xyz.com", "pos0.pos1.pos2#xyz.com");
Test("{0}.{1}", "name0.name1");
}
static void Test(string format, string input)
{
string pattern = CreatePattern(format);
var match = Regex.Match(input, pattern);
var array = match.Groups
.OfType<Group>()
.Skip(1)
.OrderBy(g => g.Name, StringComparer.Ordinal)
.Select(g => g.Value)
.ToArray();
Console.WriteLine(string.Join(", ", array));
}
static string CreatePattern(string format)
{
var sb = new StringBuilder();
var match = Regex.Match(format, #"(.*?)\{(\d+)\}");
int index;
int length;
do
{
sb.Append(Regex.Escape(match.Groups[1].Value));
sb.Append("(?'group").Append(match.Groups[2].Value).Append(#"'\w+)");
index = match.Index;
length = match.Length;
match = match.NextMatch();
} while (match.Success);
sb.Append(Regex.Escape(format.Substring(index + length)));
return sb.ToString();
}
}
}
Try following :
string[] inputs = { "name0.name1#xyz.com", "name0.name1#xyz.com", "name0.name1" };
string pattern = "(?'path'.*)#(?'site'.*)";
foreach(string input in inputs)
{
Match match = Regex.Match(input,pattern);
string[] paths = match.Groups["path"].Value.Split(new char[] {'.'}).ToArray();
string site = match.Groups["site"].Value;
Console.WriteLine("Path : '{0}', Site : '{1}'", string.Join("-", paths), site);
}
Console.ReadLine();

Repeat substrings N times

I receive series of strings followed by non-negative numbers, e.g. "a3". I have to print on the console each string repeated N times (uppercase) where N is a number in the input. In the example, the result: "AAA". As you see, I have tried to get the numbers from the input and I think it's working fine. Can you help me with the repeating?
string input = Console.ReadLine();
//input = "aSd2&5s#1"
MatchCollection matched = Regex.Matches(input, #"\d+");
List<int> repeatsCount = new List<int>();
foreach (Match match in matched)
{
int repeatCount = int.Parse(match.Value);
repeatsCount.Add(repeatCount);
}
//repeatsCount: [2, 5, 1]
//expected output: ASDASD&&&&&S# ("aSd" is converted to "ASD" and repeated twice;
// "&" is repeated 5 times; "s#" is converted to "S#" and repeated once.)
For example, if we have "aSd2&5s#1":
"aSd" is converted to "ASD" and repeated twice; "&" is repeated 5 times; "s#" is converted to "S#" and repeated once.
Let the pattern include two groups: value to repeat and how many times to repeat:
#"(?<value>[^0-9]+)(?<times>[0-9]+)"
Then we can operate with these groups, say, with a help of Linq:
string source = "aSd2&5s#1";
string result = string.Concat(Regex
.Matches(source, #"(?<value>[^0-9]+)(?<times>[0-9]+)")
.OfType<Match>()
.SelectMany(match => Enumerable // for each match
.Repeat(match.Groups["value"].Value.ToUpper(), // repeat "value"
int.Parse(match.Groups["times"].Value)))); // "times" times
Console.Write(result);
Outcome:
ASDASD&&&&&S#
Edit: Same idea without Linq:
StringBuilder sb = new StringBuilder();
foreach (Match match in Regex.Matches(source, #"(?<value>[^0-9]+)(?<times>[0-9]+)")) {
string value = match.Groups["value"].Value.ToUpper();
int times = int.Parse(match.Groups["times"].Value);
for (int i = 0; i < times; ++i)
sb.Append(value);
}
string result = sb.ToString();
You can extract substring and how often it should be repeated with this regex:
(?<content>.+?)(?<count>\d+)
Now you can use a StringBuilder to create output string. Full code:
var input = "aSd2&5s#1";
var regex = new Regex("(?<content>.+?)(?<count>\\d+)");
var matches = regex.Matches(input).Cast<Match>();
var sb = new StringBuilder();
foreach (var match in matches)
{
var count = int.Parse(match.Groups["count"].Value);
for (var i = 0; i < count; ++i)
sb.Append(match.Groups["content"].Value.ToUpper());
}
Console.WriteLine(sb.ToString());
Output is
ASDASD&&&&&S#
Another solution without LINQ
i tried to keep the solution so it would be similar to yours
string input = "aSd2&5s#1";
var matched = Regex.Matches(input, #"\d+");
var builder = new StringBuilder();
foreach (Match match in matched)
{
string stingToDuplicate = input.Split(Char.Parse(match.Value))[0];
input = input.Replace(stingToDuplicate, String.Empty).Replace(match.Value, String.Empty);
for (int i = 0; i < Convert.ToInt32(match.Value); i++)
{
builder.Append(stingToDuplicate.ToUpper());
}
}
and finally Console.WriteLine(builder.ToString());
which result ASDASD&&&&&S#
My solution is same as others with slight differences :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication107
{
class Program
{
static void Main(string[] args)
{
string input = "aSd2&5s#1";
string pattern1 = #"[a-zA-z#&]+\d+";
MatchCollection matches = Regex.Matches(input, pattern1);
string output = "";
foreach(Match match in matches.Cast<Match>().ToList())
{
string pattern2 = #"(?'string'[^\d]+)(?'number'\d+)";
Match match2 = Regex.Match(match.Value, pattern2);
int number = int.Parse(match2.Groups["number"].Value);
string str = match2.Groups["string"].Value;
output += string.Join("",Enumerable.Repeat(str.ToUpper(), number));
}
Console.WriteLine(output);
Console.ReadLine();
}
}
}
Very simple program. No linq nothing, simple string and for loop.
string input = "aSd2&5s#1";
char[] inputArray = input.ToCharArray();
string output = "";
string ab = "";
foreach (char c in inputArray)
{
int x;
string y;
if(int.TryParse(c.ToString(), out x))
{
string sb = "";
ab = ab.ToUpper();
for(int i=0;i<b;i++)
{
sb += ab;
}
ab = "";
output += sb;
}
else
{
ab += c;
}
}
if(!string.IsNullOrEmpty(ab))
{
output += ab.ToUpper();
}
Console.WriteLine(output);
Hope it helps.

Scanning through string array

I am new to C#.
How do I take user input and save each letter I type as a separate entity in an array.
I want to scan through the array and find a particular sequence of characters to act as a starting point.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter Nitrogenous base sequence");
string[] sequence = new string[]{Console.ReadLine()};
foreach(string a in sequence)
{
if(a=="TATAAT")
{
Console.WriteLine("YAAY");
}
else
{
Console.WriteLine("NO");
You can think of a string as a glorified character array.
var pattern = "TATAAT";
var input = Console.ReadLine();
var patternIndex = input.IndexOf(pattern);
if(patternIndex >= 0) {
var answer = input.Substring(patternIndex + pattern.Length, 4);
Console.WriteLine("YAAY: " + answer);
} else {
Console.WriteLine("NO");
}
You can do this using LINQ, this will get the input from user then save each character as string into an array:
string[] sequence = Console.ReadLine().Select(x => x.ToString()).ToArray();
If you don't want to include spaces:
string[] sequence = Console.ReadLine().Where(x => !char.IsWhiteSpace(x))
.Select(x => x.ToString())
.ToArray();

remove duplicate words from string in C#

here is my code:
class Program
{
static void Main(string[] args)
{
string sentence = string.Empty;
sentence = Console.ReadLine();
string[] sent = sentence.Split(' ');
//to be sorted alphabetically
var x =
from k in sent
orderby k
select k;
foreach (string s in x)
{
Console.WriteLine(s.ToLower());
}
Console.ReadLine();
}
}
is there any method to find and remove duplicate words or I should make my own method?
You could use Linq's Distinct extension method:
var sent = sentence.Split(' ').Distinct();
You can also use this to ignore the case of strings when comparing them—e.g. "WORD" and "word" would be considered duplicates:
var sent = sentence.Split(' ').Distinct(StringComparer.CurrentCultureIgnoreCase);
Use System.Linq Distinct:
foreach (string s in x.Distinct())
Use Distinct:
foreach (string s in x.Distinct())
{
Console.WriteLine(s.ToLower());
}
This should do everything you're asking:
class Program
{
static void Main(string[] args)
{
string sentence = string.Empty;
sentence = Console.ReadLine();
var sent = sentence
.Split(' ')
.Distinct()
.OrderBy(x => x);
foreach (string s in sent)
{
Console.WriteLine(s.ToLower());
}
Console.ReadLine();
}
}
Hope it helps!

How can I convert text to Pascal case?

I have a variable name, say "WARD_VS_VITAL_SIGNS", and I want to convert it to Pascal case format: "WardVsVitalSigns"
WARD_VS_VITAL_SIGNS -> WardVsVitalSigns
How can I make this conversion?
You do not need a regular expression for that.
var yourString = "WARD_VS_VITAL_SIGNS".ToLower().Replace("_", " ");
TextInfo info = CultureInfo.CurrentCulture.TextInfo;
yourString = info.ToTitleCase(yourString).Replace(" ", string.Empty);
Console.WriteLine(yourString);
Here is my quick LINQ & regex solution to save someone's time:
using System;
using System.Linq;
using System.Text.RegularExpressions;
public string ToPascalCase(string original)
{
Regex invalidCharsRgx = new Regex("[^_a-zA-Z0-9]");
Regex whiteSpace = new Regex(#"(?<=\s)");
Regex startsWithLowerCaseChar = new Regex("^[a-z]");
Regex firstCharFollowedByUpperCasesOnly = new Regex("(?<=[A-Z])[A-Z0-9]+$");
Regex lowerCaseNextToNumber = new Regex("(?<=[0-9])[a-z]");
Regex upperCaseInside = new Regex("(?<=[A-Z])[A-Z]+?((?=[A-Z][a-z])|(?=[0-9]))");
// replace white spaces with undescore, then replace all invalid chars with empty string
var pascalCase = invalidCharsRgx.Replace(whiteSpace.Replace(original, "_"), string.Empty)
// split by underscores
.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries)
// set first letter to uppercase
.Select(w => startsWithLowerCaseChar.Replace(w, m => m.Value.ToUpper()))
// replace second and all following upper case letters to lower if there is no next lower (ABC -> Abc)
.Select(w => firstCharFollowedByUpperCasesOnly.Replace(w, m => m.Value.ToLower()))
// set upper case the first lower case following a number (Ab9cd -> Ab9Cd)
.Select(w => lowerCaseNextToNumber.Replace(w, m => m.Value.ToUpper()))
// lower second and next upper case letters except the last if it follows by any lower (ABcDEf -> AbcDef)
.Select(w => upperCaseInside.Replace(w, m => m.Value.ToLower()));
return string.Concat(pascalCase);
}
Example output:
"WARD_VS_VITAL_SIGNS" "WardVsVitalSigns"
"Who am I?" "WhoAmI"
"I ate before you got here" "IAteBeforeYouGotHere"
"Hello|Who|Am|I?" "HelloWhoAmI"
"Live long and prosper" "LiveLongAndProsper"
"Lorem ipsum dolor..." "LoremIpsumDolor"
"CoolSP" "CoolSp"
"AB9CD" "Ab9Cd"
"CCCTrigger" "CccTrigger"
"CIRC" "Circ"
"ID_SOME" "IdSome"
"ID_SomeOther" "IdSomeOther"
"ID_SOMEOther" "IdSomeOther"
"CCC_SOME_2Phases" "CccSome2Phases"
"AlreadyGoodPascalCase" "AlreadyGoodPascalCase"
"999 999 99 9 " "999999999"
"1 2 3 " "123"
"1 AB cd EFDDD 8" "1AbCdEfddd8"
"INVALID VALUE AND _2THINGS" "InvalidValueAnd2Things"
First off, you are asking for title case and not camel-case, because in camel-case the first letter of the word is lowercase and your example shows you want the first letter to be uppercase.
At any rate, here is how you could achieve your desired result:
string textToChange = "WARD_VS_VITAL_SIGNS";
System.Text.StringBuilder resultBuilder = new System.Text.StringBuilder();
foreach(char c in textToChange)
{
// Replace anything, but letters and digits, with space
if(!Char.IsLetterOrDigit(c))
{
resultBuilder.Append(" ");
}
else
{
resultBuilder.Append(c);
}
}
string result = resultBuilder.ToString();
// Make result string all lowercase, because ToTitleCase does not change all uppercase correctly
result = result.ToLower();
// Creates a TextInfo based on the "en-US" culture.
TextInfo myTI = new CultureInfo("en-US",false).TextInfo;
result = myTI.ToTitleCase(result).Replace(" ", String.Empty);
Note: result is now WardVsVitalSigns.
If you did, in fact, want camel-case, then after all of the above, just use this helper function:
public string LowercaseFirst(string s)
{
if (string.IsNullOrEmpty(s))
{
return string.Empty;
}
char[] a = s.ToCharArray();
a[0] = char.ToLower(a[0]);
return new string(a);
}
So you could call it, like this:
result = LowercaseFirst(result);
Single semicolon solution:
public static string PascalCase(this string word)
{
return string.Join("" , word.Split('_')
.Select(w => w.Trim())
.Where(w => w.Length > 0)
.Select(w => w.Substring(0,1).ToUpper() + w.Substring(1).ToLower()));
}
Extension method for System.String with .NET Core compatible code by using System and System.Linq.
Does not modify the original string.
.NET Fiddle for the code below
using System;
using System.Linq;
public static class StringExtensions
{
/// <summary>
/// Converts a string to PascalCase
/// </summary>
/// <param name="str">String to convert</param>
public static string ToPascalCase(this string str){
// Replace all non-letter and non-digits with an underscore and lowercase the rest.
string sample = string.Join("", str?.Select(c => Char.IsLetterOrDigit(c) ? c.ToString().ToLower() : "_").ToArray());
// Split the resulting string by underscore
// Select first character, uppercase it and concatenate with the rest of the string
var arr = sample?
.Split(new []{'_'}, StringSplitOptions.RemoveEmptyEntries)
.Select(s => $"{s.Substring(0, 1).ToUpper()}{s.Substring(1)}");
// Join the resulting collection
sample = string.Join("", arr);
return sample;
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("WARD_VS_VITAL_SIGNS".ToPascalCase()); // WardVsVitalSigns
Console.WriteLine("Who am I?".ToPascalCase()); // WhoAmI
Console.WriteLine("I ate before you got here".ToPascalCase()); // IAteBeforeYouGotHere
Console.WriteLine("Hello|Who|Am|I?".ToPascalCase()); // HelloWhoAmI
Console.WriteLine("Live long and prosper".ToPascalCase()); // LiveLongAndProsper
Console.WriteLine("Lorem ipsum dolor sit amet, consectetur adipiscing elit.".ToPascalCase()); // LoremIpsumDolorSitAmetConsecteturAdipiscingElit
}
}
var xs = "WARD_VS_VITAL_SIGNS".Split('_');
var q =
from x in xs
let first_char = char.ToUpper(x[0])
let rest_chars = new string(x.Skip(1).Select(c => char.ToLower(c)).ToArray())
select first_char + rest_chars;
Some answers are correct but I really don't understand why they set the text to LowerCase first, because the ToTitleCase will handle that automatically:
var text = "WARD_VS_VITAL_SIGNS".Replace("_", " ");
TextInfo textInfo = CultureInfo.CurrentCulture.TextInfo;
text = textInfo.ToTitleCase(text).Replace(" ", string.Empty);
Console.WriteLine(text);
You can use this:
public static string ConvertToPascal(string underScoreString)
{
string[] words = underScoreString.Split('_');
StringBuilder returnStr = new StringBuilder();
foreach (string wrd in words)
{
returnStr.Append(wrd.Substring(0, 1).ToUpper());
returnStr.Append(wrd.Substring(1).ToLower());
}
return returnStr.ToString();
}
This answer understands that there are Unicode categories which can be tapped while processing the text to ignore the connecting characters such as - or _. In regex parlance it is \p (for category) then the type which is {Pc} for punctuation and connector type character; \p{Pc} using our MatchEvaluator which is kicked off for each match within a session.
So during the match phase, we get words and ignore the punctuations, so the replace operation handles the removal of the connector character. Once we have the match word, we can push it down to lowercase and then only up case the first character as the return for the replace:
public static class StringExtensions
{
public static string ToPascalCase(this string initial)
=> Regex.Replace(initial,
// (Match any non punctuation) & then ignore any punctuation
#"([^\p{Pc}]+)[\p{Pc}]*",
new MatchEvaluator(mtch =>
{
var word = mtch.Groups[1].Value.ToLower();
return $"{Char.ToUpper(word[0])}{word.Substring(1)}";
}));
}
Usage:
"TOO_MUCH_BABY".ToPascalCase(); // TooMuchBaby
"HELLO|ITS|ME".ToPascalCase(); // HelloItsMe
See Word Character in Character Classes in Regular Expressions
Pc Punctuation, Connector. This category includes ten characters, the
most commonly used of which is the LOWLINE character (_), u+005F.
If you did want to replace any formatted string into a pascal case then you can do
public static string ToPascalCase(this string original)
{
string newString = string.Empty;
bool makeNextCharacterUpper = false;
for (int index = 0; index < original.Length; index++)
{
char c = original[index];
if(index == 0)
newString += $"{char.ToUpper(c)}";
else if (makeNextCharacterUpper)
{
newString += $"{char.ToUpper(c)}";
makeNextCharacterUpper = false;
}
else if (char.IsUpper(c))
newString += $" {c}";
else if (char.IsLower(c) || char.IsNumber(c))
newString += c;
else if (char.IsNumber(c))
newString += $"{c}";
else
{
makeNextCharacterUpper = true;
newString += ' ';
}
}
return newString.TrimStart().Replace(" ", "");
}
Tested with strings
I|Can|Get|A|String
ICan_GetAString
i-can-get-a-string
i_can_get_a_string
I Can Get A String
ICanGetAString
I found this gist useful after adding a ToLower() to it.
"WARD_VS_VITAL_SIGNS"
.ToLower()
.Split(new [] {"_"}, StringSplitOptions.RemoveEmptyEntries)
.Select(s => char.ToUpperInvariant(s[0]) + s.Substring(1, s.Length - 1))
.Aggregate(string.Empty, (s1, s2) => s1 + s2)

Categories

Resources