Computers and programming The 3 rd lecture Jiří Šebesta.

Slides:



Advertisements
Similar presentations
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Advertisements

Strings.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
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.
Computers and programming The 7 th lecture Jiří Šebesta.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
1 Homework Assignments Turn in HW1 (If not done yet, catch up!) Questions about HW1? Anyone still stuck on apply / UNIX account? Everyone have the books?
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Character Input and Output
Review: midterm #9 #include void main(void) { int c; c = getchar(); if(c>=48){ if(c
C Programming Day 2 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Computers and Programming CPC The 1 st lecture Jiří Šebesta Complete sources are available on the Web pages:
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
CHAPTER 8 CHARACTER AND STRINGS
While Loop Structure while (condition) { …. // This line will process when while condition is true }
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
BBS514 Structured Programming (Yapısal Programlama)1 Character Processing, Strings and Pointers,
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
C-Language Keywords(C99)
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Computers and Programming CPC The 2 nd lecture Jiří Šebesta.
Chapter 18 I/O in C.
Intro to Programming in C. Motivation for Using C  High level, compiler language  Efficiency over user-friendliness  Allows programmer greater freedom,
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)
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
Strings, Pointers and Tools
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
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 may.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Dr. Sajib Datta Sep 8,  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
INC 161 , CPE 100 Computer Programming
ECE Application Programming
C Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
CSE1320 Loop Dr. Sajib Datta
Chapter 18 I/O in C.
Arrays in C.
Input and Output Lecture 4.
توابع ورودي-خروجي.
Chapter 18 I/O in C.
Your questions from last session
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
CS1100 Computational Engineering
Characters and Strings Functions
C Characters and Strings
Chapter 18 I/O in C.
Presentation transcript:

Computers and programming The 3 rd lecture Jiří Šebesta

TOPIC 1.Iteration statements II. 2.Jump statements 3.String terminated by NULL 4.Functions stdio.h for characters and strings 5.Library string.h for strings

Iteration statements II. (1/4) while(test) statement; while Meaning of while is identical with for (initialization and increment outside of the head) Example: Ex23.c float temp[31]={-1.1,-0.3,-7.6,-2.7,-1.2,-2.6,-3.3, … … 2.4};// day temperatures in January unsigned int day = 1; while(temp[day-1]<=0) // pass the loop while temp<=0 day++; printf("%d. January temperature ovecomes 0, it was %3.1f deg. C!\n", day, temp[day-1]); return 0;

Iteration statements II. (2/4) while

Iteration statements II. (3/4) do statement; while(test); do - while Example: Ex24.c int code;// number for password do { printf("Insert access code:\n"); scanf("%d", &code); //number from stdin to variable code } while(code != 12345); // test of proper password printf("\n\nAccess allowed"); printf("\n\nInsert +ENTER for exit"); do//exit after insertion c=getchar(); while (c!=' '); return 0;

Iteration statements II. (4/4) do - while

Jump statements (1/3) Utilization: – in bodies of loops ( for, do, while ); – in body of switch ( switch ). Interrupting a performed iteration cycle: – break: leaving the whole loop – continue: continuing by the next iteration continue, break

Jump statements (2/3) break Example: Ex25.c char c; int code, test, n=3; // n is the number of trials do { printf("Insert access code:\n"); scanf("%d", &code); //number from stdin to variable code n--; //decrementation of the number of trials test=(code == 12345); // test of proper password ”12345” if ((test==0)&&(n==0))break; // if no proper // code and 3 trials done - acces denied } while(!test); // in test is info about access, // 0 means denied, 1 means allowed

Jump statements (3/3) continue Example: divider searching Example: Ex26.c int num; //input number int test, n, m=0; printf("Input number:"); scanf("%d", &num); //number from stdin to variable num for(n=2; n<=100; n++) //loop for n from 2 to 100 { test = (num%n == 0); //test if n is divider if(!test) continue; //if not, next n printf(“\n%d", n); //if yes, print n m++; //number of dividers – incr. } printf(“Number of dividers: %d\n", m);

Strings terminated by null (1/8) String: an array of characters (in memory: 1 byte/character – ASCII coding) NTS (Null Terminated Strings) : the last character of string null – spec. notation v C/C++ ’\0’ Array elements: pointers to characters

Strings terminated by null (2/8) String as an array of ASCII codes of characters in memory: ASCII codes in range worldwide standardized ASCII codes in range 128 – 255 (unsigned) depend on actual character set (national characters, UTF-8, Windows- 1250, ISO-8859-x) – Extended ASCII

Strings terminated by null (3/8) int main( void) { unsigned char c; printf("Actual character set: \n\n"); for(c=32; c =32; c++) printf("\n ASCII code %d = character %c", c, c); scanf("%c", &c); return 0; } Ex. All characters printing including Extended ASCII Example: Ex27.c

Strings terminated by null (4/8) Declaration methods: char strA[5] = {‘A’,’B’,’C’,’D’,’\0’}; – 4 characters + terminating character null char strB[5] = ”ABCD”; – 4 characters, null inserted automatically – the same principle as string in the funtion printf() char *strC = ”ABCD”; - pointer to the first character, the first character points to the second character etc. up to null (this declaration is not recommended – potential conflict in memory)

Strings terminated by null (5/8) Special characters: \b - backspace BS \f - form feed FF (also clear screen) \n - new line NL \r - carriage return CR \t - horizontal tab HT \v - vertical tab (not all versions) \“ - double quotes (not all versions) \' - single quote character ' \\ - backslash character \ \ddd - character ddd, where ddd is an ASCII code given in octal base \xhhh - character hhh, where hhh is an ASCII code given in hexadecimal base

