Strings

Strings - STRINGS Dr. David A. Gaitros dgaitros@fsu.edu...

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

View Full Document Right Arrow Icon
STRINGS Dr. David A. Gaitros dgaitros@fsu.edu
Background image of page 1

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

View Full DocumentRight Arrow Icon
C-Style Strings Essentially an array of characters Surrounded by single quotes (‘) is a character Surround by double quotes (“) is a string Strings are pointers First character is index zero (0) The name of the string and the address of the first index are the same. The size of the character string must be large enough to hold all of the characters plus the termination character (\0). All character arrays must be terminated by the null character “\0”.
Background image of page 2
C-Style Strings Examples A character array with no size definition. char color[] = “blue”; A character array with size definition. char color[5] = “blue”; A pointer to a character with initialization. char *colorptr = “blue”;
Background image of page 3

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

View Full DocumentRight Arrow Icon
C-Style Strings char word[20]; cin >> word; Input string cannot be any larger than 19 because of the need for the termination character (\0); The above will read characters until it finds a space, tab, newline or end-of-file. cin >> setw(20) >> word; The above reads in up to 19 characters and saves the last character for the termination null character.
Background image of page 4
C-Style Strings char sentence[80]; cin.getline(sentence, 80, ‘\n’); The above will read until the delimiter character (‘\n’) or end-of-file chacter. Number of characters read is one less than the array size passed (80). When the delimiter character is read (‘\n’), it will be discarded. The third argument defaults to (‘\n’). The following line of code is equivalent. cin.getline(sentence,80);
Background image of page 5

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

View Full DocumentRight Arrow Icon
C-Style Strings In <stddef.h> char *strcpy( char * s1, const char *s2); char *strncpy( char * s1, const char *s2, size_t n); char strcat( char *s1, const char *s2); char strncat(char *s1, const char *s2, size_t n); int strcmp (char *s1, const char *s2); int strcmp (char *s1, const char *s2, size_t n); // 0 if ==, neg if s1 < s2, pos if s1>s2) size_t strlen (const char * s); // Number of characters preceeding the (\0)
Background image of page 6
C-Style String Some character functions . The following return non-zero if true and zero if false: int isalunm(char c); int isalpha( char c); int iscontrl( char c); int isdigit (char c); int isgraph (char c); int islower( char c); int isupper (char c); int isprint (char c); int ispunct (char c); int isxdigit (char c);
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 26

Strings - STRINGS Dr. David A. Gaitros dgaitros@fsu.edu...

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

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