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.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

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.
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.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
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.
Elementary Data Structures: Part 2: Strings, 2D Arrays, Graphs
Pointers in C Rohit Khokher
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
Chapter 10.
Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
Friday, January 05, 2007 A few weeks of developing and testing can save a whole afternoon in the library. -Anonymous.
Arrays Chapter 6.
Chapter 9: Arrays and Strings
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
Chapter 8 Arrays and Strings
C-strings Array with base type char One character per indexed variable
C Arrays and Pointers In Java, pointers are easy to deal with –In fact, there is little that can go wrong in Java since pointer access is done for you.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Chapter 5 Arrays and Strings C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.
STRINGS & STRING HANDLING FUNCTIONS STRINGS & STRING HANDLING FUNCTIONS.
Chapter 8 Arrays and Strings
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages in Chapter 7, and pages in Chapter 8.  Homework #9 and Lab #9 due next week.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 6 Array and String.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
© Oxford University Press All rights reserved. CHAPTER 6 STRINGS.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
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.
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.
An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character.
String operations. About strings To handle strings more easily, we need to include a library> #include To see what the library allows us to do, look here:
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
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
In C programming, one of the frequently arising problem is to handle similar types of data. For example: If the user want to store marks of 100 students.
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous memory allocation.
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.
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
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.
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.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
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.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Computer Programming BCT 1113
Lecture 8 String 1. Concept of strings String and pointers
CS111 Computer Programming
INC 161 , CPE 100 Computer Programming
Lecture 11 Strings.
Dr. Khizar Hayat Associate Prof. of Computer Science
Presentation transcript:

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 as string_2 ? – Append string_1 at the end of string_2 – Sort a collection of strings in ascending order – Find occurrence of a character in a given sentence (string)

Strings The string in C programming language is actually a one-dimensional array of characters which is terminated by a null character '\0‘. The following declaration and initialization create a string consisting of the word "Hello“: char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

Strings This is how this char array looks in memory contains the above string Be careful about the length of string’s array length No bounds checking by the compiler May result in unpredictable results Hello\0

Strings Example – Placement of NULL Write a program to declare a char array, initialize it to the string “Hello” and then move around the NULL character to see its effects

Strings Manipulation Because dealing with strings is cumbersome, a library of functions is available for programmers to manipulate strings Some of the more frequently used string manipulation functions are: – strlen(str); //returns length of a str – strcpy(dest_str, src_str);//copies source str in dest str – strcat(dest_str, src_str); //append source str at the end of dest str – strcmp(Str1, Str2);//compares two strings lexicographically //returns 0 if both are equal, negative number if LHS is < RHS

Strings Example – strlen() function Write a program to declare a char array, initialize it to a string and then traverse the string in forward and backward direction

Strings Example – strcpy(), strcat() functions Write a program to declare two char arrays and initialize them to some strings. Declare a third char array and copy a concatenation of the first two strings into the third array.

String I/O Another convenient way for string I/O is: – gets(str); //read a string from user and store in str – puts(str); //print a string contained in str

Strings Example – strcmp() function Write a program to declare two char arrays and initialize them to some strings. Compare the two strings using strcmp() function

Strings Example – strcmp() function Write a program to input a list of names. Sort the list of names in ascending (lexicographical / dictionary) and descending orders.

Algorithm – Sorting an array of names (strings) You have a 2-Dimensional array containing 4 names char names[4][50]; The array has 4 rows and 50 columns each row can hold one name of length 50 characters (including the NULL that comes at the end of a string

Algorithm – Sorting an array of names (strings) To input names, you only need to specify the row where the name is to be stored for(i=0;i<10;++i) gets(str[i]); Here, i is the index of the row where every name will be stored The function gets() takes care of where every character of a name goes and also the string terminating NULL character

Algorithm – Sorting an array of names (strings)

Loops to compare strings and swap places for(i=0;i<3;++i) for(j=i+1;j<4 ;++j){ if(strcmp(str[i],str[j])> 0) { strcpy(temp,str[i]); strcpy(str[i],str[j]); strcpy(str[j],temp); }

Algorithm – Sorting an array of names (strings) Row 1 Row 2 Row 3 Row 4 Col 1, col 2, col 3, …. Col 50

Algorithm – Sorting an array of names (strings) i j Compare and swap if(strcmp(str[i],str[j])> 0)

Algorithm – Sorting an array of names (strings) The array now looks like this a d a m \0 … t y l e r \0 … s o p h i a \0 … r a c h e l \0 … i j

Algorithm – Sorting an array of names (strings) a d a m \0 … t y l e r \0 … s o p h i a \0 … r a c h e l \0 … i j Compare and swap if(strcmp(str[i],str[j])> 0)

Algorithm – Sorting an array of names (strings) a d a m \0 … t y l e r \0 … s o p h i a \0 … r a c h e l \0 … i j Compare and swap if(strcmp(str[i],str[j])> 0)

Algorithm – Sorting an array of names (strings) a d a m \0 … t y l e r \0 … s o p h i a \0 … r a c h e l \0 … i j i is incremented (i++), j is re-initialized to j=i+1 Compare and swap if(strcmp(str[i],str[j])> 0)

Algorithm – Sorting an array of names (strings) a d a m \0 … s o p h i a \0 … t y l e r \0 … r a c h e l \0 … i j Array now looks like this

Algorithm – Sorting an array of names (strings) a d a m \0 … s o p h i a \0 … t y l e r \0 … r a c h e l \0 … i j Compare and swap if(strcmp(str[i],str[j])> 0)

Algorithm – Sorting an array of names (strings) a d a m \0 … r a c h e l \0 … t y l e r \0 … s o p h i a \0 … i j Array now looks like this

Algorithm – Sorting an array of names (strings) a d a m \0 … r a c h e l \0 … t y l e r \0 … s o p h i a \0 … i j i is incremented (i++), j is re-initialized to j=i+1 Compare and swap if(strcmp(str[i],str[j])> 0)

Algorithm – Sorting an array of names (strings) a d a m \0 … r a c h e l \0 … s o p h i a \0 … t y l e r \0 … i j Compare and swap if(strcmp(str[i],str[j])> 0)

Algorithm – Sorting an array of names (strings) a d a m \0 … r a c h e l \0 … s o p h i a \0 … t y l e r \0 … Finally, the sorted array looks like this

Strings Example – counting words Write a program to input a sentence (string) and count the number of words in it.

Strings Example – frequency of a character Write a program to input a sentence (string) and find the frequency of a given character.

Strings Example – Palindrome or Not Write a program to input a word and find out if it is a palindrome. Civic, mom, dad, kayak, radar, madam, racecar Function strrev(str) reverses a string