C workshop #3 flow control / strings.

Slides:



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

1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
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.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
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.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
C workshop Yuli Kaplunovsky - Today - Introduction to C Recommended book: The C programming Language / Kernighan & Ritchie.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
C Programming Strings. Array of characters – most common type of array in C  Let’s make them easier for use Denote the end of array using a special character.
If () else statement, switch statement, while () loop, do…while() loop and for( ; ; ) loop 1.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Strlen() implementation /* strlen : return length of string s */ int strlen(char *s) { int n; for (n = 0 ; s[n] != ‘\0’ ; n++) ; return n; } /* strlen.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
C Tokens Identifiers Keywords Constants Operators Special symbols.
Real World Applications: Generating Prime Numbers  Problem Write a program that prints all positive prime integers less than or equal to n. A positive.
Tutorial ICS431 – Introduction to C.
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
Introduction to Computer Algorithmics and Programming Ceng 113 Program Control Statements.
Adv. UNIX:io/91 Advanced UNIX v Objectives of these slides: –look in some detail at standard input and output in C Special Topics in Comp.
CS 162 Introduction to Computer Science Chapter 17 C++ String Objects Herbert G. Mayer, PSU (Copied from Prof. Phillip Wong at PSU) Status 11/30/2014.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
Assignment Operators = +=-= *= /=%= Statement Equivalent Statement a = a + 2 ;a += 2 ; a = a - 3 ;a -= 3 ; a = a * 2 ;a *= 2 ; a = a / 4 ; a /= 4 ; a =
CS140: Intro to CS An Overview of Programming in C (part 3) by Erin Chambers.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
CMSC 104, Version 9/011 The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading Section 4.7,
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
Slides from Shane Griffith (TA and guest instructor in Fall 2008) CprE 185: Intro to Problem Solving.
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).
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
Computer Programming for Engineers
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
Review (before the 1 st test): while (conditions) { statements; } while loop: if/else if/else statements: if (conditions) { statements; } else if (different.
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.
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
CSE 220 – C Programming Loops.
Lecture 8 String 1. Concept of strings String and pointers
Quiz 11/15/16 – C functions, arrays and strings
CSE 303 Lecture 14 Strings in C
Module 2 Arrays and strings – example programs.
מחרוזות קרן כליף.
INC 161 , CPE 100 Computer Programming
توابع ورودي-خروجي.
INC 161 , CPE 100 Computer Programming
מ- C++ ל- C קרן כליף.
The switch Statement Topics Multiple Selection switch Statement
Week 2 Variables, flow control and the Debugger
Chapter 8 Character Arrays and Strings
ECE 103 Engineering Programming Chapter 51 Random Numbers
Character Arrays char string1[] = “first”;
Strings #include <stdio.h>
Characters and Strings Functions
C Characters and Strings
UMBC CMSC 104 – Section 01, Fall 2016
Presentation transcript:

C workshop #3 flow control / strings

do / while / for Output: * ** *** **** 0, 1, 2, 3, 1, 2, 3, 4, #include <stdio.h> void main() { int I = 1, J; do { for ( J = 0 ; J < I ; J++ ) printf("*"); printf("\n"); I++; } while ( I < 5 ); I = 0; printf("%d, ",I); } while ( I < 4 ); while ( I++ < 4 ) while ( ++I < 4 ) } Output: * ** *** **** 0, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3,

goto Continue executing at different location Output: #include <stdio.h> void main() { printf("1\n"); goto LABEL_QQ; printf("2\n"); LABEL_QQ: printf("3\n"); } The label can be anything, e.g. Cont_here, Line123, AfterTest. Cannot jump into a block, or into a different function. NOT RECOMMENDED TO USE. Used mainly with error handling. Output: 1 3

switch / case Output: **How are you???** #include <stdio.h> void main() { int I; for ( I = 0 ; I < 10 ; I++ ) { switch (I) { case 2: printf("How "); break; case 3: printf("are "); case 4: printf("you"); case 5: case 6: case 7: printf("?"); default: printf("*"); } printf("\n"); Output: **How are you???**

continue Used in ‘for / while / do’ loops Output: #include <stdio.h> void main() { int I; for ( I = 0 ; I < 10 ; I++ ) { printf(“ %d", I ); if ( I == 9 || I==3 ) continue; printf(“*"); } printf("\n"); Output: 0* 1* 2* 3* 4 5* 6* 7 8* 9*

break Used in ‘for / while / do’ loops Output: 0* 1* 2* 3* 4 #include <stdio.h> void main() { int I; for ( I = 0 ; I < 10 ; I++ ) { printf(“ %d", I ); if ( I == 4 ) break; printf(“*"); } printf("\n"); Output: 0* 1* 2* 3* 4

characters A single byte (a bytes defined as a number between 0 to 255), represents a single character Sample of character representations: 32= 33=! 34=" 35=# 36=$ 37=% 38=& 39=' 40=( 41=) 42=* 43=+ 44=, 45=- 46=. 47=/ 48=0 49=1 50=2 51=3 52=4 53=5 54=6 55=7 56=8 57=9 58=: 59=; 60=< 61== 62=> 63=? 64=@ 65=A 66=B 67=C 68=D 69=E 70=F 71=G 72=H 73=I 74=J 75=K 76=L 77=M 78=N 79=O 80=P 81=Q 82=R 83=S 84=T 85=U 86=V 87=W 88=X 89=Y 90=Z 91=[ 92=\ 93=] 94=^ 95=_ 96=` 97=a 98=b 99=c 100=d 101=e 102=f 103=g 104=h 105=i 106=j 107=k 108=l 109=m 110=n 111=o 112=p 113=q 114=r 115=s 116=t 117=u 118=v 119=w 120=x 121=y 122=z 123={ 124=| 125=} 126=~

char sample #1 Output: t u u u #include <stdio.h> void main() { char C, G, L; C = 't'; printf("%c\n", C ); C = 't' + 1; G = C; printf("%c %c\n", G, C ); printf("Now, dear user, press any key, and then press ENTER\n"); L = getchar(); printf("You entered: %c\n", L ); } Output: t u u u Now, dear user, press any key, and then press ENTER 3 You entered: 3

char sample #2 #include <stdio.h> void main() { char C; for ( C = 32 ; C < 127 ; C++ ) { printf("%3d=%c ", C, C ); } printf("\n"); Output: 32= 33=! 34=" 35=# 36=$ 37=% 38=& 39=' 40=( 41=) 42=* 43=+ 44=, 45=- 46=. 47=/ 48=0 49=1 50=2 51=3 52=4 53=5 54=6 55=7 56=8 57=9 58=: 59=; 60=< 61== 62=> 63=? 64=@ 65=A 66=B 67=C 68=D 69=E 70=F 71=G 72=H 73=I 74=J 75=K 76=L 77=M 78=N 79=O 80=P 81=Q 82=R 83=S 84=T 85=U 86=V 87=W 88=X 89=Y 90=Z 91=[ 92=\ 93=] 94=^ 95=_ 96=` 97=a 98=b 99=c 100=d 101=e 102=f 103=g 104=h 105=i 106=j 107=k 108=l 109=m 110=n 111=o 112=p 113=q 114=r 115=s 116=t 117=u 118=v 119=w 120=x 121=y 122=z 123={ 124=| 125=} 126=~

strings string is an array of characters To have meaningful data, the last character of the string has to be \0 (not to confuse with the character ‘0’). #include <stdio.h> void main() { char S[100]; S[0] = 'H'; S[1] = 'i'; S[2] = '!'; S[3] = '\0'; printf("%s\n", S ); printf("%c %c %c %ce\n", S[0], S[1], S[2], S[3] ); printf("%d %d %d %d\n", S[0], S[1], S[2], S[3] ); } Output: Hi! H i ! 72 105 33 0

strings sample #2 Output: Good Morning! Test0123 s #include <stdio.h> #include <string.h> void main() { char ST[100]; char ST2[] = "Good Morning!"; char C; strcpy( ST, ST2 ); printf("%s\n", ST ); strcpy( ST, "Test0123" ); C = ST[2]; printf("%c\n", C ); } Output: Good Morning! Test0123 s

strings sample #3 Output: 33 #include <stdio.h> #include <string.h> void main() { char ST[100]; int I = 33; strcpy( ST, "%d\n" ); printf( ST, I ); } Output: 33

strings sample #4 (strcat, strlen, strstr) #include <stdio.h> #include <string.h> void main() { char ST[100]; char *P; int L; strcpy( ST, "Beginning " ); strcat( ST, "End"); L = strlen( ST ); P = strstr( ST, "nni" ); if ( P != NULL ) *P = 'N'; printf( "%s, %d\n", ST, L ); } Output: BegiNning End, 13

strings sample #5 Output: Good Morning 123!! GOOD MORNING 123!! #include <stdio.h> #include <string.h> void ToCap( char *ST ) { while ( *ST != 0 ) { if ( *ST >= 'a' && *ST <='z' ) *ST = (*ST - 'a') + 'A'; ST++; } void main() char ST[100]; strcpy( ST, "Good Morning 123!!\n" ); printf( ST ); ToCap( ST ); Output: Good Morning 123!! GOOD MORNING 123!!

strings sample #5.5 (SAME) #include <stdio.h> #include <string.h> void ToCap( char *P ) { while ( *P != 0 ) { if ( *P >= 'a' && *P <='z' ) *P = (*P - 'a') + 'A'; P++; } void main() char ST[100]; strcpy( ST, "Good Morning 123!!\n" ); printf( ST ); ToCap( ST ); Output: Good Morning 123!! GOOD MORNING 123!!

strings sample #6 (scanf) #include <stdio.h> #include <string.h> void main() { char ST[100]; printf("Please enter a word\n"); scanf("%s", ST ); printf("Your word is: \"%s\“,\nand it has %d characters\n", ST, strlen(ST) ); } Output: Please enter a word Hello!!! Your word is: "Hello!!!", and it has 8 characters

sample #7 (float type) Output: Please enter a number 10 #include <stdio.h> void main() { int J; float F; double D; printf("Please enter a number\n"); scanf("%d", &J ); printf("Please enter a fractional number\n"); scanf("%f", &F ); D = F; printf("Your numbers are: %d %g %g\n", J, F, D ); } Output: Please enter a number 10 Please enter a fractional number 33.44 Your numbers are: 10 33.44 33.44

strings sample #8 (sprintf) #include <stdio.h> #include <string.h> void main() { char ST[250]; int I = 22; sprintf( ST, "Test123 %d", I ); printf( "%s\n", ST ); } Output: Test123 22

strings sample #9 (gets) #include <stdio.h> #include <string.h> void main() { char ST[250]; printf("Enter a word\n"); gets( ST ); printf("You entered: '%s'\n", ST ); } Output: Enter a word Too many examples!!! You entered: 'Too many examples!!!'

Matrix multiplication example #include <stdio.h> #include <stdlib.h> void MatMult( int N, double A[10][10], double B[10][10], double C[10][10] ) { int I,J,K; double D; for ( I = 0 ; I < N ; I++ ) { for ( J = 0 ; J < N ; J++ ) { D = 0.0; for ( K = 0 ; K < N ; K++ ) D = D + A[I][K] * B[K][J]; C[I][J] = D; } void PrintMat( int N, double A[10][10] ) int I,J; for ( J = 0 ; J < N ; J++ ) printf("%g, ", A[I][J] ); printf("\n"); void main() double X[10][10], Y[10][10], Z[10][10]; int N = 2, I, J; for ( I = 0 ; I < N ; I++ ) X[I][J] = rand() % 10; Y[I][J] = rand() % 10; MatMult( N, X, Y, Z ); printf("Matrix X is: \n"); PrintMat( N, X ); printf("\nMatrix Y is: \n"); PrintMat( N, Y ); printf("\nMatrix Z is: \n"); PrintMat( N, Z ); Output: Matrix X is: 1, 7, 4, 0, Matrix Y is: 9, 4, 8, 8, Matrix Z is: 65, 60, 36, 16,

Quiz (15 minutes) Write a program that prints the multiplication table Write a FUNCTION that gets as an input two strings, compares them, and returns either 0 or 1. If the strings are the same it returns 1, if not, it returns 0. Write a program that has two hard-coded (predefined) strings and uses the above function.

Next Wednesday More standard libraries (math.h, stdlib.h, etc.) How to create a library of our own functions More pointers (type casting) The “Numerical Recipes” package Financial applications Saturday C/C++ Interface to excel Question session