Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 9 – Lesson 2 Input & Output of Character Strings

Similar presentations


Presentation on theme: "Week 9 – Lesson 2 Input & Output of Character Strings"— Presentation transcript:

1 Week 9 – Lesson 2 Input & Output of Character Strings
IPC144 Week 9 – Lesson 2 Input & Output of Character Strings

2 Displaying Character Strings
In the previous lesson, we could use a for loop with the format specifier “%c” to display each element of the array, since a character string is an “array of characters”. #include<stdio.h> #define SIZE main() { int i; word1[SIZE] = “hello”; for (i=0; i < SIZE; i++)‏ printf (“%c “, word1[i]); printf (“\n\n”); }

3 Displaying Character Strings
The printf() statement also allows the “%s” format specifier to display the entire character string that is stored in an array of characters. This is extremely convenient since you do NOT require a for loop to display each element in the array of characters. eg. printf (“%s\n\n”, word1);

4 Displaying Character Strings
The “%s” format specifier has additional string alignment and formatting features: “%s” - right justify (0 character offset) “%-s” - left justify (0 character offset) “%25s” - right justify (25 character offset) “%-25s” - left justify (25 character offset)‏ “%-25.15s” - left justify (display no more than characters)

5 Inputting Character Strings
The “%s” format specifier can also be used to read into an array when using the scanf() statement. Note: Since when passing an array to a function, it actually passes a “pointer” to that array, you do NOT use the & symbol. Limitation: “%s” alone does not read multiple words (i.e. words delimited by a space, etc..)‏ eg char word[SIZE+1]; scanf(“%s”, word);

6 Inputting Character Strings
The “%[]” format specifier is used to read in a string, but characters within the square brackets are allowed, and the symbol ^ followed by characters within [] indicates any string except those characters. “%[yYnN]” “%[^0-9]” In this case, if any character not allowed in above examples, then scanning of input stops, and the string value will be terminated...

7 Inputting Character Strings
The “%[^\n]” format specifier can solve the previous problem of scanning multiple words (i.e. reading in white space characters like space). The following method is useful to read a character string with multiple words, but not to include <ENTER> or new-line eg char word[SIZE+1]; scanf(“%[^\n]”, word);

8 Inputting Character Strings
The “%[]” format specifier can also specify how many characters to accept in a line. eg. “%30[^\n]” If more than 30 characters entered, then when <ENTER> is pressed, only first 30 characters assigned to string...

9 Inputting Character Strings
When using scanf with “%s” or “%[^\n]” format specifiers, a function should be created to clear the “\n” character that is stored for the next read. This clearInput function should be called immediately after using scanf function to read in a string.

10 Inputting Character Strings
void clearInput(void)‏ { while(getchar() != '\n')‏ ; } printf ("Please enter string #1: "); scanf ("%[^\n]", x); clearInput(); printf ("Please enter string #2: "); scanf ("%[^\n]", y);

11 Inputting Character Strings
The gets() function is like the getchar() function, except gets() reads in the entire string. eg. gets(name); Similar to scanf(“%[^\n]\n”, name); gets does not know the number of characters passed into the array, so allow a sufficient size to store characters into the array! It might be better to stick to scanf, or functions like getchar() to build a more robust string entry function (see next slide)...

12 Inputting Character Strings
void getString(char x[])‏ { int n=0; char c; while('\n' != (c = getchar()))‏ if (n < SIZE)‏ x[n++] = c; x[n] = '\0'; } /* End of getString function */

13 Additional Resources Here are some Related-Links for Interest Only:


Download ppt "Week 9 – Lesson 2 Input & Output of Character Strings"

Similar presentations


Ads by Google