Suyash Bhardwaj Dept. of Computer Science & Engineering Faculty of Engineering & Technology Gurukul Kangri University, Haridwar l1l1 UNIT 1 String Processing.

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Advertisements

Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
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.
Lecture 24: Strings. 2 Lecture Contents: t Library functions t Assignment and substrings t Concatenation t Comparison t Demo programs t Exercises.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Chapter 10.
C++ Data Type String A string is a sequence of characters enclosed in double quotes. Examples of strings: “Hello” “CIS 260” “Students” The empty string.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
Arrays Chapter 6.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Programming Strings. COMP102 Prog. Fundamentals: Strings / Slide 2 Character Strings l A sequence of characters is often referred to as a character “string”.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
Computer Science 1620 Strings. Programs are often called upon to store and manipulate text word processors chat databases webpages etc.
Introduction to C programming
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
9-1 Learning Objectives  An Array Type for Strings  C-Strings.
Introduction to Programming Lecture 8. String Handling  Character is the building block of strings.  Characters are represented inside the computer.
Characters, Strings, And The string Class Chapter 10.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
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.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Chapter 9 Strings. Learning Objectives An Array Type for Strings – C-Strings Character Manipulation Tools – Character I/O – get, put member functions.
Sahar Mosleh California State University San MarcosPage 1 Character String.
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.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
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.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
Basic concepts of C++ Presented by Prof. Satyajit De
Characters, Strings, and the cstring Library
Chapter 8 Strings and Vectors 1
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Fundamentals of Characters and Strings
Characters, Strings, and the cstring Library
Characters, C-Strings, and More About the string Class
Strings A string is a sequence of characters treated as a group
Standard Version of Starting Out with C++, 4th Edition
CS111 Computer Programming
C Stuff CS 2308.
Pointers, Dynamic Data, and Reference Types
Strings A collection of characters taken as a set:
String in C++.
C-strings In general, a string is a series of characters treated as a unit. Practically all string implementations treat a string as a variable-length.
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
EECE.2160 ECE Application Programming
Strings What is a string? It is an array of characters terminated with
CPS120: Introduction to Computer Science
CS 144 Advanced C++ Programming February 7 Class Meeting
Standard Version of Starting Out with C++, 4th Edition
C++ Programming Lecture 20 Strings
Strings Skill Area 313 Part C
CS-161 Computer Programming Lecture 15 & 16: Arrays II
Introduction to Problem Solving and Programming
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

Suyash Bhardwaj Dept. of Computer Science & Engineering Faculty of Engineering & Technology Gurukul Kangri University, Haridwar l1l1 UNIT 1 String Processing

In this Unit l2l2 Structure of string storage: fixed length, variable length, linked list storage. String operations: Indexing,concatenation, length. Applications of string processing: Insertion, deletion, pattern matching, text formation.

Character Strings l3l3 A sequence of characters is often referred to as a character “string”. A string is stored in an array of type char ending with the null character '\0 '.

l A string containing a single character takes up 2 bytes of storage. Character Strings l4l4

l5l5

l6l6

Character vs. String l7l7 A string constant is a sequence of characters enclosed in double quotes. For example, the character string: char s1[2]= " a "; //Takes two bytes of storage. s1: On the other hand, the character, in single quotes: char s2= `a`; //Takes only one byte of storage. s2: a\0 a

Character vs. String l8l8

l #include l int main() l { l char s[1000],i; l printf("Enter a string: "); l scanf("%s",s); l for(i=0; s[i]!='\0'; ++i); l printf("Length of string: %d",i); l return 0; l } Length of string l9l9

#include struct node { int data; struct node* next; }; void push(struct node** head_ref, int new_data) { struct node* new_node = (struct node*) malloc(sizeof(struct node)); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } int getCount(struct node* head) { int count = 0; // Initialize count struct node* current = head; // Initialize current while (current != NULL) { count++; current = current->next; } return count; } int main() { struct node* head = NULL; push(&head, 1); push(&head, 3); push(&head, 1); push(&head, 2); push(&head, 1); printf("count of nodes is %d", getCount(head)); return 0; } l 10

Example 1 l 11 char message1[12] = "Hello world"; cout << message1 << endl; message1: char message2[12]; cin >> message2; // type "Hello" as input message2:

Example 2: String I/O l 12 String can be input using the extraction operator >>, but one or more white spaces indicates the end of an input string. char A_string[80], E_string[80]; cout << "Enter some words in a string:\n"; cin >> A_string >> E_string; cout << A_string << E_string << “\nEND OF OUTPUT\n"; Output: Enter some words in a string: This is a test. Thisis END OF OUTPUT

getline l 13 The function getline can be used to read an entire line of input into a string variable. The getline function has three parameters: The first specifies the area into which the string is to be read. The second specifies the maximum number of characters, including the string delimiter. The third specifies an optional terminating character. If not included, getline stops at ‘\n’.

