CSCI-1190: Beginning C Programming For Engineers: Lecture 5: Characters and Strings Gang Chen
This document summarizes a lecture on characters and strings in the C programming language.
1) Characters are small integers from 0-255 that represent ASCII codes. Character constants denote corresponding characters like '0' and '\n'.
2) Strings have two interpretations - arrays of characters or pointers pointing to characters. Strings are null-terminated with '\0'.
3) Common string functions include strlen(), strcmp(), and strcpy(). Input/output of strings uses scanf() and printf().
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
26 views
CSCI-1190: Beginning C Programming For Engineers: Lecture 5: Characters and Strings Gang Chen
This document summarizes a lecture on characters and strings in the C programming language.
1) Characters are small integers from 0-255 that represent ASCII codes. Character constants denote corresponding characters like '0' and '\n'.
2) Strings have two interpretations - arrays of characters or pointers pointing to characters. Strings are null-terminated with '\0'.
3) Common string functions include strlen(), strcmp(), and strcpy(). Input/output of strings uses scanf() and printf().
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9
CSCI-1190: Beginning C
Programming for Engineers
Lecture 5: Characters and Strings Gang Chen Characters • Type char • Characters are small integers (0-255) • Character constants are integers that denote corresponding characters – '0','1','A','B','a','b','\n' • ASCII code maps characters to integers ASCII Code 0 1 2 3 4 5 6 7 8 9 48 49 50 51 52 53 54 55 56 57 A B C D E F G H I J 65 66 67 68 69 70 71 72 73 74 a b c d e f g h i j 97 98 99 100 101 102 103 104 105 106 NULL BEL TAB NEWLINE SPACE (\0) (\g) (\t) (\n) 0 7 9 10 32 Strings • Two interpretations – Arrays whose elements are characters – Pointers pointing to characters • Strings are always terminated with a NULL character ('\0' or 0) char a[]="hello\n"; /* size? */ char* b=“hello\n”;
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
*b *(b+1) *(b+2) *(b+3) *(b+4) *(b+5) *(b+6) h e l l o \n null 104 101 108 108 111 10 0 Input/Output with Strings 1. #include <stdio.h> 2. int main() 3. { 4. char s[10]; 5. scanf("%s",s); /* bad */ 6. scanf("%9s",s); /* good */ 7. printf("%s",s); 8. return 0; 9. } String Operations • The header file string.h contains functions that work with strings – strlen(a): number of characters in a – strcmp(a,b): compares a and b, returning 0 if they are equal • NOT a==b; – strcpy(a,b): copies contents of b into a • NOT a=b; In-Class Exercise 5-1 • Write a program that reads a line of text from the keyboard using the gets() funtion, then prints it out backwards. For instance, if you type in the line: Test data The result should be: atad tseT The syntax for the gets() function: char s[100]; gets(s); Make use of the strlen() function Converting Numeric Strings to Numbers • From characters to digits /* a is a character */ n=a-'0'; /* n is now a digit */ • From digits to numbers 3492 = 2+9*10+4*100+3*1000 = 2+10*(9+10*(4+10*3)) In-Class Exercise 5-2 • Write a program that reads an integer represented as a string from the keyboard, then coverts the string to the integer and prints it out. • Notes: – You must use scanf("%s",s) to read a string – Do NOT use atoi() or any other similar functions