Strings terminated by null (6/8) Example: Ex28.c Ex. Arithmetic manipulation with characters #include int main(void) { char text[] = "Vjku\"oguucig\"ku\"ugetgv#"; unsigned int n; for(n=0; text[n]!='\0'; n++)// all chars in string if(text[n]!=' ') // excluding space text[n] -= 2; // character code shift printf("%s\n", text); scanf("%c", &c); return 0; }

Strings terminated by null (7/8) #include "stdafx.h" #include "stdio.h" #include "string.h" //library for ops. with strings int main(void) { char s_inp[6] = {'a','b','c','d','e','\0'}; // or char s_inp[6] = "abcde"; char s_out[6], c; strcpy(s_out, s_inp); //copying of string, not possible s_out = s_inp; printf("%s\n", s_out); scanf("%c", &c); return 0; } Copying of strings (using strcpy form string.h ) Example: Ex29.c

Strings terminated by null (8/8) char S[7], T[7], c; char R[7] = "ABCDEF"; int n; for( n=0; n first S[5-n] = R[n]; //or S[5-n] = *(R+n); S[6] = '\0' ; printf("%s\n", S); for( n=0; n small T[n] = R[n]+32; //or *(T+n) = *(R+n)+32; T[n] = '\0' ; printf("%s\n", T); scanf("%c", &c); Changeover of text, small letter => capital letter Example: Ex30.c

Funct. stdio.h for chars and strings (1/3) int main(void) { char c,d; int n; puts("Is C/C++ …"); // unformated printing of string to stdout followed by new line c=getchar(); // get char.from the standard input if (c=='y') puts("Right,..."); if (c=='n') puts("I disagree,..."); do c=getchar(); while(c!=' ');// wait for space return 0; } Functions puts() and getchar() Example: Ex31.c

Funct. stdio.h for chars and strings (2/3) int main(void) { char c; for(c='A';c<='Z';c++)// loop for all alphabet { putchar(c);// printing character to the stdout - capital putchar(c+32);// small putchar('\n');// new line } doc=getchar(); while(c!=' ');// wait for space return 0; } Function putchar() Example: Ex32.c

Funct. stdio.h for chars and strings (3/3) int main(void) { char fname[20], sname[20], c; printf("Insert your first name: "); gets(fname); // reading characters from stdin and stores them as a string - first name printf("\nInsert your surname: "); gets(sname); // reading surname printf("\nYour whole name is: %s %s", fname, sname); scanf("%c", &c); return 0; } Function gets() Example: Ex33.c

Library string.h for strings (1/4) char S[20], T[20], c; char R[20] = "ABCDEF"; int n; for(n=0; n first S[strlen(R)-n-1] = R[n]; S[n] = '\0'; printf("%s\n", S); for( n=0; n small T[n] = R[n]+32; T[n] = '\0'; printf("%s\n", T); scanf("%c", &c); Length of string – strlen() Example: Ex34.c

Library string.h for strings (2/4) char final[30], c; char first[] = "wine"; char second[] = "women"; char third[] = "songs"; strcpy(final, first); strcat(final, second); // appends a copy of the source string to the destination string strcat(final, third); printf("%s\n", final); scanf("%c", &c); return 0; Concatenation of strings – strcat() Example: Ex35.c

Library string.h for strings (3/4) char number[11], c; char *ptr, digit = '8'; strcpy(number, " "); printf("Original string: %s\n", number); ptr = strchr(number, digit); while( ptr) { *ptr = 'x' ; ptr = strchr(number, digit); } printf("Modified string: %s", number); scanf("%c", &c); Character searching in string – strchr() Example: Ex36.c

Library string.h for strings (4/4) char txt1[51], txt2[5], txt3[5]="----",*ptr, c; int n; printf("Insert text1: "); gets(txt1); printf("Insert text2 (searched): "); gets(txt2); n=strlen(txt2); ptr = strstr(txt1, txt2); while( ptr) { strncpy(ptr,txt3,n); ptr = strstr(txt1, txt2); } printf("Modified string: %s", txt1); String searching in string– strstr()+strncpy() Example: Ex37.c

Strings – examples (1/2) In the variable num, unknown number of integer and rational numbers is stored. Each number is ended by semicolon. Compute the total number of numbers and the number of integer numbers in the string. char num[30] = "12;3.8;100;94.5;33;44;", c; int n, total=0, rat=0; printf("Numbers: %s\n\n", num); for (n=0; n<strlen(num); n++) { if(num[n] == ';') // total number of num. total++; if(num[n] == '.') // number of rational n. rat++; } printf("Total number of num.: %4d\n", total); printf("Integer numbers: %4d\n", total-rat); scanf("%c", &c); Example: Ex38.c

Strings – examples (2/2) In the string num, unknown number of integer numbers is stored. Each number is ended by semicolon. Print the numbers to the column and justify them properly. char num[30] ="12;4;100;95;33;44;"; char part[10], c; int m=0, n; printf("Numbers: %s\n\n", num); for(n=0; n<strlen(num); n++) if(num[n] != ';') part[m++] = num[n]; else { part[m] = '\0'; printf("%6s\n", part); m = 0; } Example: Ex39.c

TOPIC OF THE NEXT LECTURE Pointers and arrays THANK YOU FOR YOUR ATTENTION