Docsity

Character and String Manipulation in C and Python, Study notes of Software Engineering

Partial preview of the text

Download Character and String Manipulation in C and Python and more Study notes Software Engineering in PDF only on Docsity!

CHARACTERS, STRINGS,

AND FILES

CSSE 120—Rose Hulman Institute of Technology

Characters, Strings, and Files

Characters in C

 C's char type is really a kind of number!

 A char takes 1 byte of storage space

 Example:

char myChar; myChar = 'C'; printf("%c\n", myChar); /* %c is format spec. for char */ printf("%d\n", myChar); /* can print char as a decimal */ printf("%c\n", 67); /* can print int as char */ myChar++; printf("%c\n", myChar); /* What prose do you suppose? */

Ten Ways to Say 'A'

char c = 'A'; int i = 'A'; printf("A"); printf("%c", 'A'); printf("%c", 'B'-1); printf("%c", c); printf("%c", i); putchar('A'); /* can "push" single characters to output */ putchar('C' - 2); putchar(toupper('a')); /* Need to #include <ctype.h> */ putchar(c); putchar(i);

Getting Characters

 To read a single character from the console use:

getchar()  Caveat: getchar() returns an int , either a char value or EOF (end of file) void getSomeChars() { int inChar; int count = 0; printf("\n\nType some text, then press 'Enter': "); fflush(stdout); inChar = getchar(); while (inChar != '\n' && inChar != EOF) { count++; inChar = getchar(); } printf("\nYou entered %d characters.", count); }

Note: most operating systems only pass characters to your program after the user presses the enter key

Character Functions: ctype.h

 Conversion Functions:

int tolower(int c);int toupper(int c);

 Modify

getSomeChars() to:

 print each character entered  print the upper-case version of each character

 Test functions:

isdigit(c)isalpha(c)islower(c)isupper(c)isspace(c)

 Count the number of

spaces entered

See the C Library Reference link on ANGEL under Course Resources for more functions.

Programming Exercise

 Write a function getline()

 Signature: int getline(char s[], int lim);  Spec:  Gets a line of characters from the user and puts up to lim characters of it in the given array. Returns the length of the string.

 Example calling code:

char lname[MAX_NAME]; int len; len = getline(lname, MAX_NAME); printf("\n\n'%s', has %d characters.\n", lname, len);

String Functions: string.h

Function Purpose char *strcpy(char s, char ct) copy string ct to string s, including '\0'; return s char *strcat(char s, char ct) concatenate string ct to end of string s; return s int strcmp(char *cs, char *ct) compare string cs to string ct, return a negative number if cs<ct, zero if cs==ct, or positive otherwise char *strstr(char *cs, char *ct) return a pointer to first occurrence of ct in cs, or NULL if not present size_t strlen(char *cs) return length of cs (size_t is an typedef for int on most systems)

Descriptions from K&R, p. 249. See the C Library Reference link on ANGEL for more.

Summary: Strings in C

 Strings are arrays of characters:

char lname[10]; or  *char fname = "Lou";

 "Null terminated", that is, a '\0' at the end

 Don't forget to reserve enough space to hold the

string

Key Points!

Arrays of Strings

 Arrays of ints:

int x; // an element  int xA[4]; or *int xA; // an array of ints

 Strings are arrays of chars:

char c; //an element  char str[4]; or *char str; // a string, i.e., an array of chars

 So what's an array of strings?

 *char str; // an element  *char strA[4] or **char strA; // an array of strings