Strings C supports strings using one-dimensional character arrays. A string is defined as a null-terminated character array. In C, a null is 0. You must.

Slides:



Advertisements
Similar presentations
Lecture 20 Arrays and Strings
Advertisements

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.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
Lecture 08 METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 24, 2002.
Chapter 9: Arrays and Strings
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Chapter 8 Arrays and Strings
Strings. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 6 Array and String.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
WEEK 6 Class Activities Lecturer’s slides.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
Computer Programming for Engineers
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Arrays. Arrays are objects that help us organize large amounts of information.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
26/06/ :14:35 CSC Alliance — 1 Kimera Richard Phone: INSTITUTE OF COMPUTER SCIENCE DEPARTMENT.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
1-d Arrays.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Computer Science 210 Computer Organization
Strings (Continued) Chapter 13
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Lecture 8 String 1. Concept of strings String and pointers
C Programming Tutorial – Part I
EKT120 : Computer Programming
Quiz 11/15/16 – C functions, arrays and strings
Array 9/8/2018.
Module 2 Arrays and strings – example programs.
Arrays in C.
Computer Science 210 Computer Organization
Arrays An Array is an ordered collection of variables
Engineering Problem Solving with C++, Etter/Ingber
Lecture 8b: Strings BJ Furman 15OCT2012.
CNG 140 C Programming (Lecture set 8)
EKT150 : Computer Programming
Declaration, assignment & accessing
Arrays Outline Introduction Arrays Declaring Arrays
Introduction To Programming Information Technology , 1’st Semester
EKT120: Computer Programming
Chapter 7 Arrays PROGRAMMING IN ANSI C.
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
Chapter 2 Array and String Visit to more Learning Resources.
Multidimensional array
Exercise Arrays.
Arrays.
Character Arrays char string1[] = “first”;
CS31 Discussion 1H Fall18: week 6
ICS103 Programming in C Lecture 12: Arrays I
Visit for more Learning Resources
Presentation transcript:

Strings C supports strings using one-dimensional character arrays. A string is defined as a null-terminated character array. In C, a null is 0. You must define the array that is going to hold a string to be one byte larger than the largest string it will be required to hold, to make room for the null. A string constant is also null-terminated by the compiler automatically.

Strings in Memory char str[20] = “Rahul arora”; Rahularora\0???????? The empty string "" occupies 1 char

String Initializers char pet[5] = { ‘l’, ‘a’, ‘m’, ‘b’, ‘\0’ } ; char pet[5] ; pet[0] = ‘l’ ; pet[1] = ‘a’ ; pet[2] = ‘m’ ; pet[3] = ‘b’ ; pet[4] = ‘\0’ ; char pet[5] = "lamb" ; But not: char pet[5]; pet = “lamb” ; /* No array assignment in C */ Remember that initializers are not assignment statements! all equivalent

Gets() To read a string from the keyboard we must use C’s standard library functions, gets(). To use gets(), call it using the name of a character array without any index. The gets() function reads characters until you press “Enter” The carriage return is not stored, but is replaced by a null, which terminates the string. The gets() function performs no bounds checking Be sure to call it with an array large enough to hold the expected input.

A string entered at the keyboard. #include “stdio.h” main() { char str[80]; int i; printf(“Enter a string (less than 80 chars) :\n”); gets(str); for(i=0; str[i]; i++) printf(“%c”, str[i]); } Notice how the program uses the fact that a null is false to control the loop that outputs the string.

Display a string, using printf(). #include “stdio.h” main(){ char str[80]; printf(“Enter a string (less than 80 chars) : \n”); gets(str); printf(str); /* output the string */ } Since the first argument to printf() is a string, you simply use str without any index as the first argument to printf(). If you wanted to output a newline, you could output str like this: printf(“%s\n”, str);

Puts() To output a string we can use C’s standard library functions, puts(). Note:the scanf will take the space as the termination of string so we cannot enter a string “rahul arora” using the scanf statement but we can get the same string using gets( ).

