CSC 270 Survey of Programming Languages. C-String Values

Similar documents
8. Characters, Strings and Files

LESSON 4. The DATA TYPE char

Strings. Daily Puzzle

Sample Basic C Programs

Course organization. Course introduction ( Week 1)

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

Structured programming

Array Initialization

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

Computer Science & Engineering 150A Problem Solving Using Computers

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

Characters and Strings

ONE DIMENSIONAL ARRAYS

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

Chapter 8: Character & String. In this chapter, you ll learn about;

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Chapter 8 C Characters and Strings

Chapter 8 - Characters and Strings

CS1100 Introduction to Programming

Introduction to string

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Scientific Programming in C V. Strings

Strings and Library Functions

Strings. Arrays of characters. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY

C: How to Program. Week /May/28

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

Computers Programming Course 11. Iulian Năstac

Using Character Arrays. What is a String? Using Character Arrays. Using Strings Life is simpler with strings. #include <stdio.

C Characters and Strings

Create a Program in C (Last Class)

Personal SE. Functions, Arrays, Strings & Command Line Arguments

Overview. Concepts this lecture String constants Null-terminated array representation String library <strlib.h> String initializers Arrays of strings

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

scanf erroneous input Computer Programming: Skills & Concepts (CP) Characters Last lecture scanf error-checking our input This lecture

C Programming. Unit 9. Manipulating Strings File Processing.

C mini reference. 5 Binary numbers 12

C Strings. Abdelghani Bellaachia, CSCI 1121 Page: 1

CSC 2400: Computer Systems. Arrays and Strings in C

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

CSC 2400: Computer Systems. Arrays and Strings in C

Computer Programming: Skills & Concepts (CP) Strings

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

Strings. Chuan-Ming Liu. Computer Science & Information Engineering National Taipei University of Technology Taiwan

Intermediate Programming, Spring 2017*

LAB7 : Characters and Strings

Arrays, Strings, & Pointers

CSC 270 Survey of Programming Languages. Input and Output

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

C programming basics T3-1 -

String constants. /* Demo: string constant */ #include <stdio.h> int main() {

Chapter 8 Character Arrays and Strings

Week 5. Muhao Chen.

Quiz 0 Answer Key. Answers other than the below may be possible. Multiple Choice. 0. a 1. a 2. b 3. c 4. b 5. d. True or False.

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

0 vs NULL // BBM 101. Introduction to Programming I. Lecture #12 C Pointers & Strings

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

Approximately a Test II CPSC 206

Iteration. Side effects

CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

o Echo the input directly to the output o Put all lower-case letters in upper case o Put the first letter of each word in upper case

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS

CS 33. Introduction to C. Part 5. CS33 Intro to Computer Systems V 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

CS107 Spring 2019, Lecture 4 C Strings

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

CS107, Lecture 4 C Strings

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

String can be represented as a single-dimensional character type array. Declaration of strings

CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings

Fundamentals of Programming & Procedural Programming

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17

C PROGRAMMING. Characters and Strings File Processing Exercise

C-String Library Functions

Functions: call by reference vs. call by value


CSE 374 Midterm Exam 11/2/15. Name Id #

Reminder. Sign up for ee209 mailing list. Precept. If you haven t received any from ee209 yet Follow the link from our class homepage

Strings. Steven R. Bagley

CS360 Midterm 1 - February 21, James S. Plank. Put all answers on the answer sheet. In all of these questions, please assume the following:

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

Chapter 13. Strings. Introduction

C Programming Language

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ARRAYS(II Unit Part II)

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program which consists of two function definitions including the main function.

CS 137 Part 6. ASCII, Characters, Strings and Unicode. November 3rd, 2017

File IO and command line input CSE 2451

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

Multiple Choice Questions ( 1 mark)

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

BSM540 Basics of C Language

1 Pointer Concepts. 1.1 Pointer Examples

Section 3: Library Functions

Lecture 05 Pointers ctd..

Transcription:

CSC 270 Survey of Programming Languages C Lecture 4 Strings C-String Values The most basic way to represent a string of characters in C++ is using an array of characters that ends with a null byte. Example printf("hello"); H e l l o \0 1

