1 / 13

Input and Output of Character Strings

Learn how to display and input character strings using format specifiers in C. Explore the use of %s and %[^<br>] format specifiers, as well as additional alignment and formatting features. Discover helpful functions like clearInput() and getString().

wemma
Download Presentation

Input and Output of Character Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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 80 + 1 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 15 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: http://cs.senecac.on.ca/~btp100/pages/content/strin.html http://cs.senecac.on.ca/~btp100/pages/content/valid.html http://www.mkssoftware.com/docs/man1/printf.1.asp

More Related