IPC144 - Introduction to Strings. array of characters is an array, each element of which has a character data type.

Similar documents
Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

Unit 7. Functions. Need of User Defined Functions

Functions. Arash Rafiey. September 26, 2017

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

BSM540 Basics of C Language

BSM540 Basics of C Language

ARRAYS(II Unit Part II)

CSE101-Lec#17. Arrays. (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Tutorial 7. Number Colour Black Brown Red Orange Yellow Green Blue Violet Gray White

AMCAT Automata Coding Sample Questions And Answers

Arrays. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

COMP1917: 09 Arrays and Strings

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

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

ONE DIMENSIONAL ARRAYS

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

Basic and Practice in Programming Lab7

Solutions to Assessment

Language comparison. C has pointers. Java has references. C++ has pointers and references

Fundamentals of Programming

Fundamental of Programming (C)

Lesson 7. Reading and Writing a.k.a. Input and Output

(9-2) Strings I H&K Chapter 8. Instructor - Andrew S. O Fallon CptS 121 (March 7, 2018) Washington State University

Advanced C Programming Topics

Decision Making -Branching. Class Incharge: S. Sasirekha

Outline Arrays Examples of array usage Passing arrays to functions 2D arrays Strings Searching arrays Next Time. C Arrays.

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

Programming Language B

Functions BCA-105. Few Facts About Functions:

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

More Pointers Week 11

printf("this program adds the value 10 to a given integer number.\n\n");

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

MTH 307/417/515 Final Exam Solutions

Slides adopted from T. Ferguson Spring 2016

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

LSN 3 C Concepts for OS Programming

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

C Programming Language

Lab 5 Pointers and Arrays

Programming Studio #9 ECE 190

Write a C program using arrays and structure

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

MODULE 3: Arrays, Functions and Strings

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Pointers and scanf() Steven R. Bagley

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

Worksheet 4 Basic Input functions and Mathematical Operators

C Structures Self-referencing Card Shuffling Unions. C Structures CS Prof. Jonathan Ventura. Prof. Jonathan Ventura C Structures

A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables.

Arrays and Strings. Arash Rafiey. September 12, 2017

today cs3157-fall2002-sklar-lect05 1

Technical Questions. Q 1) What are the key features in C programming language?

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1 Lecture 7. COMP s1. Before we begin. Strings. Programming Fundamentals. Overview. Andrew Bennett

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

return return else return

7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA

ALGORITHM 2-1 Solution for Exercise 4

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

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.

Structures in C. C allows you to declare a struct. Once you do so, you can create variables of this type. Here is the general syntax:

1. The keyword main in C language is used for

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

Programming in C. Session 2. Seema Sirpal Delhi University Computer Centre

File I/O. Last updated 10/30/18

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

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

IV Unit Second Part STRUCTURES

Two Dimensional Array - An array with a multiple indexs.

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

Arrays and Pointers (part 1)

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C Program Development and Debugging under Unix SEEM 3460

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

Lecture 6. Statements

H192 Midterm 1 Review. Tom Zajdel

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

Week 8: Arrays and File I/O. BJ Furman 21OCT2009

Basics of Programming

BİL200 TUTORIAL-EXERCISES Objective:

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

Computers in Engineering. Moving From Fortran to C Part 2 Michael A. Hawker

CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso

Programming for Engineers Arrays

Computers in Engineering COMP 208. Roots of a Quadratic in C 10/25/2007. Moving From Fortran to C Part 2 Michael A. Hawker

Computer Programming Lecture 12 Pointers

More Arrays. Last updated 2/6/19

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Arrays, Strings, & Pointers

Content. In this chapter, you will learn:

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Array Initialization

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

Transcription:

IPC144 - Introduction to Strings Agenda: 1 Review/Questions 2 Array of characters 3 Character Strings 4 Try it! 5 Homework Array of Characters array of characters is an array, each element of which has a character data type. Example 1: #declare SIZE 4 /* declaring an array of characters */ char name[size]; /* we can assign a value to each element */ name[0] = F ; name[1] = r ; name[2] = e ; name[3] = d ; Example 2: Array of characters can be initialized at the same time when it is declared: #declare SIZE 4 /* Reminder: braces can be used only when initializing an array at the declaration time. */ char name[size] = { F, r, e, d ; /* then we can display every element using for loop: */ for( i = 0; i < SIZE; i ++) printf( %c, name[i]);

