【问题标题】:How to format each character in input string according to it position?How to format each character in input string according to it position?
【发布时间】:2022-12-01 14:04:01
【问题描述】:

im noob at prog, so i need help.

Need to make a string from each word in the array so that each letter copies itself as many times as the serial number in the word it has, and each new character must starts with uppercase;

Example:

"abcd" -> "A-Bb-Ccc-Dddd"

"RqaEzty" -> "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"

"cwAt" -> "C-Ww-Aaa-Tttt"

One of ways I tried to do it:

public static String Accum(string s) 
  {
    string res;
     for(int i = 0; i < s.Length; i++)
       {
       res += s[i].ToUpper() + s[i].ToLower().Repeat(i) + (i < s.Length - 1 ? "-": "");
     }
    return res;
  }

  • some errors, that I understand, but can't understand what to do with them(google didnt help so much):

error CS1501: No overload for method 'ToUpper' takes 0 arguments

error CS0165: Use of unassigned local variable 'res'

【问题讨论】:

  • "I tried a lot of wariations" - before all of us have to go through the same mistakes as you, please edit the question and include the code that you have tried. We do not provide code writing services here. The task sounds like a student assignment. You probably should know everything to solve it yourself.
  • im noob at prog<-- this does not automatically mean you are a noob at problem solving. Have you tried writing down / drawing up what you would expect would be the necessary steps to achieve the resulting string? That may help.
  • @Thomas Weller Made it, dont judge strictly :D
  • @Astrid E. Ok, thank you for the idea, idk why i havent thought about it xD
  • The idea of codewars is that you solve the puzzle yourself or you press the "surrender" button to see other people's code. It's discouraged to post solutions (or make other people post solutions as an answer).

标签: c# arrays string


【解决方案1】:

Funny... I think I saw this on leetcode.com or codewars.com...

Anyway...

string  Accum(string s) 
{
    var sb = new StringBuilder(s.Length * s.Length); // a bit too long, but that's Ok
    for(int i=0; i < s.Length; ++i)
    {
        sb.Append(Char.ToUpper(s[i]));
        sb.Append(Char.ToLower(s[i]), i);    // i is repeat count.  Fortunately, it silently accepts 0 for count
        sb.Append('-');
    }
    
    // Trick to remove last character;
    sb.Length--;
    
    return sb.ToString();
}

As for the error you were getting:

error CS1501: No overload for method 'ToUpper' takes 0 arguments

ToUpper() works on string. s[i] is just a character.

error CS0165: Use of unassigned local variable 'res'

If s is an empty string, we never enter the loop, and res is unassigned.

UPDATE: I fixed the code to make sure the subsequent letters are lower case, and fixed where I missed a place where I renamed a varaible.

【讨论】:

  • Char also has a method .ToUpper(), but it is not an extension method (and hence does require an input parameter).
  • Thank you, you helped understand. You are right, its from 'codewars'
  • @AstridE. I think you mean it's not an instance method. It's a static method (on the class, not the object). That's what I used in my code example.
  • Absolutely, I stand corrected. My apologies.
【解决方案2】:

Simple oneline with :

string f(string s) => CultureInfo.CurrentCulture.TextInfo.ToTitleCase(
                        String.Join("-", 
                          s.Select((x, i) => new string (Char.ToLower(x), i+1))
                        )
                      );

online demo

【讨论】:

  • interesting, thank you for the feedback!
猜你喜欢
  • 2022-12-27
  • 2022-12-02
  • 2022-12-26
  • 2022-12-31
  • 1970-01-01
  • 2022-11-01
  • 2022-12-16
  • 2022-12-01
  • 1970-01-01
相关资源
最近更新 更多