I/O CHARACTERS AND STRINGS



I/O - Characters and Strings

Authored by: John Even

Faculty Supervisor: Al Day

HTML Documentation by: Julie Sandberg

Date last updated: 9/16/95


Character Input/Output

Variables of type char can be input and output in the same fashion as variables of types int and double.

e.g.:
	scanf("%c", &initial);
	printf("The initial is %c", initial);
%c:
conversion specification for character type I/O

initial:
character variable

Character I/O Example

#include < stdio.h > /* printf, scanf
definitions */

int main (void) {
char let_1, let_2, let_3; /* three letters */
int year; /* current year */
FILE *outp; /* pointer to output file */

outp = fopen("output.txt", "w");
printf("Enter a 3-letter nickname and press 
return> ");
scanf ("%c%c%c", &let_1, &let_2, &let_3);
printf("Enter the current year and press
return> ");
scanf("%d", &year);
printf("Welcome, %c%c%c.  %d is a great year 
to study C!\n",
 let_1, let_2, let_3, year);
fprintf(outp, "%c%c%c", let_1, let_2, let_3);
fclose (outp);
return (0);
}

Character I/O Example Output

Program Output:

Enter a 3-letter nickname and press return> Bob
Enter the current year and press return> 1995
Welcome, Bob. 1995 is a great year to study C!


The program also outputs the 3 characters to a data file.



But...How useful are single characters?
Is there a better way to manipulate words?


What is a string?

Strings are a grouping of characters. Many programs are concerned with the manipulation of textual data.

We have already used string constants. Consider the call:

	printf("The answer is: %f", var);
The first part in the quotes is the string constant "The answer is:", a string of 15 characters. Blank spaces ARE counted.


String Declaration

Strings are implemented as arrays of type char. So you declare an array like:
	char string[30];
This will hold a string of up to 29 characters in length.

All strings of characters are termined with the null character '\0'. The null character marks the end of the string, allowing the different C functions to know where the end of the string is. All characters following the '\0' are ignored.


String Initialization

Strings can be initialized with either a brace enclosed character list, or a string constant.
e.g.  char str[4] = {'U', 'S', 'A', '\O';}



char str[20] = "Initial value";



String I/O

 Format: scanf("%s", pvar);
	    printf("The string is: %s", pvar);
%s -- type declaration for string I/O
pvar -- string variable

Like int, double, and char, there are
certain options that can be used with strings.

The & is not used with the variable name for string input.


String I/O Options

A number can be used just like the other types of output. This number specifies the minimum field width, with the string being right-justified within the field if the string is shorter than the number.
	e.g.  printf("%5s", string);
This would cause the string to be right-justified if less than 5 characters in length. If the string is longer than 5 characters, the field would be expanded to accommodate it exactly.


String - Operator

If it is undesirable to have the string right-justified, a special minus sign prefix can be added. This will left-justify the string.
     e.g.  printf("%-20s", vegetable_type);
This is useful whenever the situation calls for left-justification of strings.


Example

#include < stdio.h >
#define STRING_LEN 10

int  main(void) {
    char dept [STRING_LEN];
    int course_num;
    char days [STRING_LEN];
    int time;

    printf("Enter department code, course number, days ");
    printf("and time like this: \n> ENGR 160 TR 1200\n> ");
    scanf("%s%d%s%d", dept, &course_num, days, &time);
    printf("%s %d meets %s at %d\n", dept, course_num,
		days, time);
    return (0);
}
Enter department code, course number, days and time like this:
> ENGR 160 TR 1200
> MATH 267 MTRF 300
MATH 267 meets MTRF at 300


FGETS

As you may have noticed, during input, scanf ignores white space. This can cause problems if our program needs the ability to have white space in the midst of user entered strings. The solution to this problem is the function fgets in stdio.h.

FGETS Example

Form:

	#define COLUMNS 81
		.
		.
		.
	char line [COLUMNS];
	char *status;
	FILE *inp;
	inp=fopen("sample.txt", "r");
	if(status  == NULL) {
 	    printf("End-of-file encountered or read");
	    printf("error ocurred.\n");
	}

FGETS Explanation