Two dimensional Arrays  Collection of rows & columns.  A two-dimensional array is essentially an array of one-dimensional arrays and is most easily thought of in a row, column format.  Eg. Checkerboard Matrices

Declaration & initialization type array-name[nrow][ncol]; char name[3][10]; Initialization: type array-name[nrow][ncol]= {value-list}; int sqr [3] [3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 } ; Char name[3][10]={“utkarsh”,”anurag”,”ankita”};

Initialization of arrays within arrays Eg. int list[3][4]; int daymon[2][12] = { {31,28,31,30,31,30,31,31,30,31,30,31}, {31,29,31,30,31,30,31,31,30,31,30,31} };

Two-Dimensional Arrays # define MAXROW 3 # define MAXCOL 4 int list[MAXROW][MAXCOL] for (int row = 0; row < MAXROW; row++) for (int col = 0; col < MAXCOL; col++) scanf (“%d”,&list[row][col]) list list[0] list[1] list[2]

EG.Listing the marks of students in different subjects int marks[3][4]; [0] [1] [2] [3] marks[0] marks[1] marks[2] ( subject no.) ( student no.)

Memory size Bytes = size of 1 st index *size of 2 nd index *sizeof(base type) Representation in Memory: Row-Major order Column-Major order

Storing the marks of all students in all subjects for (students = 0; students < MAXSTU; students++) for (subjects = 0; subjects < MAXSUB; subjects++) scanf (“%d”, &marks[students][subjects]); highest = 0; for (students = 0; students < MAXSTU; students++) { for (subjects = 0; subjects < MAXSUB; subjects++ ) { if (marks[students][subjects]> highest) highest = marks[students][subjects]; } printf (“the highest of all the subject is %d”,highest); /* to find the maximum of all the marks scored */

/* to find the sum of all the marks scored by each individual in all the subjects */ for (students = 0; students < MAXSTU; students++) { total[students] = 0; for (subjects = 0; subjects < MAXSUB; subjects++) total[students]=total[students]+marks[students][subjects]; }

for (int subjects = 0; subjects < MAXSUB; subjects++ ) { highest[subjects] = 0; for (int students = 0; students < MAXSTU; students++) { if (marks[students][subjects]> highest[subjects]) highest[subjects] = marks[students][subjects]; } printf (“the highest of the subject number %d is %d”, (subjects+1),highest[subjects]); } /* to find the highest marks scored in each subject*/

Exercise 1.Program to obtain transpose of of 6*6 Matrix 2.Program to sort the elements of 5*5 Matrix 3.Program to multiply matrices where the size can be upto 50 rows & 50 columns. 4.Program to generate 100 random numbers between 1 and 100.Count the number of occurrences of each number and print them out in order of frequency of occurrence.(Hint: use rand( )%100 to generate random numbers.)

Three dimensional arrays [0] [1] [2] [3] marks[0] marks[1] marks[2] ( subject no.) ( student no.) Section 1 [0] [1] [2] [3] marks[0] marks[1] marks[2] ( subject no.) ( student no.) 60 Section 2 In the previous examples we had stored marks of all the students in all their subjects. This is only for one section. If you want to store marks for another section, one more table has to be created. Similarly for a third section, another table has to be created.

Three - Dimensional array ( Subject no.) Section No. ( student no.) Table for Section 1 Table for Section 2 Table for Section 3 Three dimensional arrays can be visualized as a group of tables. Here it can be noticed that the third component is the section no. Therefore the dimensions would be Section, Student, Subject

The three dimensional array would be declared as follows marks[MAXSEC][MAXSTU][MAXSUB] or marks[3][120][6], for a maximum of 3 sections. 120 students. 6 subjects. Three - Dimensional array

Storing the marks of all students in all the sections in all subjects in a three- dimensional array /* Program to get the inputs of marks of all the students in all their subjects for all sections*/ # define MAXSEC 3 # define MAXSTU 120 # define MAXSUB 6 void main() { int marks[MAXSEC][MAXSTU][MAXSUB]; int sections,students,subjects; /* taking the input of all the marks of all students */ for (sections=0; sections < MAXSEC; sections++) for (students = 0; students < MAXSTU; students++) for (subjects = 0; subjects < MAXSUB; subjects++) scanf (“%d”, &marks[sections][students][subjects]); }

Highest marks in any subject in any section [0] [1] [2] [3] marks[0] marks[1] marks[2] ( subject no.) ( student no.) Section 1 [0] [1] [2] [3] marks[0] marks[1] marks[2] ( subject no.) ( student no.) 60 Section 2 Highest 86

Highest marks in any subject in any section /* Program to get the inputs of marks of all the students in all their subjects for all sections*/ int highest=0; for (sections=0; sections < MAXSEC; sections++) for (students = 0; students < MAXSTU; students++) for (subjects = 0; subjects < MAXSUB; subjects++) { if (highest < marks[sections][students][subjects]) highest = marks[sections][students][subjects]; }

Highest marks in each subject in any section [0] [1] [2] [3] marks[0] marks[1] marks[2] ( subject no.) ( student no.) Section 1 [0] [1] [2] [3] marks[0] marks[1] marks[2] ( subject no.) ( student no.) 60 Section 2 Highest[subjects]

Highest marks in each subject in any section /* Program to get the inputs of marks of all the students in all their subjects for all sections*/ int highest[MAXSUB]; for (subjects = 0; subjects < MAXSUB; subjects++) { highest[subjects]=0; for (sections=0; sections < MAXSEC; sections++) for (students = 0; students < MAXSTU; students++) { if (highest[subjects] < marks[sections][students][subjects]) highest[subjects] = marks[sections][students][subjects]; }

Highest marks in each subject in each section [0] [1] [2] [3] marks[0] marks[1] marks[2] ( subject no.) ( student no.) Section 1 [0] [1] [2] [3] marks[0] marks[1] marks[2] ( subject no.) ( student no.) 60 Section 2 Highest[sections][subjects]

Highest marks in each subject for each section /* Program to get the inputs of marks of all the students in all their subjects for all sections*/ int highest[MAXSEC][MAXSUB]; for (subjects = 0; subjects < MAXSUB; subjects++) for (sections=0; sections < MAXSEC; sections++) { highest[sections][subjects]=0; for (students = 0; students < MAXSTU; students++) { if (highest[sections][subjects] < marks[sections][students][subjects]) highest[sections][subjects] = marks[sections][students][subjects]; }

Highest marks of each student in each section [0] [1] [2] [3] marks[0] marks[1] marks[2] ( subject no.) ( student no.) Section 1 [0] [1] [2] [3] marks[0] marks[1] marks[2] ( subject no.) ( student no.) 60 Section 2 Highest[students][sections]

Highest marks of each student in each section /* Program to get the inputs of marks of all the students in all their subjects for all sections*/ int highest[MAXSEC][MAXSTU]; for (sections=0; sections < MAXSEC; sections++) for (students = 0; students < MAXSTU; students++) highest[sections][students] = 0; for (subjects = 0; subjects < MAXSUB; subjects++) { if (highest[sections][students] < marks[sections][students][subjects]) highest[sections][students] = marks[sections][students][subjects]; }

DISADVANTAGE OF ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC Maximum size (maximum number of elements) requested by the programmer would be reserved in the memory irrespective of the usage of the number of elements by the user.The memory space that is unused is wasted. LESS RESOURCE UTILIZATION. DIFFERENT DATA TYPES COULD NOT BE STORED IN AN ARRAY If an array is declared int test[30], only integer values should be entered and not float or any other data type.

PARALLEL ARRAYS IF YOU WANT TO STORE DIFFERENT DATA TYPES For example employee id Gross salary of the employee. Then you require two arrays 1. For storing the integer values of the employee id. 2. For storing the float values of the salary. Therefore int empid [50] flaot salary[50]