8 Replies - 1905 Views - Last Post: 22 May 2012 - 02:26 AM

#1 uzumaki07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 19-May 12

Can I use substring in a "for loop"?

Posted 19 May 2012 - 11:26 AM

Hey guys,

my problem about my program is...I really want to count the number of occurences for each first letters in each word that my program found on the file...an example for this is,
3 words are found let say GHIO PSLEM GBNCM... my program will scan a word, checkout the first letter "G" therefore prints out G 1 count and that goes with P as well..P 1 count...then scans the next firstletter of the third word "G", gives out G 2 count.
so I just want my program to scan the first letter of a word then ignore the other letters...then adds "1" to the frequency of the letter that was found.

I saw a couple of examples on how to use a substring...is there and possibilities that I can use substring in a loop?
If not, will you guys be able to give me a couple of good starting point to start with?

Sorry, about this I just started doing programming this year so please help me

Is This A Good Question/Topic? 0
  • +

Replies To: Can I use substring in a "for loop"?

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Can I use substring in a "for loop"?

Posted 19 May 2012 - 12:00 PM

Hey.

Sure, you can use substring in a loop. There is nothing preventing you from doing that. However, in this case I don't really see a need for it. You'll only ever need to read single characters from the string, and you can do that using array-like syntax, passing the character index.
var str = "Hello, world!";
var char = str[7];

// "char" is now "w", the 8'th char in the
// string. (7 being it's zero-indexed position.)



If you want to go through a file and find the first letter in every word, all you have to do is loop through the string, check each letter to see if it is a space, and if it is grab the next letter. - O, and remember to check if the first letter of the string should be counted to. It will be skipped by the loop.

This assumes every word is separated from others by a space. You may have to take additional steps if you are anticipating poor grammar, like: "First sentence.Second sentence." (Note the lack of a space after the first sentence.)
Was This Post Helpful? 0
  • +
  • -

#3 uzumaki07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 19-May 12

Re: Can I use substring in a "for loop"?

Posted 19 May 2012 - 12:58 PM

Thank you very much!!! Now I can start using substring in some my loops
Was This Post Helpful? 0
  • +
  • -

#4 JMRKER  Icon User is online

  • D.I.C Addict

Reputation: 129
  • View blog
  • Posts: 832
  • Joined: 25-October 08

Re: Can I use substring in a "for loop"?

Posted 19 May 2012 - 07:13 PM

An alternate solution might be to use the str.charAt() function...
<html>
<head>
<title> Untitled </title>
<script type="text/javascript">
function countFirstLetters() {
  var tarr = document.getElementById('TArea').value.split(' ');
  var c;  var str = '';
  var counts = [];
  for (var i=0; i<tarr.length; i++) {
    c = tarr[i].charAt(0).toUpperCase();  // str += c+' ';
    if (counts[c] == undefined) { counts[c] = 1; } else { counts[c]++; }
  }
  str += '<p>';
  for (var key in counts) {
    str += key+' : '+counts[key]+'<br>';
  }
  document.getElementById('divArea').innerHTML = str;
}
</script>

<style type="text/css">

</style>
</head>
<body>
<textarea id="TArea" rows="10" cols="40">
Now is the time for all good men to come to the aid of their country.
Whatever the mind of man can conceive and believe, he can achieve.
</textarea>
<button onclick="countFirstLetters()">Count first letters</button><br>
<div id="divArea"></div>
</body>
</html>

:bananaman:
Was This Post Helpful? 0
  • +
  • -

#5 uzumaki07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 19-May 12

Re: Can I use substring in a "for loop"?

Posted 19 May 2012 - 11:50 PM

Hi JMKER,

Thank you for posting an alternate solution....I will use it as well
Was This Post Helpful? 0
  • +
  • -

#6 uzumaki07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 19-May 12

Re: Can I use substring in a "for loop"?

Posted 20 May 2012 - 01:20 AM

Hey guys,

Can I ask another problem.....I'm trying to compare the letter frequency that my program just printed out (output) to the WORLD letter freqeuncy...

Is there a good way on how to start my program to do this?
Was This Post Helpful? 0
  • +
  • -

#7 uzumaki07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 19-May 12

Re: Can I use substring in a "for loop"?

Posted 20 May 2012 - 01:30 AM

I forgot to put an example:

Here you go:

so if my program calculated the letters:


Letter frequencies:

a: 30 2.412%
b: 23 1.849%
.
.
.
.
.
o: 10.772%

I want this to be compared to the WORLD letter frequencies which are located in wikipeda:

World Letter Frequencies:
a: 8.167%
b: 1.492%
.
.
e: 12.702%
.
.
.
t: 9.052%


First letter frequencies:
a:
b: 1 0.351%
c: 10 3.509%
d: 32 11.228%
e: 7 2.456%
.
.
.
.

t: 49 17.193%



World First letter frequencies:
a 11.602%
b 4.702%
.
.
e 2.000%
.
.
t 16.671%



so if we look at letter 'o' its the frequent letter ( 10.772%) there this makes it likely that the substitute letter will be letter 'e' (12.702%) or ‘t’ (9.056%)
letter ‘o’ has a frequency of 1.754% as the first
letter of a word in the enciphered text, which is closer to the expected frequency of
‘e’ (2.000%) than ‘t’ (16.671%), so it’s a good bet that ‘o’ is actually the substitute
for ‘e’ in the plaintext.
Was This Post Helpful? 0
  • +
  • -

#8 JMRKER  Icon User is online

  • D.I.C Addict

Reputation: 129
  • View blog
  • Posts: 832
  • Joined: 25-October 08

Re: Can I use substring in a "for loop"?

Posted 20 May 2012 - 01:04 PM

View Postuzumaki07, on 20 May 2012 - 02:20 AM, said:

Hey guys,

Can I ask another problem.....I'm trying to compare the letter frequency that my program just printed out (output) to the WORLD letter freqeuncy...

Is there a good way on how to start my program to do this?


Post a URL link to the stats you want to compare
Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,335
  • Joined: 08-June 10

Re: Can I use substring in a "for loop"?

Posted 22 May 2012 - 02:26 AM

View Postuzumaki07, on 20 May 2012 - 10:30 AM, said:

so if we look at letter 'o' its the frequent letter ( 10.772%) there this makes it likely that the substitute letter will be letter 'e' (12.702%) or ‘t’ (9.056%)
letter ‘o’ has a frequency of 1.754% as the first letter of a word in the enciphered text, which is closer to the expected frequency of ‘e’ (2.000%) than ‘t’ (16.671%), so it’s a good bet that ‘o’ is actually the substitute
for ‘e’ in the plaintext.

I can’t follow you on that conclusion. why should a letter be a substitute letter for another one?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1