Divide words on syllables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mavishster
    New Member
    • May 2007
    • 9

    Divide words on syllables

    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?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by mavishster
    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?
    A little tip: System.out.prin tln() is your friend: stick in those debug print statements
    everywhere where you expect something could be wrong, You'll most likely
    hunt down that bug in one short session.

    kind regards,

    Jos

    Comment

    • mavishster
      New Member
      • May 2007
      • 9

      #3
      i am already using this function.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by mavishster
        i am already using this function.
        We can't help you any further if you don't post some relevant snippets of code.
        Today my crystal ball is in its charger so I can't make psychic guesses ;-)

        kind regards,

        Jos

        Comment

        • mavishster
          New Member
          • May 2007
          • 9

          #5
          here i am sending some code

          Comment

          • mavishster
            New Member
            • May 2007
            • 9

            #6
            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

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              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,

              Jos

              Comment

              • mavishster
                New Member
                • May 2007
                • 9

                #8
                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

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by mavishster
                  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.
                  Strings in Java aren't terminated by '\0' characters. Instead of hacking your way
                  through, can you tell *what* the syllable rules are? Then we can simple start
                  from scratch.

                  kind regards,

                  Jos

                  Comment

                  • mavishster
                    New Member
                    • May 2007
                    • 9

                    #10
                    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

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by mavishster
                      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)
                      So there are three type of characters (yes, three, read on). Let 'v' be a vowel
                      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$$
                      Basically when you've 'read' a consonant, simply emit it. When you've read a
                      vowel read two more characters and apply the rules outlined above.

                      kind regards,

                      Jos

                      Comment

                      • mavishster
                        New Member
                        • May 2007
                        • 9

                        #12
                        just the same i have done, and it is working. i have a problem w/ output

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by mavishster
                          just the same i have done, and it is working. i have a problem w/ output
                          You must've done something else from what you've shown us. The code you've
                          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,

                          Jos

                          Comment

                          • emekadavid
                            New Member
                            • Mar 2007
                            • 46

                            #14
                            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

                            Working...