Computers Programming Course 11. Iulian Năstac

Similar documents
Computers Programming Course 10. Iulian Năstac

Computers Programming Course 12. Iulian Năstac

ONE DIMENSIONAL ARRAYS

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

ARRAYS(II Unit Part II)

Introduction to string

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

Chapter 8 Character Arrays and Strings

Create a Program in C (Last Class)

8. Characters, Strings and Files

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

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

by Pearson Education, Inc. All Rights Reserved.

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

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

Introduction to C/C++ Lecture 5 - String & its Manipulation

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

String can be represented as a single-dimensional character type array. Declaration of strings

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

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

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

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

Computer Programming: Skills & Concepts (CP) Strings

Chapter 8 - Characters and Strings

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

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

CS1100 Introduction to Programming

Scientific Programming in C V. Strings

Intermediate Programming, Spring 2017*

9/9/2017. Prof. Vinod Mahajan

C Characters and Strings

Characters and Strings

Chapter 21: Introduction to C Programming Language

CS107 Spring 2019, Lecture 4 C Strings

Chapter 8 C Characters and Strings

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

Chapter 9 Strings. With this array declaration: char s[10];

CS107, Lecture 4 C Strings

CSE2301. Arrays. Arrays. Arrays and Pointers

CMPSC 311- Introduction to Systems Programming Module: Strings

Structured programming

Using Character Arrays. What is a String? Using Character Arrays. Using Strings Life is simpler with strings. #include <stdio.

BITG 1113: Array (Part 2) LECTURE 9

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

Course organization. Course introduction ( Week 1)

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

C: How to Program. Week /May/28

Objectives. Chapter 8 Arrays and Strings. Objectives (cont d.) Introduction 12/14/2014. In this chapter, you will:

Arrays array array length fixed array fixed length array fixed size array Array elements and subscripting

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

1 Pointer Concepts. 1.1 Pointer Examples

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


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

Computer Science & Engineering 150A Problem Solving Using Computers

C-String Library Functions

Important Questions for Viva CPU

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

Fundamentals of Programming & Procedural Programming

Pointers, Arrays, and Strings. CS449 Spring 2016

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Strings and Library Functions

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

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

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

CSI33 Data Structures

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings

C Programming Language Review and Dissection III

Arrays and Pointers (part 1)

Programming for Engineers Arrays

LECTURE 15. String I/O and cstring library

C Libraries. Bart Childs Complementary to the text(s)

Array Initialization

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings

Procedural Programming & Fundamentals of Programming

Data Structures and Algorithms (DSA) Course 3. Iulian Năstac

Multiple Choice Questions ( 1 mark)

CS 137 Part 6. ASCII, Characters, Strings and Unicode. November 3rd, 2017

Procedural Programming & Fundamentals of Programming

Computer Science & Engineering 150A Problem Solving Using Computers

CS1100 Introduction to Programming

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

Week 5. Muhao Chen.

Functions: call by reference vs. call by value

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

C Style Strings. Lecture 11 COP 3014 Spring March 19, 2018

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Object-Oriented Programming

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

C does not support string data type. Strings in C are represented by arrays of characters.

PDS Class Test 2. Room Sections No of students

Computers Programming Course 7. Iulian Năstac

Procedural Programming

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

Transcription:

Computers Programming Course 11 Iulian Năstac

Recap from previous course Cap. Matrices (Arrays) Matrix representation is a method used by a computer language to store matrices of different dimension in memory. In C programming language, a matrix is a collection of variables (of the same type) that can be called with the same name. C programming language uses "Row Major schema, which stores all the elements for a given row contiguously in memory. 2

Recap from previous course Declaring Arrays Syntax: type arrayname [lim_1] [lim_2] [lim_n]; where lim_i is the limit of index i (on dimension i) The index i can have the following values: 0, 1, 2,, lim_i - 1 3

Recap from previous course One-dimensional matrices (vectors) The vectors are one-dimensional arrays. Syntax: type name_vect [lim]; 4

Recap from previous course The relationship between pointers and vectors The name of an array is a pointer because it holds the value the address of its first element. However there is a difference between an array name and a pointer type variable. Thus, a pointer type variables can be assigned values at runtime, while it is not possible to make the same for an array/vector (the name of a vector/array always represents the address of its first element). It is customary to say that the name of an array is a constant pointer. 5

