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.

Slides:



Advertisements
Similar presentations
Problem Solving & Program Design in C
Advertisements

Introduction to C Programming
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
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.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Chapter 9 Character Strings
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Chapter 10.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Topic 10 - Strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
To remind us We finished the last day by introducing If statements Their structure was:::::::::
Strings String - a string is a series of characters treated as a unit. A C string is a variable-length array of characters that is delimited by the null.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
1. An array is a collection of a fixed number of components wherein all of the components are of the same type Example: Suppose that there is a list of.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
Introduction to C programming
CHAPTER 8 CHARACTER AND STRINGS
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
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.
STARTING OUT WITH STARTING OUT WITH Class 9 Honors.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
Chapter 8: Character and String CMPD144: Programming 1.
1 This chapter covers both string constants (or literals, as they're called in the C standard) and string variables, which can change during the execution.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
String Array (Multidimensional Arrays) 1. A string array is a multidimensional array of strings. It is declared in the following syntax: char variable_name[No_of_strings][size_of_each_string];
UniMAP SEM I - 09/10EKT 120 Computer Programming1 Lecture 8 – Arrays (2) & Strings.
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).
UniMAP SEM I - 10/11EKT 120 Computer Programming1 Lecture 8 – Arrays (2) and Strings.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Strings, Pointers and Tools
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
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.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions.
Chapter 8 “Character Arrays and Strings” Prepared by: Prof. Ajay M. Patel CE, IDS, NU.
INC 161 , CPE 100 Computer Programming
C Characters and Strings
Fundamentals of Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
A First Book of ANSI C Fourth Edition
Arrays in C.
CS111 Computer Programming
Chapter 2 Array and String Visit to more Learning Resources.
C Characters and Strings
Presentation transcript:

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 be changed. Since the array name is a pointer constant to the first element, the address of the first element and the name of the array both represent the same location in memory.

2 Pointers to Arrays

3 same a &a[0] a is a pointer only to the first element—not the whole array. To access an array, any pointer to the first element can be used instead of the name of the array.

4 Dereference of Array Name

5 Array Names as Pointers

6 Pointer Arithmetic and Arrays Besides indexing, programmers use another powerful method of moving through an array: pointer arithmetic. Pointer arithmetic offers a restricted set of arithmetic operators for manipulating the addresses in pointers.  Pointers and One-Dimensional Arrays  Arithmetic Operations on Pointers  Using Pointer Arithmetic

7 Pointer Arithmetic Given pointer, p, p ± n is a pointer to the value n elements away.

8 Pointer Arithmetic and Different Types

9 Dereferencing Array Pointers The following expressions are identical. *(a + n) and a[n]

10 Some valid arithmetic operations on pointers: p p p -5 p ++ p --

11 C Strings A C string is a variable-length array of characters that is delimited by the null character. A string is a sequence of characters. A string literal is enclosed in double quotes.

12 DECLARING AND INITIALIZING STRING VARIABLES Declaring a String: A string variable is a valid C variable name and always declared as an array. The general form of declaration of a string variable is, char string_name [size]; The size determines the number of characters in the string_name. When the compiler assigns a character string to a character array, it automatically supplies a null character(‘\0’) at the end of the string. The size should be equal to the maximum number of characters in the string plus one.

13 DECLARING AND INITIALIZING STRING VARIABLES Initializing a String: This can be done in two ways. 1.char str1[7]=“Welcome”; 2.char str2[8]={‘W’,’e’,’l’,’c’,’o’,’m’,’e’,’\0’};

14 Storing Strings and Characters

15 Differences Between Strings and Character Arrays

16 Strings in Arrays

17 Character Literals and String Literals

18 String Input/Output Functions C provides two basic ways to read and write strings. First, we can read and write strings with the formatted input/output functions, scanf/fscanf and printf/fprintf. Second, we can use a special set of string-only functions, get string (gets/fgets) and put string ( puts/fputs ).

19 STRING INPUT/OUTPUT FUNCTIONS(Contd…) Formatted input and output functions:scanf ()and printf() The string can be read using the scanf function with the format specifier %s. Syntax for reading string using scanf function is scanf(“%s”, string_name); Disadvantages: The termination of reading of data through scanf function occurs, after finding first white space through keyboard. White space may be new line (\n), blank character, tab(\t). For example if the input string through keyword is “hello world” then only “hello” is stored in the specified string.

20 Character I/O from keyboard: To read characters from the keyboard and write to the screen, it takes the following form: char c = getchar( ); //reads one character from the keyboard putchar(c); // display the character on the monitor Un-formatted input functions:gets ()and puts() C provides easy approach to read a string of characters using gets() function. Syntax: gets (string_name); The function accepts string from the keyboard. The string entered includes the white spaces. The input will terminate only after pressing. Once the is pressed, a null character(\0) appended at the end of the string. Advantage: It is capable of reading multiple words from the keyword. To display the string on the screen we use a function puts(). Syntax:puts(str); Where str is a string variable containing a string value.

