Solving Confusion about Scanf Homework

  • Thread starter Peter P.
  • Start date
  • Tags
    Confusion
In summary, the conversation discusses a programming problem where the user is prompted to input words without spaces and the program will determine which letter appears latest in the alphabet. It is suggested to use a while loop and check for lower-case letters by subtracting 32 or using the toupper() function. The isalpha() function can also be used to check if the current letter is in the alphabet.
  • #1
Peter P.
23
0

Homework Statement


The problem that was given was to prompt the user to type words with no spaces in between, and end it by pressing enter. From there, the program would calculate which letter comes the latest in the alphabet and is included in what the user types in.

example.
user types in: ABCDcduUlkO<enter>

program determines that 'u' appears latest in the alphabet based on ASCII values (also if there are either a capitalized or minimized version of a letter, they will be treated as having the same value, but the first one that appears will be displayed).

Homework Equations





The Attempt at a Solution


The problem that i am having is only with the input part, scanf. we are forced to used things we only learned in the lectures or up to a certain point in the textbook. normally i would use arrays to do this, but we didn't get that far yet.
so i know that scanf takes in the first character that is pressed only when you ask for input on a type char variable. is there any way around this or even for the program to do calculations after each keypress until they press enter?
 
Physics news on Phys.org
  • #2
I would do this by putting the call to scanf inside a while loop. After a character is read from input, determine whether it is a lower-case letter. If so, subtract 32 from it to get its upper-case version. (There is also a toupper() functon that you can use - its prototype is in ctype.h.)
If the character's ASCII value is larger than 64, the ASCII value for the character just before 'A' (65), this character is the new candidate for being latest in the alphabet, so store it in some variable that keeps track of the highest letter.

Loop through all of the letters in the input string. Once the letter read is the newline character '\n', exit the loop.

It would also be a good idea to check that the current letter is actually in the alphbet. The isalpha() function can be used for this. It's also in the ctype.h header.
 

FAQ: Solving Confusion about Scanf Homework

What is the purpose of a scanf homework?

The purpose of a scanf homework is to practice using the scanf function in the C programming language. This function allows the user to input data from the keyboard, which is essential in many programs.

Why do I keep getting errors when using scanf?

There are a few common errors that can occur when using scanf, such as forgetting to include the ampersand (&) before a variable or not using the correct format specifier. Make sure to carefully review your code and check for any mistakes.

How can I debug my scanf homework?

Debugging scanf homework can be done by using print statements to check the values of variables and ensure they are being properly assigned. You can also use a debugger tool to step through the code and identify any errors.

Can I use scanf for other data types besides integers?

Yes, scanf can be used for other data types such as characters, strings, and floating point numbers. However, you must use the correct format specifier (%c for characters, %s for strings, and %f for floating point numbers) to properly read in the data.

Are there any alternatives to using scanf for user input?

Yes, there are other functions that can be used for user input in C, such as fgets and gets. These functions have different syntax and may be more suitable for certain situations. It is important to understand the differences between these functions and choose the appropriate one for your program.

Similar threads

Replies
10
Views
2K
Replies
9
Views
4K
Replies
2
Views
1K
Replies
10
Views
2K
Replies
1
Views
2K
Replies
12
Views
1K
Replies
6
Views
3K
Back
Top