Example 3: /* fun27.c - using character arrays */ #include <stdio.h> #define SIZE 10 main(){ int i, flag = 1, length; char name[size]; printf("please enter your name (maximum %d characters): ", SIZE); for(i = 0; i < SIZE && flag; i++){ scanf("%c", &name[i]); if(name[i] == '\n') flag = 0; length = i < SIZE? i - 1:i; printf("your name is: "); for( i = 0; i < length ; i++ ) printf("%c", name[i]); printf("\n"); Strings String is an array of characters terminated by null ( \0 ) character. char name[5] = { F, r, e, d, \0 ; /* can t use this method after declaration */ /* NOTE: null character stored as a character, although it is not printable character */ Another way to declare a string is: char name[5] = Fred ; /* can t use this method after declaration */ /* the null character will be placed at the end of the string by default */ Now when we know that there is a null character at the end of the string, we can read a string up to \0 ( null character ).

Example 1: char product_name[31]= ifeel Mouse ; char product_desc[41]= Optical Mouse ; int i; for(i = 0; product_name[i]!= \0 ; i ++) printf( %c, product_name[i]); for(i = 0; product_desc[i]!= \0 ;i++) printf( %c, product_desc[i]); Input and output of strings %s is a format specifier used to output the string using printf or read the string of characters and store it into a string using scanf. char product_desc[41]; printf( Enter product description: ); /* scanf() will read input from the keyboard up to a white space character! That means that only one word will be stored into array, and it is up to a programmer to make sure that the array size is large enough to hold it. */ scanf( %s, product_desc); /* NOTE: name of the array is a pointer to the first element in the array, so it s value is an address of the first element in the array. When using scanf(), the name of the array is supplied to scanf() function. */ printf( Product description: %s\n, product_desc); #include <stdio.h> int main(){ int i, flag = 1, length; char first_name[21]; char last_name[21]; printf("please enter your first name (maximum 20 characters): "); scanf("%20s", first_name); printf("please enter your last name (maximum 20 characters): "); scanf("%20s", last_name); printf("first Name: %s\n", first_name); printf("last Name: %s\n", last_name); return 0;

Passing arrays to functions Recount that the name of the array is a pointer to the first element in the array, so it s value is an address of the first element in the array. When passing an array to a function the name of the array is passed (an address of the first element of the array!). In other words, the array is passed to a function using call by reference. void get_name( char arr[ ] ); int main(){ char name[5]; get_name(name ); /* passing array to the function */ printf( %s\n, name); return 0; void get_name(char arr[ ]){ arr[0] = J ; arr[1] = o ; arr[2] = h ; arr[3] = n ; arr[4] = \0 ; When an array is processed by a function, we need to know where the array ends. So, if it is an array of integers or doubles we must supply the length of the array to the function. If it is a string ( recount: a character array terminated by \0 character), the null character serves to mark the end of the string. /* displays a string character by character */ void display_name( char arr[ ] ){ int i; for( i = 0; arr[ i ]!= \0 ; i ++) printf( %c, arr[ i ]);

More scanf() (see example: fun36.c ) %[] - an additional format specifier available for scanf is %[] where the square brackets contain a list of valid characters. "-" character inside the square [] brackets indicates a range. "^" character inside the square [] brackets indicates that all characters should be accepted except those inside the square brackets. %30s number specifies maximum number of characters to be read. Examples: The following statement will accept any number of y's or n's scanf("%[ a-za-z0-9]", name); The following statement will accept only spaces, alphabetic characters, and digits. scanf("%[ a-za-z0-9]", name); The following statement will accept everything except a newline character. It will accept a line of input (including whitespace other than the newline) into a string. scanf("%[^\n]", line); up to 4 characters will be stored in the "word", any character will be accepted excluding whitespace characters scanf("%4s", word);

Try it! Write a function that will get a string and an integer as parameters. This function should truncate string for the number received as the second parameter. If the integer is less than 0 or greater than the size of the sting, the function should return 1. Otherwise the function returns 0. Here is the function prototype: int truncate(char s[], int num); Homework #8 (email your solution: arta.kogan@senecac.on.ca) Write a function called check_passwd() that will compare two strings. It takes two strings as parameters, and returns a character. If the two strings are equal then A returned, otherwise the first character that is different from the first string will be returned. For example: char ch; char passwd[15] = abcdefg ; char temp[15] = abcdefg ; ch = check_passwd(passwd, temp); /* ch will get a value of A */ ch = check_passwd(passwd, abcefgh ); /* ch will get a value of d */