Introduction to C Programming Ray Lauff Temple University Computer Science v2.0

Similar documents
Intermediate Programming, Spring 2017*

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

20 Dynamic allocation of memory: malloc and calloc

Programming refresher and intro to C programming

Programming in C++: Assignment Week 1

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

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

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

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

today cs3157-fall2002-sklar-lect05 1

Chapter 8 C Characters and Strings

Computer Programming: Skills & Concepts (CP) Strings

Why arrays? To group distinct variables of the same type under a single name.

CS 61c: Great Ideas in Computer Architecture

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

CSE 220: Systems Programming

SOFTWARE Ph.D. Qualifying Exam Fall 2017

Characters, Character Strings, and string-manipulation functions in C

Library Functions. General Questions

Pointers, Arrays, and Strings. CS449 Spring 2016

BSM540 Basics of C Language

Computers Programming Course 5. Iulian Năstac

Strings. Steven R. Bagley

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

Memory, Arrays & Pointers

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

Functions. Arash Rafiey. September 26, 2017

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

ch = argv[i][++j]; /* why does ++j but j++ does not? */

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

Chapter 8 - Characters and Strings

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

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

High Performance Programming Programming in C part 1

8. Characters, Strings and Files

Characters and Strings

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

1 SAT-DANCE-HEULE INTRO 1

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

arrays and strings week 3 Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 22 imperative programming review

Approximately a Test II CPSC 206

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

C Characters and Strings

LAB 6 (2017 June 22/27) Array of pointers. Dynamic memory allocation.

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

CS16 Exam #1 7/17/ Minutes 100 Points total

Exercise 1.1 Hello world

Lecture 10 Arrays (2) and Strings. UniMAP SEM II - 11/12 DKT121 1

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Standard C Library Functions

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

Computer Science & Engineering 150A Problem Solving Using Computers

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

BSM540 Basics of C Language

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

CS240: Programming in C

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Programming Language B

Presented By : Gaurav Juneja

C: How to Program. Week /May/28

mith College Computer Science CSC231 Bash Labs Week #10, 11, 12 Spring 2017 Introduction to C Dominique Thiébaut

C: How to Program. Week /Mar/05

Basic and Practice in Programming Lab7

COMP1917: 09 Arrays and Strings

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

ECE551 Midterm Version 1

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

Fundamentals of Programming. Lecture 11: C Characters and Strings

Fundamentals of Programming Session 8

Communication. Serial port programming

The C Programming Language Part 4. (with material from Dr. Bin Ren, William & Mary Computer Science, and

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

File IO and command line input CSE 2451

A brief introduction to C programming for Java programmers

File (1A) Young Won Lim 11/25/16

COP 3223 Final Review

File Access. FILE * fopen(const char *name, const char * mode);

C Basics And Concepts Input And Output

C Programming Language Review and Dissection III

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

CS 261 Data Structures. Introduction to C Programming

CS 107 Lecture 5: Arrays. and Pointers in C. Monday, January 22, Stanford University. Computer Science Department

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

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

Chapter 2 - Introduction to C Programming

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

Student Number: Computer Science 211b Final Examination. 28 April hours

BİL200 TUTORIAL-EXERCISES Objective:

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

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

ECE551 Midterm Version 2

Introduction to string

ECE2049: Homework 1. Due: Thursday, 25 May 2017 by 7pm

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 {

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

C++ For Science and Engineering Lecture 15

C Language Part 3. Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

M.CS201 Programming language

Transcription:

Introduction to C Programming Ray Lauff Temple University Computer Science v2.0 NOTE: Not all the material covered in class is included in this slide deck. These slides are not meant to be used as a stand-alone learning tool. You are still responsible for ALL material covered. Get the notes from a classmate.

What is a string?

What is a string? A string is a collection of zero or more characters followed by the ASCII NUL character. '\0'

What is a character array?

What is a character array? A character array is a variable of type char that represents one or more characters. char my_char_array[ 400 ];

1. C will create strings for you automatically every time you use double quotes in source code. True or False?

1. C will create strings for you automatically every time you use double quotes in source code. True or False? char name[ 20 ] = "Patty Programmer";

2. Given: puts( "My brain is full!!!" ); The argument to the puts() function is a string. True or False?

3. Given: printf( "%s\n", "That does not compute." ); Both arguments to the printf() function are strings. True or False?

4. The puts( ) statement can only take one argument, and it must be a string. True or False?

puts() and printf() The puts( ) function ONLY accepts one string argument. The printf( ) function ALWAYS takes one string argument as the first parameter to the function. All the other arguments to printf( ) are dependent on this first argument. The other arguments to printf( ) to match up with the placeholders. Those other arguments can be of all different types, and are always passed by value to printf( ). Is printf( ) special or what??

5. The sprintf( ) function allows you to write strings to the screen just like the printf( ) function. True or False?

sprintf( ) Writes output to a string, not to the screen. The string is the first parameter to the function. This is the prototype for sprintf( ), which can be found in stdio.h. int sprintf(char * restrict str, const char * restrict format,...);

6. sprintf( ) should be used instead of snprintf( ) because it allows you to output the data to files as well as to strings. True or False?

snprintf( ) snprintf( ) is preferred because it limits the size of the data that goes into the string, preventing overindexing. const int bufsize = 80; char buffer[ bufsize ]; int yum = 700; char name[100] = "Big Rock Candy Mountain"; snprintf( buffer, bufsize, "%d %s", yum, name );

snprintf( ) This is the prototype for snprintf( ), which is found in stdio.h. int snprintf(char *str, size_t size, const char *format,...); Only the first three parameters are required. The placeholders in the format determine how many additional parameters are required on the snprintf( ) invocation.

Choose the BEST answer! 7. The primary difference between an array and a string is a) the length of an array is always known, but the length of a string varies. b) multiple strings can be stored in an array, but only one can be in a string. c) sizeof a will always return the number of elements in the array a. d) strings always end with a sentinel value where arrays may or may not. e) arrays can hold strings, but strings can't hold arrays. f) a string can't be passed to a function, but an array can.

Choose the BEST answer! 8. The length of a string can be determined with a) sizeof b) strlen c) strnlen d) sizeof / sizeof one of the elements

strlen and sizeof Remember that strlen returns the number of characters in a string. char name[ ] = "Tan"; strlen( name ); // will return 3

strlen and sizeof sizeof returns the space occupied by a variable. char name[ ] = "Tan"; sizeof name; // will return 4

strlen and sizeof sizeof is an operator and is "baked in" to C as part of the language. strlen is a function with a prototype provided in the string.h header. extern size_t strlen ( const char * s);

strlen and sizeof size_t strlen ( const char * s); What is the return type of this function prototype?

strlen and sizeof size_t strlen ( const char * s); size_t is a type suitable for representing the amount of space a given thing requires, expressed in characters. It is always going to be zero or a positive integer. It is the return type of both strlen and sizeof. It is a 'unsigned long integer'.

strlen and sizeof char name[ ] = "Tan"; unsigned long stringlen = strlen( name ); unsigned long sizelen = sizeof name; printf( "The length of the string is %lu.\n", stringlen ); printf( "The memory spaced used is %lu.\n", sizelen );

strlen and sizeof char name[ ] = "Tan"; unsigned long stringlen = strlen( name ); unsigned long sizelen = sizeof name; printf( "The length of the string is %lu.\n", stringlen ); printf( "The memory spaced used is %lu.\n", sizelen ); The length of the string is 3. The memory spaced used is 4.

9. char buf[ 12 ] = "Temple"; The number of elements in this array which have been initialized is a) 0 b) 6 c) 7 d) 8 e) 9 f) 11 g) 12

10. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%s\n", buf );

10. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%s\n", buf ); Temple

11. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%c\n", buf[0] );

11. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%c\n", buf[0] ); T

12. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%c\n", *buf );

12. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%c\n", *buf ); T

13. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%d\n", buf[0] );

13. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%d\n", buf[0] ); 84

14. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%s\n", buf[ 4 ] );

14. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%s\n", buf[ 4 ] ); Segmentation Fault! Core dump.

15. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%s\n", buf + 4 );

15. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%s\n", buf + 4 ); le

Note Doing the math with a pointer, as with char buf[ 12 ] = "Temple"; printf( "%s\n", buf + 4 ); can also be written as printf( "%s\n", &(buf[ 4 ])); or you can print just a single character as with printf( "%c\n", buf[ 4 ] );

16. What gets printed by the following? char buf[ 12 ] = "Temple"; int i = 3; while ( buf[i]!= '\0' ) putchar( buf[ i++ ] );

16. What gets printed by the following? char buf[ 12 ] = "Temple"; int i = 3; while ( buf[i]!= '\0' ) putchar( buf[ i++ ] ); ple

17. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%s\n", buf + 3 );

17. What gets printed by the following? char buf[ 12 ] = "Temple"; printf( "%s\n", buf + 3 ); ple

18. What gets printed by the following? char buf[ 12 ] = "Temple"; int i; for ( i = 0; buf[ i ]!= '\0'; i++ ) if ( buf[ i ] == 'e' buf[ i ] == 'l' ) putchar( buf[ i ] );

18. What gets printed by the following? char buf[ 12 ] = "Temple"; int i; for ( i = 0; buf[ i ]!= '\0'; i++ ) if ( buf[ i ] == 'e' buf[ i ] == 'l' ) putchar( buf[ i ] ); ele

19. What gets printed by the following? char buf[ 12 ] = "Temple"; int i; for ( i = 0; buf[ i ]!= '\0'; i++ ) if ( buf[ i ] == 'e' buf[ i ] == 'l' ) putchar( buf[ i - 1 ] );

19. What gets printed by the following? char buf[ 12 ] = "Temple"; int i; for ( i = 0; buf[ i ]!= '\0'; i++ ) if ( buf[ i ] == 'e' buf[ i ] == 'l' ) putchar( buf[ i - 1 ] ); Tpl

20. What is in the variable hold? char buf[ 12 ] = "Temple"; char hold[ 100 ]; int i; int j = strlen( buf ); hold[ j-- ] = '\0'; for ( i = 0; buf[ i ]!= '\0'; i++ ) hold[ j-- ] = buf[ i ];

20. What is in the variable hold? char buf[ 12 ] = "Temple"; char hold[ 100 ]; int i; int j = strlen( buf ); hold[ j-- ] = '\0'; for ( i = 0; buf[ i ]!= '\0'; i++ ) hold[ j-- ] = buf[ i ]; The variable hold now contains: e l p m e T \0 [0] [1] [2] [3] [4] [5] [6]

21. What gets printed by the following? char *nicks[ ] = { }; "Crash Override", "Acid Burn", "Zero Cool", int i; int n = sizeof nicks / sizeof nicks[0]; for ( i = 0; i < n; i++ ) printf( "%c\n", nicks[ i ][ 1 ] );

21. What gets printed by the following? char *nicks[ ] = { }; "Crash Override", "Acid Burn", "Zero Cool", int i; int n = sizeof nicks / sizeof nicks[0]; for ( i = 0; i < n; i++ ) printf( "%c\n", nicks[ i ][ 1 ] ); r c e

22. What gets printed by the following? char *nicks[ ] = { "Crash Override", "Acid Burn", "Zero Cool", }; int i, j; int n = sizeof nicks / sizeof nicks[0]; for ( i = 0; i < n; i++ ) { for ( j = 0; nicks[ i ][ j ]!= '\0'; j++ ) putchar( nicks[ i ][ j ] ); putchar( '\n' ); }

22. What gets printed by the following? char *nicks[ ] = { "Crash Override", "Acid Burn", "Zero Cool", }; int i, j; int n = sizeof nicks / sizeof nicks[0]; for ( i = 0; i < n; i++ ) { for ( j = 0; nicks[ i ][ j ]!= '\0'; j++ ) putchar( nicks[ i ][ j ] ); putchar( '\n' ); } Crash Override Acid Burn Zero Cool

23. What gets printed by the following? char *nicks[ ] = { "Crash Override", "Acid Burn", "Zero Cool", }; int i, j; int n = sizeof nicks / sizeof nicks[0]; int sums[ 10 ]; for ( i = 0; i < n; i++ ) { sums[ i ] = summit( nicks[ i ] ); printf( "Sums [ %d ] = %d.\n", i, sums[i] ); } int total = 0; for ( i = 0; i < n; i++ ) total += sums[ i ]; printf( "Total = %d\n", total );

int summit( char *s ) { int i = 0; int count = 0; while ( s[ i ]!= '\0' ) { if ( s[ i ] == 'a' s[ i ] == 'e' s[ i ] == 'i' s[ i ] == 'o' s[ i ] == 'u' s[ i ] == 'y' ) count++; i++; } } return count;

Sums [ 0 ] = 4. Sums [ 1 ] = 2. Sums [ 2 ] = 4. Total = 10

Caesar Cipher Caesar Cipher is one of the most basic forms of an encryption algorithm. In this type of encryption, each letter in the message is replaced with a letter corresponding to a certain number of letters up or down in the alphabet.

Caesar Cipher For example, we can use the algorithm to convert Temple into Ufnqmf

Caesar Cipher For example, we can use the algorithm to convert into Temple Ufnqmf (Which is each letter in sequence have increased by 1 location in the alphabet. ASCII makes it easy!)

Caesar Cipher void encrypt( char arr[] ) { int i; } for( i = 0; i < strlen( arr ); i++ ) { arr[i] += ROT; }

Caesar Cipher void decrypt( char arr[] ) { int i; } for( i = 0; i < strlen( arr ); i++ ) { arr[i] -= ROT; }

#include<stdio.h> #include<stdlib.h> #include<string.h> int main() { char msg[100]; const int rot = 1; void encrypt( char arr[] ) { int i; for( i = 0; i < strlen( arr ); i++ ) { arr[i] += rot; } } void decrypt( char arr[] ) { int i; for( i = 0; i < strlen( arr ); i++ ) { arr[i] -= rot; } } } printf( "Enter your message: " ); fgets( msg, 100, stdin ); printf("\nencrypted Version\n"); encrypt(msg); printf("%s\n",msg); printf("\ndecrypted Version\n"); decrypt(msg); printf("%s",msg); puts(""); return EXIT_SUCCESS;

%./a.out Enter your message: Temple Encrypted Version Ufnqmf Decrypted Version Temple %./a.out Enter your message: Luke, I am your father! Encrypted Version Mvlf-!J!bn!zpvs!gbuifs" Decrypted Version Luke, I am your father! %

Moving On from Strings