Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C# program is upper-casing wrong letters. I want the program to uppercase the fi

ID: 3772424 • Letter: C

Question

C# program is upper-casing wrong letters.

I want the program to uppercase the first letter of each word that a user enters. My program is below, and it is uppercasing all the letters that are not first and leaving the first lower-case. I do not know what is wrong. Please test the solution

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApplication41
{
class Program

{

static void Main(string[] args)

{

Console.WriteLine("Enter a String : ");

var inputSentence = Console.ReadLine();

var regExpr = new System.Text.RegularExpressions.Regex(@"(?<=w)w");

var newString = regExpr.Replace(inputSentence, new MatchEvaluator(m => m.Value.ToUpperInvariant()));

Console.WriteLine(newString);

}

}
}

Explanation / Answer

The Program written was completely correct, but there was just a problem with regular expression specified.

Change the regular expression as below:

var regExpr = new System.Text.RegularExpressions.Regex(@"(^w)|(sw)");

Regular expression specified in this modified line searches for begining of the string denoted by '^' and searchs for any alpha numberic character denoted by 'w' (or) any alphanumeric character preceeded by whitespace denoted by 'sw'

Dr Jack
Hire Me For All Your Tutoring Needs
Quick quotes • Clear explanations • Study support
Chat Now And Get Quote