c# - Unhandheld Exception: System.Indexoutofbound exception: Index was outside the bound of array -


i trying write code splitting first char string like: input = this stackoverflow output(which want)= tis getting unhandheld exception: system.indexoutofbound exception: index outside bound of array

//my code is

using system; public class n2 {      public static void main(string[] args)     {          string s1;         console.writeline("enter string");         s1=console.readline();         console.writeline(s1);         char[] chararr=s1.tochararray();          for(int i=0;i<s1.length;i++)         {             console.writeline(chararr[i]);         }          console.writeline(s1.length);         char[] n1= new char[s1.length];         n1[0]=chararr[0];          for(int j=1;j<s1.length-1;j++)         {                                if(chararr[j]==' ')                 {                    for(int k=1;k<10;k++)                     {                                n1[k]=chararr[++j];                     }                           }              }          for(int i=0;i<10;i++)         {             console.write(n1[i]);         }         } } 

i dont know mistake, please provide suggestions. in advance....

the immediate reason of error in the

  n1[k]=chararr[++j];  

however, regular expressions , linq seem better approach here:

  string source = "this stackoverflow";    // "tis"   string result = string.join("", regex     .matches(source, @"(^| )\w")     .oftype<match>()     .select(m => m.value.trim())); 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -