-
Notifications
You must be signed in to change notification settings - Fork 0
/
Character_Array_Notation.c
108 lines (72 loc) · 3.58 KB
/
Character_Array_Notation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*######################################################################
# Character Array Notation #
########################################################################
# Sinwindie #
########################################################################
# Program will prompt the user for a line of text up to 50 characters. #
# It will then output the text in all uppercase letters and once again #
# in all lower case letters. #
########################################################################
# Last Updated: October, 28 2018 #
######################################################################*/
#include <stdio.h>
#include <ctype.h>
int main(void) /* begin main*/
{
/* Function Prototypes */
void safer_gets (char array[], int max_chars);
/* Variable Declarations */
char text[51]; /* Holds characters entered by user */
char *text_ptr; /* Pointer to characters entered by user */
char c; /* For clearing input buffer */
int count = 0; /* Counter for looping */
/* Prints introductory lines and asks for user input */
printf("Welcome to the Character Array Notation program.\n");
printf("\nEnter a line of text (up to 50 characters): ");
/*Takes user input and assigns it to text[50] using safe_gets */
safer_gets(text, 50);
/* Prints user entered text in all uppercase characters */
printf("\nThe line of text in uppercase is:\n");
for(text_ptr = text; *text_ptr != '\0'; text_ptr++)
putchar(toupper(*text_ptr));
/* Prints user entered text in all lowercase characters */
printf("\n\nThe line of text in lowercase is:\n");
for(text_ptr = text; *text_ptr != '\0'; text_ptr++)
putchar(tolower(*text_ptr));
/* Prints closing message */
printf("\n\nThank you for using the Character Array Notation program.\n\n");
/* Ensures output screen isn't erased too fast when using other compilers */
getchar();
return 0;
} /* end main */
/*------------------------------------------------------------------------------
safer_gets checks string data taken as input to prevent overflow caused by the
user entering more characters than is permitted.
------------------------------------------------------------------------------*/
void safer_gets (char array[], int max_chars)
{ /* Begin safer_gets */
/* Variable Declarations */
int i;
/* Read info from input buffer, character by character, */
/* up until the maximum number of possible characters. */
/* ------------------------------------------------------ */
for (i = 0; i < max_chars; i++)
{ /* Begin for */
array[i] = getchar();
/* If "this" character is the carriage return, exit loop */
/* ----------------------------------------------------- */
if (array[i] == '\n')
break;
} /* end for */
/* If we have pulled out the most we can based on the size of array, */
/* and, if there are more chars in the input buffer, */
/* clear out the remaining chars in the buffer. */
/* ---------------------------------------------------------------- */
if (i == max_chars )
if (array[i] != '\n')
while (getchar() != '\n');
/* At this point, i is pointing to the element after the last character */
/* in the string. Terminate the string with the null terminator. */
/* -------------------------------------------------------------------- */
array[i] = '\0';
} /* end safer_gets */