1

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

Yashar Aliabbasi
  • 2,663
  • 1
  • 23
  • 35
Teallaair9
  • 39
  • 9

5 Answers5

1

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
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

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;
Sweeper
  • 213,210
  • 22
  • 193
  • 313
1

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);
Yashar Aliabbasi
  • 2,663
  • 1
  • 23
  • 35
1

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])

fubo
  • 44,811
  • 17
  • 103
  • 137
0

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

Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62