130 likes | 149 Views
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().
E N D
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”); }
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);
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)
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);
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...
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);
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...
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.
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);
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)...
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 */
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