Hi
I have aslight problem regarding the strlen function. I was told that if you had an array that can hold 20 characters, and you asked the user to input a string, and they entered a string of 7 characters, that the program would print out 'rubbish' due to the string being so small. I was told I could counter this problem by using "strlen".
This is where I get stuck, i'm not really sure how to implement this into my program.
I know how to use the function if I wanted to print to the screen the total of characters in the string, but I need the function to tell check the lenght of the string and then take that value and use that in the loop with the 'if' statement, so it dosent process any rubbish.Code:#include<stdio.h> #include<string.h> void main() { char romanchar[16]; int counter; int sum; int lenght; printf("Please enter a Roman Numeral"); gets(romanchar); counter = 0; sum = 0; lenght = strlen(romanchar); <<<---------------- this is where I get stuck for ( counter = 0; counter < 16; counter++ ) { if ( romanchar[counter] == 'I' && romanchar[counter+1] == 'V') { sum = sum + 4; counter++; }
Any help will be greatly appreciated.