![]() ![]() ![]() As long as the phonetic alphabet is output, and you do a bit of defensive programming to ensure that nothing has the potential of screwing up, I’d consider your efforts a success. Your solution can be different from mine, of course. The if test at Line 24 pops out of the loop once any non-alphabet character is encountered, otherwise the array index would be invalid and the program could crash with a memory error. The scanf() function stops reading at whitespace, but it does include symbols and numbers as part of a word. The islpha() test at Line 24 confirms that the word contains only letters of the alphabet. The value generated is used as the npa array index. The printf() statement at Line 25 obtains the offset by subtracting the upper case letter’s ASCII value from the letter ‘A’. The letter is converted to uppercase by the toupper() function at Line 22. ![]() I chose a while loop at Line 20 using the current letter in the word buffer as an index. The next step in the code is to loop through input one character at a time. That allows for the null character ( \0) at the end of the string. In this instance, only 23 characters are read by setting the %s placeholder to a width of 24. I also took advantage of the scanf() function’s %s placeholder to cap input. That’s because scanf() stops reading at the first whitespace character. The scanf() function at Line 18 is ideal to read in a limited amount of text. I set its size value to 24, which is a goodly length, but not too long. "Foxtrot", "Golf", "Hotel", "India", "Juliett", So the letter ‘B’ references element 1 in the array, “Bravo.” (Remember, arrays start numbering at element zero.) That letter’s offset (from A) is used to reference each phonetic alphabet word in the array. The word input is dissected one letter at a time. For input such as hello the output would be something like:įor my solution, I used an array of 26 pointers to represent the phonetic alphabet words. This month’s task was to create code that translates spelling from a single word into the corresponding NATO phonetic alphabet words. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |