Home » Other » Training & Certification » program to print the number of occurrence of a particular letter in a word.
icon9.gif  program to print the number of occurrence of a particular letter in a word. [message #319788] Tue, 13 May 2008 01:09 Go to next message
amritaseema
Messages: 47
Registered: January 2008
Member
hi all,
i am new to pl/sql,can any one plzzz help me...
i want to print the number of occurrence of a particular letter in a word.i tried bt its not showing:

declare
s varchar2(20);
n varchar2(20);
c number;
begin
s:='&string';
dbms_output.put_line('The given word is:'||s);
for i in 1..length(s) loop
n:=substr(s,i,1);
if n=s then
c:=c+1;
end if;
end loop;
dbms_output.put_line('The no of occurance of partcular letter is :'||n||c
);
end;

thanks adavancly.
Re: program to print the number of occurrence of a particular letter in a word. [message #319790 is a reply to message #319788] Tue, 13 May 2008 01:15 Go to previous messageGo to next message
kecd_deepak
Messages: 52
Registered: December 2007
Member
Hello,
i think first you check you logic carefully.
what you required and what code you have written for that.

-> which letter you want to search in the string
Re: program to print the number of occurrence of a particular letter in a word. [message #319794 is a reply to message #319788] Tue, 13 May 2008 01:23 Go to previous messageGo to next message
S.Rajaram
Messages: 1027
Registered: October 2006
Location: United Kingdom
Senior Member
We will help you out. In return what I would like to see is an algorithm from you what you are trying to achieve. Forget pl/sql just a plain algorithm will do.

Regards

Raj

P.S : @kecd_deepak, I missed to see your post.

[Updated on: Tue, 13 May 2008 01:24]

Report message to a moderator

Re: program to print the number of occurrence of a particular letter in a word. [message #319796 is a reply to message #319788] Tue, 13 May 2008 01:34 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Also when you post code don't forget to format it using code tags.
Please read OraFAQ Forum Guide, especially "How to format your post?" section.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code (See SQL Formatter).
Use the "Preview Message" button to verify.

Regards
Michel
icon9.gif  Re: program to print the number of occurrence of a particular letter in a word. [message #319798 is a reply to message #319794] Tue, 13 May 2008 01:37 Go to previous messageGo to next message
amritaseema
Messages: 47
Registered: January 2008
Member
This is my viewpoint
the logic behind this program which i understand is:
frst we need to define two variables (one for string accept and other for holding the substring)
then we have to define a counter to 0
then we'll continue the loop till the length(string)
then we have to check the each letter which we can get from the substr(expression) with the given string
if the letter is matched with it
then the counter value is increase to 1
so in that way if its present mora than one time the counter value is accordingly increasing

for example:
let the string is eye...
it'll chk the occurance of e is 2 and y is 1

[Updated on: Tue, 13 May 2008 01:37]

Report message to a moderator

Re: program to print the number of occurrence of a particular letter in a word. [message #319805 is a reply to message #319798] Tue, 13 May 2008 01:50 Go to previous messageGo to next message
cherry
Messages: 56
Registered: December 2007
Member
SELECT LENGTH('amritaseema') - LENGTH(REPLACE('amritaseema', '&Alphabet','')) occurrence
FROM   dual
Re: program to print the number of occurrence of a particular letter in a word. [message #319817 is a reply to message #319805] Tue, 13 May 2008 02:18 Go to previous messageGo to next message
Frank
Messages: 7901
Registered: March 2000
Senior Member
Do you think this is helpful? Giving a solution without ANY explanation, while it's quite clear the original post is still learning?
icon9.gif  Re: program to print the number of occurrence of a particular letter in a word. [message #319819 is a reply to message #319805] Tue, 13 May 2008 02:21 Go to previous messageGo to next message
amritaseema
Messages: 47
Registered: January 2008
Member
thanks a lot..
bt i need it in pl/sql ...
and sorry for my mistake ,from the next post i'll take care of your suggestion.

can u plzz help me out to write the program in pl/sql and also in sql without using predefine function...
thnks in advance..
Re: program to print the number of occurrence of a particular letter in a word. [message #319822 is a reply to message #319798] Tue, 13 May 2008 02:26 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Quote:
frst we need to define two variables (one for string accept and other for holding the substring)

Quote:
for example:
let the string is eye...

This is only one string what is the second one?

Regards
Michel
icon9.gif  Re: program to print the number of occurrence of a particular letter in a word. [message #319827 is a reply to message #319822] Tue, 13 May 2008 02:31 Go to previous messageGo to next message
amritaseema
Messages: 47
Registered: January 2008
Member
the second one is for holding the substring
for example n:=substr(s,i,1)
here we declare and define n to store the each letter of the word using loop....so that it can check with the given string which is 'eye'

Re: program to print the number of occurrence of a particular letter in a word. [message #319830 is a reply to message #319827] Tue, 13 May 2008 02:34 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
So from a string, you want to get a string containing the distinct characters that are in this string?
For example, "eye" -> "ey".
Is this your problem?

Regards
Michel

[Updated on: Tue, 13 May 2008 02:34]

Report message to a moderator

icon9.gif  Re: program to print the number of occurrence of a particular letter in a word. [message #319888 is a reply to message #319830] Tue, 13 May 2008 06:12 Go to previous messageGo to next message
amritaseema
Messages: 47
Registered: January 2008
Member
No you are not getting me..
i want to show the no of occurance of the letters in a word means..
for the word eye i want to show ...occurance of e is '3'and y is '1'
Re: program to print the number of occurrence of a particular letter in a word. [message #319897 is a reply to message #319888] Tue, 13 May 2008 06:28 Go to previous messageGo to next message
Littlefoot
Messages: 21823
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
So loop twice, nested - once through the input string, second time through all letters. Compare input string substring with a current alphabet letter. Add 1 if they match. Don't add anything if they don't. Print the result.
Re: program to print the number of occurrence of a particular letter in a word. [message #319899 is a reply to message #319888] Tue, 13 May 2008 06:30 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
What is the EXACT output you want?
If it is a string what is this string for, for instance, "eye"?

Regards
Michel
Re: program to print the number of occurrence of a particular letter in a word. [message #319907 is a reply to message #319899] Tue, 13 May 2008 06:43 Go to previous messageGo to next message
Littlefoot
Messages: 21823
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
As far as I understood,

Output:
e x 2
y x 1
Re: program to print the number of occurrence of a particular letter in a word. [message #319919 is a reply to message #319907] Tue, 13 May 2008 07:06 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Quote:
Output:
e x 2
y x 1

Maybe but it is not clear (for me) what should be the actual result (but maybe my brain is frozen today).

Regards
Michel
icon5.gif  Re: program to print the number of occurrence of a particular letter in a word. [message #319920 is a reply to message #319907] Tue, 13 May 2008 07:06 Go to previous messageGo to next message
amritaseema
Messages: 47
Registered: January 2008
Member
yes, you are right i want the out put like this only...

Output:
e x 2
y x 1
Re: program to print the number of occurrence of a particular letter in a word. [message #319927 is a reply to message #319920] Tue, 13 May 2008 07:22 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
You can do it in a single SQL.
But if you want to do it with PL/SQL you can have a look at associative array.
The core of the procedure might be something like:
letters(substr(s,i,1)) := nvl(letters(substr(s,i,1)),0) + 1;

In the end, you scan the array to output the values.

Regards
Michel
Re: program to print the number of occurrence of a particular letter in a word. [message #320317 is a reply to message #319927] Wed, 14 May 2008 12:07 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
It seems OP gave up (or homework deadline is over), so I post a solution in SQL:
SQL> def val=eye
SQL> with
  2    letters as (
  3      select chr(ascii('a')+level-1) letter from dual connect by level <= 26
  4    )
  5  select letter||' -> '||
  6         ( length('&val')
  7           - length(translate('&val','#'||letter, '#')) ) val
  8  from letters
  9  where instr('&val',letter) > 0
 10  /
VAL
---------------------------------------------
e -> 2
y -> 1

2 rows selected.

Regards
Michel
Re: program to print the number of occurrence of a particular letter in a word. [message #320534 is a reply to message #319927] Thu, 15 May 2008 07:16 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Same thing in PL/SQL:
SQL> def val=eye
SQL> declare
  2    type tab_type is table of integer index by varchar2(1);
  3    letters tab_type;
  4    str     constant varchar2(200) := '&val';
  5    c       varchar2(1);
  6  begin
  7    for i in 1..length(str) loop
  8      c := substr(str,i,1);
  9      if letters.exists(c) then
 10         letters(c) := letters(c) + 1;
 11      else
 12         letters(c) := 1;
 13      end if;
 14    end loop;
 15    c := letters.first;
 16    while c is not null loop
 17      dbms_output.put_line(c||' -> '||letters(c));
 18      c := letters.next(c);
 19    end loop;
 20  end;
 21  /
e -> 2
y -> 1

PL/SQL procedure successfully completed.

Regards
Michel
icon7.gif  Re: program to print the number of occurrence of a particular letter in a word. [message #320579 is a reply to message #320534] Thu, 15 May 2008 09:55 Go to previous message
amritaseema
Messages: 47
Registered: January 2008
Member
Thanks a lot sir....
Smile Smile
Previous Topic: 70-290 70-291 70-293 does anybody have?
Next Topic: code to display string of numbers in words(Ex: 321 as Three Two One); (Please don't use to_d
Goto Forum:
  


Current Time: Sat Nov 23 14:16:22 CST 2024