hi everyone. i have to write a prog that divedes words on syllables for one language. the algoritm itself is ok.i take from user the string convert it to char string make the operation and put it to new string with difined size,but then the new string is displayed (and it is shorter than strings length) the empty spaces show squares. how can i solve this problem?
Divide words on syllables
Collapse
X
-
Tags: None
-
Originally posted by mavishsterhi everyone. i have to write a prog that divedes words on syllables for one language. the algoritm itself is ok.i take from user the string convert it to char string make the operation and put it to new string with difined size,but then the new string is displayed (and it is shorter than strings length) the empty spaces show squares. how can i solve this problem?
everywhere where you expect something could be wrong, You'll most likely
hunt down that bug in one short session.
kind regards,
Jos -
-
-
String s = textfield2.getT ext();
String st=new String(s);
p=st.length();
len=p;
//from string to char
for (int c = 0; c < len; c++)
{
str[c] = st.charAt(c);
}
//write to str1 from back
for(int l=0; str[l]!='\0'; l++)
{
str1[len-1] = str[l];
len--;
}
//checking
for( k = 0 ; str1[k]!='\0'; )
{
if((str1[k]=='a')||(str1[k]=='o')||(str1[k]=='e')||(str1[k]=='u')||(str1[k]=='i'))
{
str2[j] = str1[k];
k++;
j++;
i++;
str2[j] = str1[k];
j++;
i++;
str2[j] = '-';
j++;
k++;
i++;
}//end of if
else
{
str2[j] = str1[k];
j++;
k++;
}//end of else
}//end of for
i=i+p;
int t=i;
char[] str3 = new char[??];
//converting
for(int m = 0; m < i; m++)
{
str3[t] = str2[m];
t--;
}
newstr = String.valueOf( str3);
g.drawString(ne wstr, 50, 130);
everything is ok but the empty cell in str3 (!because size is larger than the string itself going to be!)are also being copied to newstr and display square shapes. but when i put the exact size that str3 has to have it gives errors.what should i do?Comment
-
I've been trying to understand what your code does and I'm lost; you should
check the API for the String and StringBuffer class because a lot of stuff
you're trying to accomplish the hard way can be done using just a few simple
method calls using the API for those classes. e.g. reversing a String can be
done this way:[code=java]
String reverse(String s) {
return new StringBuffer(s) .reverse().toSt ring();
}[/code]
I don't understand where that '\0' character comes from. Are you trying to
translate some C code for this?
Also you don't need to copy Strings to other new Strings so often. Maybe you
want/can elaborate on *what* the rules are you want to use for finding the
syllables of a word?
kind regards,
JosComment
-
actualy i use C PL but i have to write it in Java couse of that i have problems."\0" symbol i use to define the end of string.Comment
-
Originally posted by mavishsteractualy i use C PL but i have to write it in Java couse of that i have problems."\0" symbol i use to define the end of string.
through, can you tell *what* the syllable rules are? Then we can simple start
from scratch.
kind regards,
JosComment
-
ok, for this language rule is very simple, in one syllable there can be 1 vowel and one or two consonants.(mek-tep, o-kul, ki-tep)Comment
-
Originally posted by mavishsterok, for this language rule is very simple, in one syllable there can be 1 vowel and one or two consonants.(mek-tep, o-kul, ki-tep)
and ''c be a consonant and '$' be a non-existent character (such as the end of
the text or beyond). Then the following rules apply:Code:vcv ---> v-cv vcc ---> vc-c vv$ ---> vv$ vc$ ---> vc$ v$$ ---> v$$
vowel read two more characters and apply the rules outlined above.
kind regards,
JosComment
-
-
Originally posted by mavishsterjust the same i have done, and it is working. i have a problem w/ output
shown reverses Strings, copies them to char arrays a couple of times and
mysteriously deals with '\0' characters. What exactly is working then and what
are the 'output problems'?
kind regards,
JosComment
-
I understand your code somewhat but the way you use variables makes it confusing. Your question is on your output. If the code compiles until the output following the last for loop, I believe that is why you have the question marks on the array of char. One question I asked myself was that, how do you know what the exact size of str3 should be as you said when str3 is dependent on str2 and str2 is indirectly dependent on user input and there is no defined bound on the length of user inputted strings? Maybe that is where the problem is coming from. why not trim your code a little or rewrite these one with lots of comments, especially on the if-then statement where it searches for a vowel, I really got lost there. On why your output doesn’t work out, as I was saying, str3 is dependent on str2 from the for loop and you should make this evident in declaring str3, like this: char[] str3 = new char[Array.length(st r2)]. Try it and see but I recommend you put lots of comment on that code if you want it to be readable and show declarations for your variables so we can know where the scopes are. I can’t see the scopes so I get lost on which variable is which.
It seems you want to control length of user input, then do so.Comment
Comment