How to split a string into groups of letters?

by Minh Phan   Last Updated September 12, 2019 02:26 AM

For my c# class my instructor is having us prompt the user for a starting and ending input to split a word in different places.

My main problem is I don't know what variables to declare and how to have it split the word up.

string reply = "";
string userword = "";
string splitword = "";
int length = 0;
int startingindex = 0; 

System.Console.WriteLine("Enter a single world: ");
userword = System.Console.ReadLine();

System.Console.WriteLine("Enter a starting index: ");
startingindex = Convert.ToInt32( System.Console.ReadLine());
// ?
System.Console.WriteLine("Enter a length: ");
length = Convert.ToInt32(System.Console.ReadLine()); 
// ?

For example if the word inputted is "tomato" and the user inputs a starting index of 2 and a length of 3 the return statement would be "mat".

Tags : c#


Answers 4


Console.WriteLine(userword.Substring(startingindex,length));

Bhavjot
Bhavjot
September 12, 2019 00:48 AM


            String userword = "";
            String splitword = "";
            int length = 0;
            int startingindex = 0; 

            System.Console.WriteLine("Enter a single world: ");
            userword = System.Console.ReadLine();

            System.Console.WriteLine("Enter a starting index: ");
            startingindex = Convert.ToInt32( System.Console.ReadLine());
            System.Console.WriteLine("Enter a length: ");
            length = Convert.ToInt32(System.Console.ReadLine()); 

            splitword = userword.Substring(startingindex, length);
            System.Console.WriteLine(splitword);
Tibo Geysen
Tibo Geysen
September 12, 2019 00:55 AM

Would something like this do?

    string reply = "";
    string userword = "";
    string splitword = "";
    int length = 0;
    int startingindex = 0; 

    System.Console.WriteLine("Enter a single world: ");
    userword = System.Console.ReadLine();

    System.Console.WriteLine("Enter a starting index: ");
    startingindex = Convert.ToInt32( System.Console.ReadLine());  

    System.Console.WriteLine("Enter a length: ");
    length = Convert.ToInt32(System.Console.ReadLine()); 

    splitword = userword.Substring(startingindex, length);
    System.Console.WriteLine(splitword);
EGC
EGC
September 12, 2019 00:56 AM

string reply = "";
string userword = "";
string splitword = "";
int length = 0;
int startingindex = 0; 

System.Console.WriteLine("Enter a single world: ");
userword = System.Console.ReadLine();

System.Console.WriteLine("Enter a starting index: ");
startingindex = Convert.ToInt32( System.Console.ReadLine());

System.Console.WriteLine("Enter a length: ");
length = Convert.ToInt32(System.Console.ReadLine()); 

System.Console.WriteLine(useword.Substring(startingindex, length);
FerrySan
FerrySan
September 12, 2019 02:24 AM

Related Questions