CS1061 C Programming Lecture 15: More on Characters and Strings A. O’Riordan, 2004.

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Command-line arguments CS 201 Fundamental Structures of Computer Science.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
1 CS 201 String Debzani Deb. 2 Distinction Between Characters and Strings When using strcat, one may be tempted to supply a single character as one of.
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
CS Nov 2006 C-strings.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
Computer Science 210 Computer Organization Strings in C.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
1 Chapter 10 Characters, Strings, and the string class.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Command Line Arguments plus Variable-Length Arrays Systems Programming.
CS50 Section 2 21 September 2015 Sam Green ’17 (646)
File IO and command line input CSE 2451 Rong Shi.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Cryptography.
Chapter 9 Strings. Learning Objectives An Array Type for Strings – C-Strings Character Manipulation Tools – Character I/O – get, put member functions.
An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
Characters and Strings
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
1 Homework Continue with K&R Chapter 5 –Skipping sections for now –Not covering section 5.12 Continue on HW5.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Week 6 - Friday.  What did we talk about last time?  Pointers  Passing values by reference.
Lecture 3: Getting Started & Input / Output (I/O)
C Characters and Strings
Characters and Strings
Functions, Part 2 of 2 Topics Functions That Return a Value
Command Line Arguments
Command line arguments
Command Line Arguments
Understand argc and argv
Structure of C Programs
Command Line Arguments
Programming Fundamentals Lecture #7 Functions
Command-Line Arguments
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Command-line Arguments
Computer Science 210 Computer Organization
Command Line Arguments
C Stuff CS 2308.
sscanf()- string scan function
Functions, Part 2 of 3 Topics Functions That Return a Value
C By Example The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass.
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Characters and Strings Functions
Characters and Strings
Strings Adapted from Dr. Mary Eberlein, UT Austin.
C Characters and Strings
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

CS1061 C Programming Lecture 15: More on Characters and Strings A. O’Riordan, 2004

Text parsing C has routines to perform useful tests and manipulations of character data Each function receives a character or EOF(-1) as an argument Need to include header file ctype.h Following is a program that analyses a line of text for its constituent words: - variable nwords (the return value from get_num_words) is the number of words which the function finds. - variable line is the text we're breaking into words - for example, the string “quick brown fox” has 3 words.

Text parsing (2) #include int get_num_words(char *); int main(){ int num_words; char *line = "This is the line"; num_words = get_num_words(line); printf("There are %d words in total\n", num_words); }

Text parsing (3) int get_num_words(char *line) { char *p = line; int nwords = 0; while(1){ while(isspace(*p)) p++; if(*p == '\0') return nwords; while(!isspace(*p) && *p != '\0') p++; nwords++; if(*p == '\0') return nwords; }

Character Processing

atoi() for string to integer functions are available for converting strings of numerals into numeric types and vice versa. Need to include stdlib.h The string "123" is not the same as the integer 123. When we have a string of digits, we can convert it to the corresponding integer by calling the standard routine atoi(). int i,j; char string[] = "123"; i = atoi(string); j = atoi("456");

itoa() for integer to string /* convert an integer to a string */ #include int main(){ int num; char buff[20]; printf("Enter an integer: "); scanf("%d", &num); printf("As a string it is %s\n", itoa(num, buff, 10)); }

String Conversions

A strcat() Example The standard library function strcat concatenates strings - appends one string onto the end of another, i.e it doesn’t create a new string. Here's an example: char string1[20] = "Hello, "; char string2[] = "world!"; strcat(string1, string2); printf("%s\n", string1); Note: string1 here must be long enough to contain both string1 and string2

Command Line Arguments giving a program some variable input to work on is by invoking it with command line arguments. C's model of the command line is that it consists of a sequence of words, typically separated by whitespace. Your main program can receive these words as an array of strings, one word per string. All you have to do to receive it is to declare main as accepting two parameters: int main(int argc, char *argv[]) {...} Variable argc is a count of the number of command-line arguments, and argv is an array of the arguments themselves.

Command Line Arguments (2) set of `words' making up the command line includes the name of program, argv[0] points to the name of your program, argv[1] points to the first argument, etc. This example simply prints its arguments and is called like this: >prog first_arg second_arg #include int main(int argc, char *argv[]){ int i; for(i = 0; i < argc; i++) printf("arg %d: %s\n", i, argv[i]); }