day14 - Text processing 1 Characters char: A type...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
    1 Text processing
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
    2 Characters char : A type representing single characters. Individual characters inside a string are stored as char values. Literal char values are surrounded with apostrophe (single-quote) marks, such as 'a' or '4' or '\n' or '\'' Like any other type, you can create variables, parameters, and returns of type char . char letter = 'S'; printf(" %c ", letter); // S
Background image of page 2
    3 Strings A string is just an arrays of char s. Example: the string "letters" Strings have an additional character ( '\0' ) called the null character at the end. Why? index 0 1 2 3 4 5 6 7 value 'l' 'e' 't' 't' 'e' 'r' 's' '\0'
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
    4 The null character Since arrays have no size information, the null character is necessary to mark the end of the string. What does this output? #include <stdio.h> int main() { printf("The character \0 ends the string."); } Output : The character
Background image of page 4
    5 Storing strings Strings are just arrays of char s . char word[] = "awesome"; You can also specify a larger array size in case you want to add more to the string later. char word[100] = "awesome"; Remember that if you specify the size of the array, it must be at least one greater than the size of the string to accommodate the null character. char word[7] = "awesome"; // what is the bug?
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
  6 Assigning strings Since strings are arrays of chars, you can't just assign them to each other. char word1[] = "hello"; char word2[] = "goodbye"; word1 = word2; // won't work You have to copy the source string to the destination buffer using strcpy . Must include
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 20

day14 - Text processing 1 Characters char: A type...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online