Example 3: getline l 14 char A_string[80]; cout << "Enter some words in a string:\n"; //80 is the size of A_string cin.getline(A_string, 80); cout << A_string << “\nEND OF OUTPUT\n"; Output: Enter some words in a string: This is a test. END OF OUTPUT

Example 4: getline Example l 15 char A_string[5], E_string[80]; cout << "Enter some words in a string:\n"; cin >> A_string; cin.getline (E_string, 9) ; cout << A_string << "#" << E_string << “\nEND OF OUTPUT\n"; Output: Enter some words in a string: This is a test. This# is a te END OF OUTPUT

Example 5: getline Example l 16 char lastName[30], firstName[30]; cout :\n"; cin.getline (lastName, sizeof(lastName), ','); cin.getline (firstName, sizeof(firstName)); cout << "Here is the name you typed:\n\t|" << firstName << " " << lastName << "|\n"; Output: Enter a name in the form : Chan,Anson Here is the name you typed: |Anson Chan|

l 17

Ex. 6: String Copy Function in l 18 void strcpy(char dest[], const char src[]); // copies string src into string dest example: char name1[16], name2[16]; strcpy(name1,"Chan Tai Man"); name1: name2: strcpy(name2," "); strcpy(name2,name1); ChanTaiMan\0??? ChanTaiMan

Example 7: strcpy in l 19 #include using namespace std; int main (){ char string_1[6] = "Short";// character strings char string_2[17] = "Have a Nice Day"; char string_3[6] = "Other"; strcpy(string_1, string_2); return 0; }

Ex. 8: String Length Check Function in l 20 // string prototype, already included in string.h //returns length of string(not counting'\0‘) //you don't need to include it in your program int strlen(const char[]); int string_length = strlen("abcde"); //string_length is set to 5.

Ex. 9: String Length Check Function Example l 21 #include using namespace std; int main(){ char string_1[5] = "ABCD", string_2[10]=" "; cout << "String 1 = " << string_1 << endl; cout << "String 2 = " << string_2 << endl; strncpy(string_1,string_2,strlen(string_1)); cout << "After copying, string 1 = " << string_1<<endl; return 0; } //output: String 1 = ABCD String 2 = After copying, string 1 = 1234

Ex. 10: String Length Check Function in l 22 //Copy the value of a string to a string variable #include using namespace std; void string_copy(char target[], const char source[], int target_size); //Before: target_size is the declared size of target. //not including '\0'. //After: The value of target has been set to the string //value in source, provided the declared size of //target is large enough. If target is not large //enough to hold the entire string, a string //equal to the length of target will be stored.

Ex. 10: String Copy and String Length Check l 23 int main( ) //Driver function { char short_string[11]; char long_string[] = "This is rather long."; cout << long_string << "STRING ENDS HERE.\n"; string_copy(short_string, "Hello", 10); cout << short_string << "STRING ENDS HERE.\n"; string_copy(short_string, long_string, 10); cout << short_string << "STRING ENDS HERE.\n"; return 0; }

Ex. 10: String Copy and String Length Check l 24 //Uses string.h: void string_copy(char target[], const char source[], int target_size) { int index; int new_length = strlen(source); if (new_length > (target_size)) new_length = target_size ; for (index = 0; index < new_length; index++) target[index] = source[index]; target[index] = '\0'; }

Ex. 10: String Copy and String Length Check l 25 Output: This is rather long.STRING ENDS HERE. HelloSTRING ENDS HERE. This is raSTRING ENDS HERE.

String Comparison l 26 int strcmp(char s1[], char s2[]); /*compares strings s1 and s2, returns < 0 if s1 < s2 = 0 if s1 == s2 (i.e. strcmp returns false) > 0 if s1 > s2 */ int strncmp(char s1[], char s2[], int limit); /* Same as strcmp except that at most limit characters are compared. */

String Comparison l 27 int comp102_strncmp(char s1[], char s2[], int limit) { for (int i=0; i < limit; i++){ if (s1[i] < s2[i]) return -1; if (s1[i] > s2[i]) return 1; } return 0; }

l 28

Ex. 11: String Comparison Examples l 29 str1 str2return valuereason “AAAA”“ABCD”<0‘A’ <‘B’ “B123”“A089”>0‘B’ > ‘A’ “127”“409” <0‘1’ < ‘4’ “abc888” =0equal string “abc”“abcde”<0str1 is a sub string of str2 “3”“12345”>0‘3’ > ‘1’

Some Common Errors l 30 It is illegal to assign a value to a string variable (except at declaration). char A_string[10]; A_string = "Hello"; // illegal assignment Should use instead strcpy (A_string, "Hello");

Ex. 12: Some Common Errors l 31 The operator == doesn't test two strings for equality. if (string1 == string2) //wrong cout << "Yes!"; // illegal comparison Should use instead if (!strcmp(string1,string2)) cout << "Yes they are same!"; //note that strcmp returns 0 (false) if //the two strings are the same.