C# 如何在c中获取字符串中的其他每个字符#

C# 如何在c中获取字符串中的其他每个字符#,c#,string,loops,C#,String,Loops,所以我在研究这个问题:(在C中) 到目前为止,我只是试着把它一块一块地分解,到目前为止,我能够让它显示其他每个字符,但我不知道如何将每个字母连接成一个新的字符串 我针对这个问题的代码如下:我已经注释掉了两个for循环,因为我觉得有一个比我现有的更优雅的解决方案,但我不想失去我的位置,以防另一条路径被证明更具挑战性 using System; using System.Collections.Generic; using System.IO; class Solution { static

所以我在研究这个问题:(在C中)

到目前为止,我只是试着把它一块一块地分解,到目前为止,我能够让它显示其他每个字符,但我不知道如何将每个字母连接成一个新的字符串

我针对这个问题的代码如下:我已经注释掉了两个for循环,因为我觉得有一个比我现有的更优雅的解决方案,但我不想失去我的位置,以防另一条路径被证明更具挑战性

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
    static void Main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
        int inputQTY = Int32.Parse(Console.ReadLine());
        string input = Console.ReadLine(); // The example gives us the first word to be Hacker then the next word Rank on the next line, so the outputs would be Hce akr, and Rn ak respectively.
        int strLen = input.Length;
        char[] inputCharArray = input.ToCharArray();
        string output = "";

        /*
        for (int j = 0; j < inputQTY; j++){

            for (int i = 0; i < strLen; i++) {
                if (j % 2 == 0 && i % 2 == 0) {
                    Console.WriteLine(inputCharArray[i]);
                    output = new string (new char[] {inputCharArray[i]});
                    Console.WriteLine(output);
                    Console.WriteLine("This is i: {0}", i);
                    Console.WriteLine("This is j: {0}", j);
                    Console.WriteLine("--------------");
                    Console.WriteLine("");
                }
                else {
                    Console.WriteLine("This is the next j part hopefully: {0}", j);
                }
            } 
        }*/




    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
类解决方案{
静态void Main(字符串[]参数){
/*在这里输入您的代码。从STDIN读取输入。将输出打印到STDOUT。您的类应命名为Solution*/
int inputQTY=Int32.Parse(Console.ReadLine());
string input=Console.ReadLine();//该示例为我们提供了第一个Hacker单词,然后下一个单词在下一行中排列,因此输出分别为Hce akr和Rn ak。
int strLen=输入长度;
char[]inputCharArray=input.ToCharArray();
字符串输出=”;
/*
对于(int j=0;j
就像我理解的那样,我需要先逐字逐句,抓取每一个字母,然后再逐字逐句抓取剩下的字母,然后将这些字母连接成单词,然后将这些单词连接成一个句子,因此j将是循环,给我两个单词,我是循环,把这两个单词放在一起。。。。。但我很难理解我到底哪里出了问题。除此之外,我觉得还有另一种方法我完全不知道,那就是使用我甚至不知道的命令

不管怎样,谢谢你的帮助,希望在这之后我不会那么生气。谢谢

好了,我用下面的代码解决了这个问题,谢谢大家的帮助

我最终用以下代码(用C#)解决了它:

使用系统;
使用System.Collections.Generic;
使用System.IO;
使用系统文本;
类解决方案{
静态void Main(字符串[]参数){
/*在这里输入您的代码。从STDIN读取输入。将输出打印到STDOUT。您的类应命名为Solution*/
int count=Int32.Parse(Console.ReadLine());
for(int k=0;k
我使用了这个简单的方法来附加两个StringBuilder对象

var sb1 = new StringBuilder();
var sb2 = new StringBuilder();
int i = 0;
foreach (char c in input)
{
    var sb = (i % 2 == 0 ? sb1 : sb2);
    sb.Append(c);
    i = i + 1;
}
output = sb1.ToString() + " " + sb2.ToString();

LINQ版本,已更新以修复索引错误:

output = $"{new string(s.Where((x,i) => i % 2 == 0).ToArray())} {new string(s.Where((x,i) => i % 2 != 0).ToArray())}";
为了解释这一点,您需要抓取字符串中索引可被2整除的每个字符并打印它,然后抓取字符串中索引不可被2整除的每个字符并打印它

更新:

因为有人要我作进一步解释。首先,以下是在HackerRank挑战中成功运行的完整代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    class Solution
    {
        static void Main(String[] args)
        {
            List<string> tests = new List<string>();
            var testCount = int.Parse(Console.ReadLine());
            for (var i = 0; i < testCount; i++)
            {
                tests.Add(Console.ReadLine());
            }
            foreach (var s in tests)
            {
                Console.WriteLine($"{new string(s.Where((x, i) => i % 2 == 0).ToArray())} {new string(s.Where((x, i) => i % 2 != 0).ToArray())}");
            }
        }
    }
这是一个测试,看看一个数字是否可以被2整除,或者是一个偶数

s.Where((x,i) => i % 2 == 0)
new string(s.Where((x,i) => i % 2 == 0).ToArray())
也就是说,对于组成字符串“s”的字符数组,返回所有字符(结果是IEnumerable),其中该字符的索引(字符串中的位置)是偶数

s.Where((x,i) => i % 2 == 0)
new string(s.Where((x,i) => i % 2 == 0).ToArray())
这意味着获取索引为偶数的IEnumerable字符,并将它们返回到一个字符数组中。然后,从该字符数组中创建一个新字符串


对于奇数,它是相同的,但您使用!=这是一条漫长的路

   int count =  int.Parse(Console.ReadLine());
    for(int k = 0; k < count; k++){
        char[] inputChars = Console.ReadLine().ToCharArray();
        char[] evenChars = new char[inputChars.Length % 2 == 0 ? inputChars.Length / 2 : (inputChars.Length + 1) / 2];
        char[] oddChars = new char[inputChars.Length - evenChars.Length];
        int evenIndex=0,oddIndex = 0;
        for(int i = 0; i < inputChars.Length;i++)
            if(i % 2 == 0)
                evenChars[evenIndex++] = inputChars[i];
            else
                oddChars[oddIndex++] = inputChars[i];
        Console.WriteLine(string.Format("{0} {1}",string.Concat(evenChars),string.Concat(oddChars)));
    }
int count=int.Parse(Console.ReadLine());
for(int k=0;k
另一种选择

   int count =  int.Parse(Console.ReadLine());
    for(int k = 0; k < count; k++){
        string input = Console.ReadLine();
         Enumerable.Range(0, input.Length)
            .OrderBy(o => o % 2 != 0)
            .Select(o => {
                if(o == 1)
                   Console.Write(" ");
                Console.Write(input[o]);
                return input[o];
            }).ToArray();
        Console.Write("\n");
    }
int count=int.Parse(Console.ReadLine());
for(int k=0;ko%2!=0)
.选择(o=>{
如果(o==1)
控制台。写(“”);
控制台写入(输入[o]);
返回输入[o];
}).ToArray();
控制台。写入(“\n”);
}

使用
+
或其他方法。如果重复相同的字符,indexOf将获取您的第一个右键@levent。修好了,谢谢你,兰迪,我想我需要更多的帮助。我使用System.Linq添加了代码,然后将代码复制并粘贴到(I)中