Hello, this my first thread and I need some supper fast help I'm on a deadline...

I have applied for a Microsoft program but part of the application process is to complete this assignment...

You are to create a console application that accepts exactly one command-line argument. If it doesn’t receive the argument, the application must display an error message and exit. The application must parse the text input and output the number of times each letter of the alphabet occurs in the text. Case sensitivity is not required.

For example, if the command-line argument is “baaad” the displayed result must be:

There are 3 A's
There are 1 B's
There are 0 C's
There are 1 D's
There are 0 E's
There are 0 F's
etc...

I've gotten this so far...

<html>
<head>
<script type="text/javascript">
function count_char($str,1)
{
return ("count_char($str,1);
}
<?php
$str = "Hello World!";
print_r(count_chars($str,1));
?>
public class CountWordLetters {
public static void main(String[] args) {
String InputWord = JOptionPane.showInputDialog("Please enter a string: ");
int len = InputWord.length();
char[] tempCharArray = new char[len];
char[] charArray = new char[len];


for (int i = 0; i < len; i++) {
tempCharArray[i] = InputWord.charAt(i);
}
</script>
</head>

<script type="text/javascript">
echo(count_chars($str,1))
</script>

<body>
<form>
<input type="text" value="Please enter string" /> 
<input type="button" value="Click me!" onclick="count_chars($str,1))" />
</form>

<p>By clicking the button above the letters in the string will be counted and displayed</p>

</body>
</html>

But I know for sure I messed something up/left something out because whenever I try to run the code, clicking the button doesn't do anything, Please Help Me!!!
I started learning from w3schools.com but they don't tell you exactly how to assemble the Java Script code I have until 6/25/10 to submit my code to my application overseer.

Recommended Answers

All 10 Replies

Save this page as test.php

<html>
<head>
<script type="text/javascript">		
function fnsubmit()
{
if(document.frm.txt.value=="Please enter string")
{
	alert("Please enter string");
	document.frm.txt.focus();
	return false;

}
document.frm.method="post";
document.frm.action="test.php";
document.frm.submit();
}
</script>


<?php
//$str = "Hello World!";
//print_r(count_chars($str,1));

	$txt1="";
	if(empty($_REQUEST['txt']))
	{
		$txt1="";
	}
	else
	{
		$txt1= $_REQUEST['txt'];
		print_r(count_chars($txt1,1));
	}
?>
</head>
<body>
<form name="frm">
<input type="text" name="txt" value="Please enter string" value="<?php echo $txt1;?>" />
<input type="button" value="Click me!" onclick="fnsubmit();" />
</form>

<p>By clicking the button above the letters in the string will be counted and displayed</p>

</body>
</html>

You are to create a console application that accepts exactly one command-line argument. If it doesn’t receive the argument, the application must display an error message and exit.

What language specified by the assignment? It does not look like JavaScript or PHP because of the word 'command-line' and 'console application'. To me, it looks more like C-liked program, Java, or scripting languages such as Perl, Ruby, etc. that takes arguments from command-line. I doubt that this is the right place or right computer language you are looking for.

Anyway, just for those who may want to see code in JavaScript for this, here is one way of implementing it...

function countChars(str) {
  var out = ""  // output string
  var chrHash = new Array(256)  // counting char
  var pos = 0   // positiong of character in str
  while (pos < str.length) {  // step through each character in str
    // lazy initiation
    if (!chrHash[str.charCodeAt(pos)]) { chrHash[str.charCodeAt(pos) = 0 }
    chrHash[str.charAt(pos)] += 1  // count the char at each position
    pos += 1  // go to the next char
  }
  for (var i=65; i<91; i++) {  // from 'A' to 'Z'
    out += "There "
    if (chrHash[i] && chrHash[i]>1) { out += "are "+chrHash[i] }
    else if (chrHash[i]) { out += "is "+chrHash[i] }
    else { out += "is 0" }
    out += " "+String.fromCharCode(i)+"'s\n"
  }
  for (var i=97; i<123; i++) {  // from 'a' to 'z'
    out += "There "
    if (chrHash[i] && chrHash[i]>1) { out += "are "+chrHash[i] }
    else if (chrHash[i]) { out += "is "+chrHash[i] }
    else { out += "is 0" }
    out += " "+String.fromCharCode(i)+"'s\n"
  }
  return out
}

Sorry I must have posted the wrong portion of the assignment as you can complete the assignment in the language your most familiar with and I being very familiar with html chose java because html wasn't an option

OK. Then you could use HTML & JavaScript to complete it without PHP. Though, whichever you like could accomplish the same task.

To add to my JavaScript code, here is the HTML portion... By the way, you need to add DOCTYPE yourself because I am just writing the code here without testing. :P

<html>
  <head>
  <script type="text/javascript">
  function countChars(str) {
    // Plug in my code above right here...
  }
  function displayCountChars(inputID, displayDivID) {
    var output = countChars(document.getElementById(inputID).value)

    alert(output)  // display output in alert box

    // If you want to it to be display on the page, replace the part of "'s\n" with
    // "'s<br/>" instead as below
    output = output.replace(/\n/g, "<br/>")
    document.getElementById(displayDivID).innerHTML = output
  }
  </scrip>
  </head>

  <body>
  <h1>Count Char</h1>
  <b>Result</b>:
  <br/>
  <div id="result"></div>
  <br/>
  Enter a string: <input id="command_line" type="text" value="">
  <br/>
  <input type="button" value="Count" onclick="displayCountChars('command_line', 'result')">
  </body>
</html>

Thanx Kirti23 at your code edits have helped some, would happen to know of a better site to learn javascript as oppose to w3schools.com?

Thanx to you Taywin...ummm just wondering is anyone here familiar with w3schools.com if so does anyone know if the Tryit Editor @w3schools.com works with out side code? I am super grateful for all your help:D

Certain information on w3schools.com is now outdated. However, it can still be used for some extent. Sorry, I never used any kind of that editor... What do you mean by 'outside code'? Do you mean 'external source file'? Though, you can try may other Tryit by googling for 'tryit editor'. There are many of them to try it. ;)

What I mean by 'outside code' is code that is not related to the website content or tutorials, but thank you for the heads up on google I use it for everything and it didn't even cross my mind to use it to find an editor:)

Hay just thought I would inform you guys that the Tryit Editors at w3schools doesn't work I just found a google forum with that bit of information. :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.