Skip to content

Instantly share code, notes, and snippets.

@edinak1
Created October 28, 2015 11:59
Show Gist options
  • Select an option

  • Save edinak1/a40fee34dcc10ec2ac26 to your computer and use it in GitHub Desktop.

Select an option

Save edinak1/a40fee34dcc10ec2ac26 to your computer and use it in GitHub Desktop.
package test;
public class Lesson2 {
public static void main(String[] args) {
System.out.println(getFirstCharacterOfWord(null,2));
System.out.println(getFirstCharacterOfWord(" ",2));
System.out.println(getFirstCharacterOfWord("ahjkji",1));
System.out.println(getFirstCharacterOfWord("df jdbj cbfhd efhfbwh wehbfh khbdsh dcbhb",0));
System.out.println(getFirstCharacterOfWord("df jdbj cbfhd efhfbwh wehbfh khbdsh dcbhb",5));
System.out.println(getFirstCharacterOfWord("df jdbj cbfhd efhfbwh wehbfh khbdsh dcbhb",2));
System.out.println(getFirstCharacterOfWord("df jdbj cbfhd efhfbwh wehbfh khbdsh dcbhb",3));
System.out.println(getFirstCharacterOfWord("df jdbj cbfhd efhfbwh wehbfh khbdsh dcbhb",4));
System.out.println(getFirstCharacterOfWord("df jdbj cbfhd efhfbwh wehbfh khbdsh dcbhb",5));
System.out.println(getFirstCharacterOfWord("df jdbj cbfhd efhfbwh wehbfh khbdsh dcbhb",6));
System.out.println(getFirstCharacterOfWord("df jdbj cbfhd efhfbwh wehbfh khbdsh dcbhb",7));
System.out.println(getFirstCharacterOfWord("df jdbj cbfhd efhfbwh wehbfh khbdsh dcbhb",8));
System.out.println(getFirstCharacterOfWord("df jdbj cbfhd efhfbwh wehbfh khbdsh dcbhb",20));
}
static char getFirstCharacterOfWord(String str,int numberWord)
{
//proverka na sushestvovanie stroki
if(str==null || str=="")
{
System.out.println("String is not found");
return 0;
}
//proverka na nomer slova>0
if(numberWord<1)
{
System.out.println("Index not correct");
return 0;
}
//proverka na nomer slova > kolichestva slov v stroke
int index=0,k=1;
while(str.lastIndexOf(" ")>index)
{
index=str.indexOf((" "), index)+1;
if(str.charAt(index)!=' ')//kolichestvo slov uvelichetsya,esli posle probela ne stoit snova probel
k++;
}
if(k<numberWord)
{
System.out.println("Index > number of words");
return 0;
}
index=0;k=1;
while(true)
{
if(numberWord==k)
return str.charAt(index);
index=str.indexOf(" ",index);
//Proverka esli stroka tolko iz probelov ili bolshe odnogo probela megdu slovami
while(str.charAt(index)==' ')
{
if(index+1==str.length())
{
System.out.println("String have not any word");
return 0;
}
index++;
}
k++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment