Hey guys,
I know I shouldn't be posting such a simple assignment problem on here, however, I have run in to a rut and I can't seem to locate the error and I'm simply seeking a new set of eyes to see if you can spot it, it can't be difficult but I just can't seem to find it.
Here is what I have:
Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
int letters[26]={0}, len, i=0, tlet;
char input[50], temp;
// gather input and adjust as appropriate
printf("Enter a line of text to be counted: ");
gets(input);
len=(int)strlen(input);
// scan through and count
for( i=0 ; i < len ; i++ ) {
temp=input[i];
tlet=temp-'a';
// ensure that the character is a lower case letter
if( (tlet > -1) && (tlet < 26) )
letters[tlet]++;
}
// display results (only results with values above 0)
for( i=0 ; i < 26 ; i++ ) {
if( letters[i] > 0 ) { printf("%c: %d\n", ('a'+i), letters[i]); }
}
return(0);
}
Now this is unforunately a rudamentory programming class, but university is making take this stuff all over again. What the problem with this is, after gaining the input and processing that input my program seems to corrupt the line... now the line we have to use for input is "This is sample text for demonstrating exercise (2). Only lower case counted." For some reason right around the "lower case" section of the sentence, the data (everytime it's run) gets corrupted and letters don't get counted. MSVC also gives me a debug error saying that Stack around the variable 'input' was corrupted.
Any fresh eyes out there able to see why this is? As a side note, directly after gaining the input I output the text to the screen (during testing) and everytime it gets displayed properly. I can't seem to figure out why then, with the code above, the variable is suddenly becoming corrupted during run-time since I'm not actually doing anything with the input other then counting the number of lower case letters entered and outputting the number of each letter (for those who have counted values above 0). Also the lines using the 'temp' variable was adjusted to see if perhaps using the input[i] data directly was causing the corruption, but it doesn't seem to be the case.
Any fresh eyes would be greatly appreciated on this, it's been giving me a headache. lol.