Recap from previous course How to use a vector as a parameter to a function as a pointer as a vector with unmentioned dimension as a vector with mentioned dimension 6

Recap from previous course const modifier A variable can be transformed into a constant value (that cannot be modified). Usually a constant is defined using # define But const modifier can also transform a variable into a constant. 7

Recap from previous course Strings in C The character string is one of the most widely used applications that involves vectors. A string in C is an array of char values terminated by a special null character value '\0'. 8

Main properties of strings: 1. A string is stored in a memory organized as a vector of char type. 2. Each character is kept on different byte (through its successive code number). 3. The most commonly used code for this purpose is ASCII. 4. After the last character of the string there must be added the null character, which is '\ 0. 5. Since there is null in final position, then we have to declare the character array with one more character than the effective number of characters. 6. Although C does not have explicit data string, it allows constant string. A string constant is a list of characters enclosed in quotation marks (eg "hello"). 7. Often it is not necessary to be entered manually the null character at the end of the string (the compiler does automatically). This happens when using special functions to operate strings (like gets()). 8. To operate a string you can use: The name of the array (the constant pointer to the string respectively); A simple pointer to that string. 9

Example If we declare: char tab[ ] = This is a string ; then: tab is the address of first char `T` tab+1 is the address of second char `h` tab+2 is the address of third char `i` etc. or: tab[0] contains the ASCII of first char `T` tab[1] contains the ASCII of second char `h` A similar effect we can obtain by using: char const *p = This is a string ; where p[0] or *p - contains the ASCII of first char `T` p[1] or *(p+1) - contains the ASCII of second char `h` 10

Recap from previous course Standard functions for strings C provides a library for strings (string.h). C string library functions do not allocate space for the strings they manipulate, nor do they check that you pass in valid strings; it is up to your program to allocate space for the strings that the C string library will use. Calling string library functions with bad address values will cause a fault or "strange" memory access errors. 11

Some of the functions in the Standard C string library calculating the length of strings (eg strlen) copying the strings (eg strcpy) concatenating the strings (eg strcat) comparing the strings (eg strcmp) searching (identification) of character or substrings (eg strchr, strstr) 12

1. Computing the string length The length of a string is the number of the own characters which fall within its composition. NULL character is not considered in determining the length of a string. NULL's presence is necessary because in determining the length of a string are counted all characters until its occurrence (but without 13 NULL).

Syntax: unsigned strlen(const char * s); 14

Examples: 1) char * const p = This is a string ; unsigned n;... n=strlen (p); /* n will be assigned with the value 16 */ 2) char tab [ ] = This is a string ; int n; n = strlen(tab); /* n will be assigned with the value 16 */ 3) int n; n = strlen( This is a string ); /* n will be assigned with the value 16 */ 15

Notes: All three examples had identical results. The strlen function formal parameter is a constant pointer. Consequently, strlen function must not modify the characters string which is of a determined length. 16

2. Copying the strings Sometimes it is necessary to copy a string of character from a memory area in another part. For this we use the strcpy function, which has the syntax: char * strcpy (char * dest, const char* source); The function makes a copy of the string pointed by the source to the memory area whose address is indicated by dest. The function copies all the characters, including the null one, from the end of the string. The function returns the address of dest. 17

Examples 1) char tab[ ] = This string is copied ; char t [ (sizeof tab)+1]; strcpy (t, tab); 2) char t [100]; strcpy (t, This string is copied ); 3) char * p= This string is copied ; char t [100]; char * q; q=strcpy(t, p); 18

Variants of copy function In order to copy only first n characters of a string from one area to another zone of memory, we can use the function: char *strncpy (char *dest, const char * source, unsigned n); If n string length - then all characters of the string are transferred to the dest; If n < length of the string - then copy only the first n characters of the string. 19

Example 1 char * p = This string is partially copied ; char t[12]; strncpy (t, p, (sizeof t)-1); 20

Example 2 Write a program that reads a sequence of words, and finally displays only the longest word. 21

