Teallaair9
Teallaair9

Reputation: 39

Make every other a-z letter Upper / Lower case, ignoring whitespace

Can somebody tell me what I am doing wrong please? can't seem to get the expected output, i.e. ignore whitespace and only upper/lowercase a-z characters regardless of the number of whitespace characters

my code:

var sentence = "dancing sentence";
var charSentence = sentence.ToCharArray();
var rs = "";
for (var i = 0; i < charSentence.Length; i++)
{
    if (charSentence[i] != ' ')
    {
        if (i % 2 == 0 && charSentence[i] != ' ')
        {
            rs += charSentence[i].ToString().ToUpper();
        }
        else if (i % 2 == 1 && charSentence[i] != ' ')
        {
            rs += sentence[i].ToString().ToLower();
        }
    }
    else
    {
        rs += " ";
    }
}
Console.WriteLine(rs);

Expected output: DaNcInG sEnTeNcE

Actual output: DaNcInG SeNtEnCe

Upvotes: 1

Views: 214

Answers (5)

fubo
fubo

Reputation: 45947

Linq version

var sentence = "dancing sentence";
int i = 0;
string result = string.Concat(sentence.Select(x => { i += x == ' ' ? 0 : 1; return i % 2 != 0 ? char.ToUpper(x) : char.ToLower(x); }));

Sidenote:

please replace charSentence[i].ToString().ToUpper() with char.ToUpper(charSentence[i])

Upvotes: 1

Chandan Kumar
Chandan Kumar

Reputation: 4638

Thanks @Dmitry Bychenko. Best Approach. But i thought as per the OP's (might be a fresher...) mindset, what could be the solution. Here i have the code as another solution.

Lengthy code. I myself don't like but still representing

class Program
{
    static void Main(string[] args)
    {
        var sentence = "dancing sentence large also";
        string newString = string.Empty;
        StringBuilder newStringdata = new StringBuilder();

        string[] arr = sentence.Split(' ');
        for (int i=0; i< arr.Length;i++)
        {
            if (i==0)
            {
                newString = ReturnEvenModifiedString(arr[i]);
                newStringdata.Append(newString);
            }
            else
            {
                if(char.IsUpper(newString[newString.Length - 1]))
                {
                    newString = ReturnOddModifiedString(arr[i]);
                    newStringdata.Append(" ");
                    newStringdata.Append(newString);
                }
                else
                {
                    newString = ReturnEvenModifiedString(arr[i]);
                    newStringdata.Append(" ");
                    newStringdata.Append(newString);
                }
            }
        }
        Console.WriteLine(newStringdata.ToString());
        Console.Read();
    }

    //For Even Test
    private static string ReturnEvenModifiedString(string initialString)
    {
        string newString = string.Empty;
        var temparr = initialString.ToCharArray();
        for (var i = 0; i < temparr.Length; i++)
        {
            if (temparr[i] != ' ')
            {
                if (i % 2 == 0 && temparr[i] != ' ')
                {
                    newString += temparr[i].ToString().ToUpper();
                }
                else
                {
                    newString += temparr[i].ToString().ToLower();
                }
            }
        }
        return newString;
    }

    //For Odd Test
    private static string ReturnOddModifiedString(string initialString)
    {
        string newString = string.Empty;
        var temparr = initialString.ToCharArray();
        for (var i = 0; i < temparr.Length; i++)
        {
            if (temparr[i] != ' ')
            {
                if (i % 2 != 0 && temparr[i] != ' ')
                {
                    newString += temparr[i].ToString().ToUpper();
                }
                else
                {
                    newString += temparr[i].ToString().ToLower();
                }
            }
        }
        return newString;
    }
}

OUTPUT

enter image description here

Upvotes: 0

Yashar Aliabbasi
Yashar Aliabbasi

Reputation: 2719

I use flag instead of i because (as you mentioned) white space made this algorithm work wrong:

var sentence = "dancing sentence";
var charSentence = sentence.ToCharArray();
var rs = "";
var flag = true;
for (var i = 0; i < charSentence.Length; i++)
{

    if (charSentence[i] != ' ')
    {
        if (flag)
        {
            rs += charSentence[i].ToString().ToUpper();
        }
        else
        {
            rs += sentence[i].ToString().ToLower();
        }
        flag = !flag;
    }
    else
    {
        rs += " ";
    }
}
Console.WriteLine(rs);

Upvotes: 1

Sweeper
Sweeper

Reputation: 271660

I think you should declare one more variable called isUpper. Now you have two variables, i indicates the index of the character that you are iterating next and isUpper indicates whether a letter should be uppercase.

You increment i as usual, but set isUpper to true at first:

// before the loop
boolean isUpper = true;

Then, rather than checking whether i is divisible by 2, check isUpper:

if (isUpper)
{
    rs += charSentence[i].ToString().ToUpper();
}
else
{
    rs += sentence[i].ToString().ToLower();
}

Immediately after the above if statement, "flip" isUpper:

isUpper = !isUpper;

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186708

Try a simple Finite State Automata with just two states (upper == true/false); another suggestion is to use StringBuilder:

private static string ToDancing(string value) {
  if (string.IsNullOrEmpty(value))
    return value;

  bool upper = false;

  StringBuilder sb = new StringBuilder(value.Length);

  foreach (var c in value) 
    if (char.IsLetter(c)) 
      sb.Append((upper = !upper) ? char.ToUpper(c) : char.ToLower(c));
    else
      sb.Append(c);

  return sb.ToString();
}

Test

var sentence = "dancing sentence";

Console.Write(ToDancing(sentence)); 

Outcome

DaNcInG sEnTeNcE

Upvotes: 1

Related Questions