- 2073 Shrawan · 6 marks
Display characters between given ranges
Short answer
Read two characters, swap if out of order, then loop from start to end printing each character separated by spaces.
Write a C program to display all characters between a given ranges.
Characters are stored as ASCII codes, so a char variable can be used in a for loop like an integer. The loop starts at the first character and goes up to the last one, printing each. If the user gives the range in reverse order, the two are swapped.
#include <stdio.h>
int main(void)
{
char start, end, ch;
printf("Enter starting and ending characters: ");
scanf(" %c %c", &start, &end);
if (start > end)
{
ch = start; start = end; end = ch; /* swap if given in reverse */
}
for (ch = start; ch <= end; ch++)
printf("%c ", ch);
printf("\n");
return 0;
}
Output for the range D to L:
Enter starting and ending characters: D E F G H I J K L
The same program with a and e prints a b c d e, and with 0 and 9 prints the digits.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Rewrite string characters alphabetically · Computer Programming, chapter 7
- Pass student array; display passed students · Computer Programming, chapter 7
- Reverse array and display via pointer · Computer Programming, chapter 9
- Display records sorted by name from file · Computer Programming, chapter 10
- GOTO types; sum of series program · Computer Programming, chapter 11
- Reverse a string using functions · Computer Programming, chapter 7 · asked 2×
- Display N characters using default arguments · Object Oriented Programming, chapter 3
- 8086 ALP: display only alphabetic characters · Microprocessor, chapter 3
- Display 0 to 9 on seven segment · Embedded System, chapter 9 · asked 2×
- Write and display 10 employees using file · Object Oriented Programming, chapter 8