C-String Variables A C-string variable is an array of characters that is expected to be terminated with a null byte ('\0'). Example char s[10]; strcpy(s, "Hello"); s[0]s[1]s[2]s[3]s[4]s[5] s[6]s[7]s[8]s[9] H e l l o \0???? Writing: char Initializing C Strings yourstring[11] = "Do Be Do"; char mystring[] = "Do Be Do"; will initialize both strings with Do Be Do with a null byte immediately after Do Be Do. Writing char s[] = "abc"; and char s[] = {'a', 'b', 'c', '\0'; produce the same string 2

Pitfall: Using = C-strings and C-string variables are different from other data types. While you can declare a string by writing char s[] = "Hello"; You cannot write char s[10]; s = "Hello"; Pitfall: Using == C-strings and C-string variables cannot be compared correctly using ==. If you write if (s1 == s2) printf("the same"); else printf("different"); you are comparing their starting addresses. Instead, use if (strcmp(s1, s2) == 0) 3

Predefined Functions in <string.h> Function Description Caution strcpy(s, t) Copies t into s No bounds checking strncpy(s, t, n) Copies t into s but no more than n characters are copied Not implemented in older versions of c strcat(s, t) Concatenates t to the end of s No bounds checking strncat(s, t, n) Concatenates t to the end of s but no more than n characters strlen(s) strcmp(s, t) Returns the length of s (not counting \0 ) Returns 0 if s == t < 0 if s < t > 0 if s > t strncmp(s, t, n) Same as strcmp but compares no more than n characters Not implemented in older versions of c No bounds checking Not implemented in older versions of c strcpy() strcpy(s, t) copies the contents of t into s. This is standard way of assigning a value to a string, i.e., we copy it character by character instead of copying over the address at which it starts. 4

strcpy() A Example #include <stdio.h> int { main(void) char s[80]; strcpy(s, "Let's learn strings"); printf("%s", s); return(0); Output Let's learn strings strncpy() strncpy(s, t, n) copies the contents of t into s but no more than n-1 characters. This is standard way of assigning a value to a string, i.e., we copy it character by character instead of copying over the address at which it starts. 5

strncpy() An Example #include #include <stdio.h> <string.h> int { main(void) char s[80], t[80]; strcpy(s, "The quick brown fox jumped " "over the lazy dogs"); strncpy(t, s, 10); /* * You need this in case s was more * than 10 characters */ t[10] = '\0'; printf("s = \"%s\"\n", s); printf("t = \"%s\"\n", t); return(0); 6

strcat() strcat(s, t) concatenates s and t, saving the new string in s. Example strcpy(s, "Robert"); strcpy(t, "Michael"); strcat(s, t); printf("s = \"%s\"\n", s); Output s = "RobertMichael" strncat() strncat(s, t, n) concatenates s and t, saving the new string in s. At most it will copy n characters. Example strcpy(s, "Robert"); strcpy(t, "Michael"); strncat(s, t, 5); printf("s = \"%s\"\n", s); Output s = "RobertMicha" 7

strlen() strlen(s) returns the number of characters in the string s. Example strcpy(s, "The quick brown fox"); len = strlen(s); printf("\"%s\" has %d characters.\n", s, len); Output "The quick brown fox" has 19 characters. strcmp() strcmp(s, t) compares s and t returns an integer: If s precedes t in collating sequence, strcmp(s, t) < 0 If s and t are the same, strcmp(s, t) = 0 If s follows t, strcmp(s, t) > 0 8

#include<stdio.h> strcmp() An example char r[] = "The quick brown fox", s[] = "His quick brown fox", t[] = "Your quick brown fox"; int main(void){ printf("strcmp(r, s) = %d\n", strcmp(r, s)); printf("strcmp(r, t) = %d\n", strcmp(r, t)); printf("strcmp(r, r) = %d\n", strcmp(r, r)); return(0); Output strcmp(r, s) = 1 strcmp(r, t) = -1 strcmp(r, r) = 0 strncmp() strncmp(s, t) compares up to the first n characters of s and t returns an integer: If s precedes t in collating sequence, strncmp(s, t) < 0 If s and t are the same, strncmp(s, t) = 0 If s follows t, strncmp(s, t) > 0 9

strncmp() An example #include <stdio.h> char r[] = "The quick brown fox", s[] = "His quick brown fox", t[] = "Your quick brown fox"; int main(void) { printf("strncmp(r, s, 8) = %d\n", strncmp(r, s, 8)); printf("strncmp(r, t, 8) = %d\n", strncmp(r, t, 8)); printf("strncmp(r, r, 8) = %d\n", strncmp(r, r, 8)); return(0); Output strcmp(r, s, 8) = 1 strcmp(r, t, 8) = -1 strcmp(r, r, 8) = 0 strncmp() An example 10

Example: Command-Line Arguments Command line parameters are entered when invoking the program using a command-line interface. Example: myprog This is a test args.c #include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = 0; i < argc; i++) printf("argument #%d is %s\n", i, argv[i]); return(0); 11

Output for args.c C:\Users\Robert M. Siegfried\Documents\Visual Studio 2008\Projects\args\Debug>args This is a test Argument #0 is args Argument #1 is This Argument #2 is is Argument #3 is a Argument #4 is test C:\Users\Robert M. Siegfried\Documents\Visual Studio 2008\Projects\args\Debug> String: Input and Output In addition to scanf() and printf(), there are other input methods available when working with characters and strings: getchar() gets() gets_s() putchar() puts () 12

getchar() getchar() returns a character from the input stream. The progam will for input until the Enter (or Return) key is pressed. It will then return the input from the line character by character. Older versions of getchar() define 0 as the End of File marker (EOF); newer versions use -1 instead; therefore, it is accepted practice to use getchar() to assign the character to an integer variable. getchar() An Example #include <stdio.h> /* main() Copy input to output */ int main(void) { int c; c = getchar(); while (c!= EOF) { printf("%c", c); c = getchar(); return(0); 13

Output From Our Example This is the start of something big This is the start of something big gets () gets() can be used to read an entire string up until the next newline character. Because gets() does not include bounds checking, it was deprecated in the 1999 C standard and made obsolete in the 2011 standard. 14

gets_s() gets_s() is an improved version of gets that does bounds checking. Example int main(void) { char s[81]; gets_s(s, 30); printf("\"%s\"\n", s); return(0); putchar() putchar() displays the next character that it is given as a parameter. Example int main(void) { int c; while ((c = getchar())!= EOF) putchar(c); return(0); 15

puts() puts() will print a string passed as a parameter. Example int main(void) { int s[41]; gets_s(s, 40); puts(s); return(0); Functions in <ctype.h> Function Description Example toupper(c) Returns the upper case version of the character tolower(c) Returns the lower case version of the character isupper(c) Returns true if c is an upper case letter islower(c) Returns true if c is an lower case letter isalpha(c) Returns true if c is a letter isdigit(c) Returns true if c is a digit (0 through 9) c = toupper('a'); c = tolower('a'); if (isupper(c)) printf("upper case"); if (islower(c)) printf("lower case"); if (isalpha(c)) printf("it s a letter"); if (isalpha(c)) printf("it s a number"); 16

Functions in <ctype.h> (continued) Function Description Example isalnum(c) Returns true if c is alphanumeric isspace(c) Returns true if c is a white space character ispunct(c) Returns true if c is a printable character other than number, letter or white space isprint(c) Returns true if c is a printable character isgraph(c) Returns true if c is a printable character other an white space if (isalnum('3')) printf("alphanumeric"); while (isspace(c)) c = getchar(); if (ispunct(c)) printf("punctuation"); isctrl(c) Returns true if c is a control character In C, Characters are Integers In many ways, C considers characters to be 8- bit unsigned integers. For this reason, many string functions return an int value. Using printf to write toupper('a') will not only write 'A' but the numeric code that represents 'A' depending on the field specifier that you use. 17

toupper() An Example printf("%d\t %c\n", toupper('a'), toupper('a')); Output 65 A String Processing in C Despite C's lack of direct support of strings. it can be used easily to handle all kinds of string processing. 18

mycount.c #include #define YES 1 #define NO 0 /* * main() - Count the lines, words and * characters in the input */ int main(void) { int c, nl, nw, nc, inword; inword = NO; /* We aren't in a word yet */ nl = nw = nc= 0; while((c = getchar())!= EOF) { nc++; if (c == '\n') nl++; if (c == ' ' c == '\n' c == '\t') inword = NO; /* Our word ended */ else if (inword == NO) { /* New word started */ inword = YES; nw++; 19

printf("lines = %d\tword = %d\t "characters = %d\n",nl, nw, nc); return(0); CountDigits.c #include <stdio.h> /* * main() - Count digits, whitespace, and other * characters */ int main(void) { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i < 10; i++) ndigit[i] = 0; 20

while ((c = getchar())!= EOF) /* c - '0' is the value of the digit */ if ('0' <= c && c <= '9') ++ndigit[c-'0']; else if (c == ' ' c == '\t' c == '\n') nwhite++; else nother++; printf("digits = "); for (i = 0; i < 10; i++) printf(" %d ", ndigit[i]); printf("\nwhite space = %d\t" "other = %d\n", nwhite, nother); return(0); squeeze.c #include <stdio.h> char line[] = {"This is the start of a very big deal of a line"; void squeeze(char s[], char c); int main(void) { printf("my line is:\n\"%s\"\n", line); squeeze(line, 'i'); printf("my line is:\n\"%s\"\n", line); return(0); 21

/* * squeeze() - delete all c's from s */ void squeeze(char s[], char c) { int i, j; for (i = j = 0; s[i]!= '\0'; i++) if (s[i]!= c) s[j++] = s[i]; s[j] = '\0'; strlen() /* * strlen() Return the length of string s */ int strlen(char s[]) { int i; /* Advance to the null byte at the end */ for (i = 0; s[i]!= '\0'; i++) ; return(i); 22

strcpy() /* * strcpy() Copy t into s one character at a time */ void strcpy(char s[], char t[]) { int i; /* Until we reach the final null byte */ for (i = 0; t[i]!= '\0'; i++) /* Copy the current character */ s[i] = t[i]; s[i] = '\0'; strcat() /* * strcat() Concatenate t to s */ void strcat(char s[], char t[]) { int i, j; /* Skip to the end of s */ for (i = 0; s[i]!= '\0'; i++) ; /* Copy t nto the end of s */ for (j = 0; t[j]!= '\0'; i++, j++) s[i] = t[j]; s[i] = '\0'; 23

atoi() /* * atoi() - The function accepts a character string * and translates it into the integer that it * represents. It assumes that the first * nondigit is the end of the number. */ int atoi(char s[]) { int i, n = 0; for (i = 0; '0' <= s[i] && s[i] <= '9'; i++) n = 10*n + s[i] - '0'; return(n); lower() /* * lower() - Convert a character to lower case */ int lower(int c) { if ('A' <= c && c <= 'a') return(c + 'a' - 'A'); else return(c); 24

char text[] = "Fourscore and seven years ago"; text F o u r s c o r e a n d s e v e n y e a r s a g o \0 25

char dest[20]; dest char *s, *t; s? t? 26

strncpy(dest, "abcd", 10); dest a b c d \0 memset(dest, '\0', sizeof(dest)); dest \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 27

t = strstr(text, "score"); text t F o u r s c o r e a n d s e v e n y e a r s a g o \0 strncpy(dest,t, 10); t s c o r e a n d s e v e n dest s c o r e a n d \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 28

dest[10] = '\0'; t s c o r e a n d s e v e n dest dest[10] s c o r e a n d \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 substring.c #include #include <string.h> <stdio.h> char text[] = "Fourscore and seven years ago"; int main(void) { char dest[20]; char *s, *t; int i; strncpy(dest, "abcd", 10); printf("%s\n", dest); 29

memset(dest, '\0', sizeof(dest)); t = strstr(text, "score"); strncpy(dest,t, 10); dest[10] = '\0'; for (i = 0; i < 20; i++) putchar(dest[i]); putchar('\n'); for (i = 0; i < 20; i++) printf("%d ", (int)dest[i]); putchar('\n'); for (i = 0; i < 20; i++) printf("%d ", (int)t[i]); putchar('\n'); printf("%s\n", t); printf("%s\n", dest); return(0); 30

The output from substring.c abcd score and 115 99 111 114 101 32 97 110 100 32 0 0 0 0 0 0 0 0 0 0 115 99 111 114 101 32 97 110 100 32 115 101 118 101 110 32 121 101 97 114 score and seven years ago score and strstr() Example /* strstr example */ #include <stdio.h> #include <string.h> int main () { char str[] = "This is a simple string"; char * pch; int i; 31

pch = strstr (str,"simple"); puts(pch); i = pch-str; printf("i = %d\n", i); strncpy (pch,"sample",6); puts (str); return 0; 32