ASDV-Java/Semester 3/Assignments/lab5-recursion2_CalebFontenot/Printed HTMLs/OccurancesOfChar.html
2023-09-06 14:34:16 -05:00

59 lines
3.4 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>OccurancesOfChar.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/OccurancesOfChar.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> OccurancesOfChar {
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args)
{
Scanner input = <span class="literal">new</span> Scanner(System.in);
System.out.print(<span class="string">&quot;</span><span class="string">Enter a string: </span><span class="string">&quot;</span>);
String s = input.nextLine();
System.out.print(<span class="string">&quot;</span><span class="string">Enter a character: </span><span class="string">&quot;</span>);
<span class="literal">char</span> ch =input.nextLine().charAt(<span class="number">0</span>);
<span class="literal">int</span> times = count(s, ch);
System.out.println(ch + <span class="string">&quot;</span><span class="string"> appears </span><span class="string">&quot;</span> + times
+ (times &gt; <span class="number">1</span> ? <span class="string">&quot;</span><span class="string"> times </span><span class="string">&quot;</span> : <span class="string">&quot;</span><span class="string"> time </span><span class="string">&quot;</span>) + <span class="string">&quot;</span><span class="string">in </span><span class="string">&quot;</span> + s);
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> count(String str, <span class="literal">char</span> a) {
<span class="literal">int</span> result = <span class="number">0</span>;
<span class="literal">if</span> (str.length() &gt; <span class="number">0</span>) {
result = count(str.substring(<span class="number">1</span>), a) +
(( str.charAt(<span class="number">0</span>) == a) ? <span class="number">1</span> : <span class="number">0</span>);
}
<span class="literal">return</span> result;
}
}
</pre></body>
</html>