62 lines
3.6 KiB
HTML
62 lines
3.6 KiB
HTML
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||
|
<html>
|
||
|
<head>
|
||
|
<title>OccurencesOfSpecifiedCharacterInArray.java</title>
|
||
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||
|
<style type="text/css">
|
||
|
<!--
|
||
|
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||
|
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||
|
table {color: #888888; background-color: #313335; font-family: monospace}
|
||
|
.string {color: #6a8759}
|
||
|
.number {color: #6897bb}
|
||
|
.comment {color: #808080}
|
||
|
.whitespace {color: #505050}
|
||
|
.ST0 {color: #808080; font-family: monospace; font-weight: bold}
|
||
|
.literal {color: #cc7832}
|
||
|
-->
|
||
|
</style>
|
||
|
</head>
|
||
|
<body>
|
||
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/lab5-recursion2_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab5/recursion2_calebfontenot/OccurencesOfSpecifiedCharacterInArray.java</td></tr></table>
|
||
|
<pre>
|
||
|
<span class="comment">/*</span>
|
||
|
<span class="comment"> * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license</span>
|
||
|
<span class="comment"> * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template</span>
|
||
|
<span class="comment"> */</span>
|
||
|
<span class="literal">package</span> edu.slcc.asdv.caleb.lab5.recursion2_calebfontenot;
|
||
|
|
||
|
<span class="literal">import</span> java.util.Scanner;
|
||
|
|
||
|
<span class="comment">/**</span>
|
||
|
<span class="comment"> *</span>
|
||
|
<span class="comment"> * </span><span class="ST0">@author</span> <span class="comment">caleb</span>
|
||
|
<span class="comment">*/</span>
|
||
|
<span class="literal">public</span> <span class="literal">class</span> OccurencesOfSpecifiedCharacterInArray {
|
||
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args)
|
||
|
{
|
||
|
System.out.print(<span class="string">"</span><span class="string">Enter a string: </span><span class="string">"</span>);
|
||
|
Scanner input = <span class="literal">new</span> Scanner(System.in);
|
||
|
String s = input.nextLine();
|
||
|
<span class="literal">char</span>[] items = s.toCharArray();
|
||
|
|
||
|
System.out.print(<span class="string">"</span><span class="string">Enter a character: </span><span class="string">"</span>);
|
||
|
<span class="literal">char</span> ch = input.nextLine().trim().charAt(<span class="number">0</span>);
|
||
|
|
||
|
System.out.println(ch + <span class="string">"</span><span class="string"> appears </span><span class="string">"</span> + count(items, ch) + <span class="string">"</span><span class="string"> times.</span><span class="string">"</span>);
|
||
|
}
|
||
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> count(<span class="literal">char</span>[] chars, <span class="literal">char</span> ch) {
|
||
|
<span class="literal">return</span> count(chars, chars.length - <span class="number">1</span>, ch);
|
||
|
}
|
||
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> count(<span class="literal">char</span>[] chars, <span class="literal">int</span> high, <span class="literal">char</span> ch) {
|
||
|
<span class="literal">if</span> (high >= <span class="number">0</span>) {
|
||
|
<span class="literal">return</span> count(chars, high - <span class="number">1</span>, ch) + (ch == chars[high] ? <span class="number">1</span> : <span class="number">0</span>);
|
||
|
} <span class="literal">else</span> {
|
||
|
<span class="literal">return</span> <span class="number">0</span>;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
</pre></body>
|
||
|
</html>
|