#include<stdio.h> #include<string.h> #define MAX 100 100 characters*/ /* it is assumed that a word has no more than int main( ) { int max=0, i; char word[max+1]; char word _max[max+1]; while (scanf( %100s, word)!=eof) if (max < (i = strlen(word))) { max= i; strcpy (word _max, word); } if (max) printf( The longest word is %s and its length is %d \n, word _max, max); getch(); 22 }

3. Concatenating the strings For concatenating strings we can use strcat function. Syntax: char *strcat(char *dest, const char *source); 23

Notes: strcat function copies the string from the source area to memory zone, immediately following the last character of the string, which was already on dest. The function returns a pointer to dest. It is assumed that the area pointed by dest is large enough to keep all characters from these two strings which are concatenated plus the NULL character. strcat function does not modify the string pointed to by the source (which is a constant pointer). 24

Example char tab1[100] = The language C++ ; char tab2[ ] = is a superset of C ; strcat(tab1, ); strcat(tab1, tab2); 25

Variant of strcat function char *strncat (char *dest, const char *source, unsigned n); If n length of source - then all characters of the source string are concatenated after dest; If n < length of source - then only the first n characters of the source string are concatenated after dest. 26

4. Comparing the strings Strings of characters can be compared using the ASCII characters in their structure. To better understand this mechanism we consider two strings: s1 and s2. We have the following cases: 27

two strings: s1 and s2 s1 = s2 if both have similar length and s1[i] = s2[i] i. s1 < s2 if i such that s1[i]<s2[i] and s1[j]=s2[j] j= 0, 1,, i-1. s1 > s2 if i such that s1[i]>s2[i] and s1[j]=s2[j] j= 0, 1,, i-1. 28

We can think of the ordering of words in a dictionary 29

strcmp function int strcmp( const char *s1, const char *s2); This function returns: a negative value - if s1 < s2 0 - if s1 = s2 a positive value - if s1 > s2 30

Example... char *p= Student ; char name[20]; printf( \n Password: ); gets(name); if (strcmp(name, p)) exit(1); 31

stricmp function int stricmp( const char *s1, const char *s2); This function is similar to strcmp. The only difference is that stricmp function does not distinguish between uppercase and lowercase letters. Note: there is strcmpi on some compilers 32

Example Considering: char *sir1 = ABC ; char *sir2 = abc ; int i; By using: i = strcmp(sir1, sir2); we obtain a negative value, since ABC < abc (A has 41h, while a has 61h) But when: we obtain zero i = stricmp(sir1, sir2); 33

strincmp function int strincmp( const char *s1, const char *s2, unsigned n); This function limits the comparison up to n characters (and also ignores the difference between uppercase and lowercase letters). Note: there is strncmpi or strncmp on some compilers 34

Example Write a program that reads a sequence of words, and finally displays only the greatest word from that sequence. 35

#include<stdio.h> #include<string.h> #define MAX 100 int main () { char word[max+1]; char word _max[max+1]; word _max [0]= \0 ; /* word _max is initialized with null */ while (scanf( %100s, word)!=eof) { if (stricmp(word, word _max)>0) strcpy (word _max, word); } printf( \n The greatest word is %s, word _max); } getch(); return 0; 36

5. Finding strings or characters Some useful functions: strchr(s1, ch); /* return a pointer at first finding of ch character in s1 */ strstr(s1, s2); /* return a pointer at first finding of s2 inside s1 */ If there is no match (finding), then these functions return zero. 37

Syntax: char *strchr( const char *s1, const char ch); char *strstr( const char *s1, const char *s2); 38

Example #include<stdio.h> #include<string.h> int main () { char s[80]; gets(s); /* write a text */... if (strchr(s, e )) printf( The character - e - was find in the string ); if (strstr(s, test )) printf( The word - test - was find in the string );... } 39

Initializing Arrays In C, an array can be initialized, either one by one element, or using a single statement as follows: double A[5] = {1000.0, 2.0, 3.4, 17.0, 50.0}; The number of values between braces { } can not be larger than the number of elements that we declare for the array between square brackets [ ]. 40

Two-Dimensional Arrays The simplest form of the multidimensional array is the two-dimensional array. A twodimensional array is, in essence, a list of one-dimensional arrays. To declare a twodimensional integer array of size lim_1, lim_2 you would write something as follows: type arrayname[lim_1][lim_2] 41