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];

Slides:



Advertisements
Similar presentations
 A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character.
Advertisements

Strings string.h library. String Library Functions Dr. Sadık EşmelioğluCENG 1142 NameDescription strlen return the length of string not counting \0 strcopy.
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.
Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() 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)
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
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.
Chapter Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Chapter 9 Strings Instructor: Alkar / Demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Chapter 9 Character Strings
Chapter 10.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
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.
CS 201 String Debzani Deb.
CS Nov 2006 C-strings.
C-Strings Joe Meehean. C-style Strings String literals (e.g., “foo”) in C++ are stored as const char[] C-style strings characters (e.g., ‘f’) are stored.
Data Structure and C Part-6. Naming a Function Any valid variable-name can be given to the user-defined function. The main program itself is considered.
PRESENTED BY: ER. SUMANPREET KAUR LECTURER IN CE DEPTT. GPCG ASR.
 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
Strlen() implementation /* strlen : return length of string s */ int strlen(char *s) { int n; for (n = 0 ; s[n] != ‘\0’ ; n++) ; return n; } /* strlen.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
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.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Chapter 8: Character and String CMPD144: Programming 1.
Review of Lectures 12, 13, 14 -Functions -Arrays -Strings.
Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.
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:
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
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.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
5.6 String Processing Part 2. Sprintf(destnvar,…..regularprintf) Write formatted data to string Same as printf except the output is put in variable. A.
UniMAP SEM I - 09/10EKT 120 Computer Programming1 Lecture 8 – Arrays (2) & Strings.
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, and the string Class. C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string.
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.
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.
UNIT - 3 ARRAYS AND STRINGS. Array An Array is a collection of similar data items, that are stored under a common name. Types –One-Dimensional array –Two-Dimensional.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
C Characters and Strings
Module 2 Arrays and strings – example programs.
Strings.
EECE.2160 ECE Application Programming
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Strings Adapted from Dr. Mary Eberlein, UT Austin.
C Characters and Strings
EECE.2160 ECE Application Programming
Presentation transcript:

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]; For ex: char s[2][40] = {“I love ice cream”,”I love India”}; (A) The number of strings is optional if the string array is initialized with the declaration India};

(B) The number of string is a must of initialization of string is done after declaration. For ex: char s[2][40]; strcpy(s[0], “I love ice cream”); strcpy(s[1], “I love India”); In this case strcpy is the only way you can initialize the individual string in the string array. 2. Inputting values in a sting Array:

You can use a scanf() or gets() to accept the value in a string array For ex: char s[2][40]; int i; for(i=0; i<2; i++) { printf(“\n\n Enter the %d string”,i); scanf(“%s”, s[i]); }

(or) char s[2][40]; int i; for(i=0; i<2; i++) { printf(“\n\n Enter the %d string”, i); gets(s[i]); }

3. Printing the string array: The printing of the string array can be accompanied by using puts() or printf(). For ex: char s[2][40] = {“I love ice cream”,”I love Jamaica”}; int i; for(i=0; i<2; i++) { printf(“%s”, s[i]); }

(or) char s[2][40] = {“I love ice cream”,”I love Jamaica”}; int i; for(i=0; i<2; i++) { puts(s[i]); }

4. String Functions: C offers a variety of functions for string manipulations like: Determine the length of string Copy and joining strings Compare strings Search strings Convert strings 4.1 Length of string using strlen:

Syntax: strlen(stringname); For ex: int l = strlen(s); 4.2 strcpy() Syntax: strcpy(Destination string, source string); This copies the contents of the string pointed by source into location pointed by destination.

For ex: strcpy(s,”Welcome to Jamaica”); strcpy(s, s1); 4.3 strncpy() This lets you specify how many characters to copy from source to destination. The syntax is: strncpy(char * destination, char * source, size); char dest[10] = “Jamaica”; char source[9] = “I love”; int n = 1; For ex: strncpy(dest, source, n);

4.4 strcat(): This function concatenates two strings. The syntax is: strcat(char * str1, char * str2); This appends a copy of str2 onto the end of str1, moving the terminating character to the end of the new string. You must allocate enough space for str1 to hold the resulting string. 4.5 strncat() This function concatenates two strings, but it lets you specify how many characters to concatenate. The syntax is: strncat(char * str1, char * str2, size);

For ex: char str[16] = “abcdefghijklmnopq”; char str1[20] “I like”; strncat(str1, str, 3); str1 => “I like abc”; 4.6 strcmp(): This compares two strings. The syntax is: strcmp(char * str1, char * str2);

The resulting values of the comparison is: < 0 str1 is less than str2 0 str1 is equal to str2 > 0 str1 is greater than str2 For ex: char str1[8] = “Jamaica”; char str2[8] = “Jamaica”; if(strcmp(str1,str2) == 0) printf(“\n\n The strings are equal”);

4.7 strncmp(): This is comparing a specified number of characters of one string to another string. The syntax is: strncmp(char * str1, char * str2, size); The comparison until size character have been compared or end of str1 has been reached. The comparison is case- sensitive. 4.8 strcmpi() or stricmp(): This follows the same as strcmp() or strncmp(), except it is not case-sensitive.

4.9 strchr(): This function finds the first occurrence of a specified character in a string. The syntax is: strchr(char * str, int ch); The function searches str from left to right until ch is found or null character is found. This returns the address of the first occurrence of the character in the string. The position can be found by subtracting the address of occurrence from the address of the string.

Ex: char str[8] = “Jamaica”; char *loc; int pos; loc = strchr(str, “a”); pos = str - loc;

4.10 strrchr(): This searches the string for the last occurrence of the specified character in the string. The syntax is: strrchr(char * str, int ch); 4.11 strstr(): This searches the string for the first occurrence of the specified string. The syntax is: strstr(char *str1, char *str2);

4.12 strlwr(): This function changes the case of string to lower case. The syntax is: strlwr(char *str); 4.13 strupr(): This function changes the case of string to upper case. The syntax is: strupr(char *str);

4.14 strrev(): This function reverses the order of characters in a string. The syntax is: strrev(char *str); 4.15 strset(): This function changes all the characters in the string to character specified. The syntax is: strset(char *str, int ch);

4.16 strnset(): This function changes all the characters in the string to character specified, but it changes the first n characters. The syntax is: strnset(char *str, int ch, size);

String to Number Function 4.17 atoi(): This converts a string to an integer. The syntax is: atoi(char *str); For Ex: int a; char c[3] = “123”; a = atoi(c);

4.18 atof(): This converts a string to type double. The syntax is: atof(char *str); For ex: double a; char c[6] = “123.45”; a = atof(c);

5. Passing String Array to the Function The string array is passed in the similar multidimensional integer array. In the function prototype, we should pass the array in the following manner: datatype function name (char arrayname[ ][size of individual string elements]); datatype function name(char * arrayname[ ]); datatype function name(char arrayname[No. of strings][size of each string]);

In the calling function in main, the array should be referred to as: function name(array name); In the function definition, the array is declared as: datatype function name(char array name[ ][size]) { } datatype function name(char array name[size][size]) { }