21 String Manipulation Functions The C Library provides a rich set of string handling functions that are placed under the header file and. Some of the string handling functions are (string.h): strlen()strcat()strcpy()strrchr() strcmp() strstr()strchr()strrev() Some of the string conversion functions are (ctype.h): toupper()tolower()toascii() All I/O functions are available in stdio.h scanf()printf()gets()puts() getchar()putchar()

22 STRING HANDLING FUNCTIONS(Contd…) strlen () function: This function counts and returns the number of characters in a string. It takes the form Syantax:int n=strlen(string); Where n is an integer variable, which receives the value of the length of the string. The counting ends at the first null character.

23 STRING HANDLING FUNCTIONS(Contd…) strcat () function: The strcat function joins two strings together. It takes of the following form: strcat(string1,string2); string1 and string2 are character arrays. When the function strcat is executed, string2 is appended to string1. It does so by removing the null character at the end of string1 and placing string2 from there. strcat function may also append a string constant to a string variable. The following is valid. strcat(part1,”Good”); C permits nesting of strcat functions. Example: strcat(strcat(string1,string2),string3);

24 String Concatenation

25 STRING HANDLING FUNCTIONS(Contd…) strcmp () function: The strcmp function compares two strings, it returns the value 0 if they are equal. If they are not equal, it returns the numeric difference between the first non matching characters in the strings. It takes the following form: strcmp(str1,str2); returning value less than 0 means ''str1'' is less than ''str2'‘ returning value 0 means ''str1'' is equal to ''str2'‘ returning value greater than 0 means ''str1'' is greater than ''str2'' string1 and string2 may be string variables or string constants. Example: strcmp(name1,name2); strcmp(name1,”John”); strcmp(“their”,”there”);

26 String Compares

27 STRING HANDLING FUNCTIONS(Contd…) strcpy () function: It copies the contents of one string to another string. It takes the following form: strcpy(string1,string2); The above function assign the contents of string2 to string1. string2 may be a character array variable or a string constant. Example: strcpy(city,”Delhi”); strcpy(city1,city2); strrev() function: Reverses the contents of the string. It takes of the form strrev(string); Example: #include void main(){ char s[]=”hello”; strrev(s); puts(s); }

28 String Copy

29 String-number Copy Always use strncpy to copy one string to another.

30 STRING HANDLING FUNCTIONS(Contd…) strstr () function: It is a two-parameter function that can be used to locate a sub-string in a string. It takes the form: strstr (s1, s2); Example:strstr (s1,”ABC”); The function strstr searches the string s1 to see whether the string s2 is contained in s1.If yes, the function returns the position of the first occurrence of the sub- string. Otherwise, it returns a NULL pointer. strchr() function: It is used to determine the existence of a character in a string. Example: strchr (s1,’m’); //It locates the first occurrence of the character ‘m’. Example: strrchr(s2,’m’); //It locates the last occurrence of the character ‘m’.

31 String in String

32 Character in String (strchr)

/*Define functions- length of a string, copy, concatenate, convert into uppercase letters, compare two strings for alphabetical order- over strings and implement in a program*/ #include main() { char str1[15],str2[15],str3[10]; int n,c,len,i; printf("\n Enter the string1 "); gets(str1); puts(str1); printf("\n Enter the string2 "); gets(str2); puts(str2); printf("Enter the string 3 "); scanf("%s",str3); printf("%s",str3);

printf("\n***************************"); printf("\n 1. String Length "); printf("\n 2. String Copy "); printf("\n 3. String Comparison "); printf("\n 4. String Concat "); printf("\n 5. UpperCase "); printf("\n***************************"); printf("\n Enter the choice u want to perform"); scanf("%d",&n);

switch(n) { case 1:len=strlen(str1); printf("\n The length of the string entered is %d",len); break; case 2:strcpy(str1,str2); printf("\n 1st string =%s,2nd string=%s",str1,str2); break; case 3:c=strcmp(str1,str2); if(c==0) printf("\n Both are equal"); else printf("\n Both are different"); break; case 4:printf("\n The resultant string is: %s",strcat(str1,str2)); break; case 5:for(i=0;i<strlen(str1);i++) str1[i]=toupper(str1[i]); printf("%s",str1); break; default: printf("\n Enter correct choice"); }

OUTPUT: Enter the string1: abcd abcd Enter the string2: efgh efgh Enter the string3: pqr pqr *************************** 1. String Length 2. String Copy 3. String Comparison 4. String Concat 5. UpperCase *************************** Enter ur choice 4 The resultant string is: abcdefgh