Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
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 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.
Chapter 9 Character Strings
Chapter 10.
1 Strings ( מחרוזות ). 2 Agenda Definition and initialization Termination Input / Output String library.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
Introduction to C Programming CE Lecture 13 Strings in C.
To remind us We finished the last day by introducing If statements Their structure was:::::::::
Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]
1 More on Pointers. 2 Reminder 3 Pointers Pointer is a variable that contains the address of a variable Here P is said to point to the variable C C 7.
String What it is Why it’s useful library routines for handling strings how to input a string from the keyboard.
C Programming Strings. Array of characters – most common type of array in C  Let’s make them easier for use Denote the end of array using a special character.
Computer Science 210 Computer Organization Strings in C.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
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.
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.
BBS514 Structured Programming (Yapısal Programlama)1 Character Processing, Strings and Pointers,
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
Intro to C Part 3: © Walter Milner 2005: Slide 1 Introduction to ANSI C Part Three  Strings  Strings in structs  File handling  Linked list example.
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’};
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Chapter 15 Strings as Character Arrays
Slides from Shane Griffith (TA and guest instructor in Fall 2008) CprE 185: Intro to Problem Solving.
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).
Strings, Pointers and Tools
Strings, Slide Fundamental Programming Strings.
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
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.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
C Characters and Strings
Fundamentals of Characters and Strings
© 2016 Pearson Education, Ltd. All rights reserved.
Lecture 8 String 1. Concept of strings String and pointers
Strings (מחרוזות).
Lecture-5 Arrays.
A First Book of ANSI C Fourth Edition
Strings A string is a sequence of characters treated as a group
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
CS111 Computer Programming
INC 161 , CPE 100 Computer Programming
C Programming Strings.
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
Chapter 8 Character Arrays and Strings
Exercise Arrays.
Strings in C Array of characters is called a string.
Lecture 19: Working with strings
Programming Strings.
C Characters and Strings
Introduction to Problem Solving and Programming
Presentation transcript:

Exercise 7 Strings

An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;

The Terminator Strings terminate with NULL character, signed by ‘\0’ (ascii code 0) This is a convention used to know where the string ends It means that in order to hold a string of 7 chars we need an array of length 8 So the previous initialization is equivalent to char A[ ] = {‘b’, ‘l’, ‘a’, ‘b’, ‘l’, ‘a’, ‘\0’};

The fool on the null #include int main(void) { char str[]="I'm a full string"; printf("%s\n", str); str[7]='o'; str[8]='o'; printf("%s\n", str); str[11]='\0'; printf("%s\n", str); str[11] = ‘s’; printf("%s\n", str); return 0; }

Reading-in strings There are several ways of accepting strings as input from the user The obvious way to go is read character by character using getchar()

Example Read_String_with_getchar.c

Reading-in strings: scanf A simpler way is to use scanf To read in a string to a variable str, write :  scanf(“%s”, str);  Note there’s no ‘&’ sign!!!

Reading-in strings - scanf scanf reads-in letters until a space or newline (‘\n’) is encountered The maximum length can be stated in the parentheses:  scanf(“%10s”, str);  This will read in 10 letters, plus the ‘\0’ sign (so str should have place for 11 characters)

Example Read_String_with_scanf.c

Comparing strings We cannot just compare strings’ contents by == char A[7]=“Hello”; char B[7]=“Hello”; if (A==B) {... } Because A and B are addresses of A[0] and B[0] A==B only if A and B are the same string in memory In order to compare the contents we must scan char by char ‘H’‘e’‘l’ ‘o’‘\0’…. B ‘H’‘e’‘l’ ‘o’‘\0’…. A

Example Compare.c

Compare – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 0 i ‘Y’‘e’‘w’‘\0’ A

Compare – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 0 i ‘Y’‘e’‘w’‘\0’ A

Compare – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 1 i ‘Y’‘e’‘w’‘\0’ A

Compare – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 1 i ‘Y’‘e’‘w’‘\0’ A

Compare – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 2 i ‘Y’‘e’‘w’‘\0’ A

Compare – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 2 i ‘Y’‘e’‘w’‘\0’ A

Compare – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 2 i ‘Y’‘e’‘w’‘\0’ A

Compare – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 2 i ‘Y’‘e’‘w’‘\0’ A

Equal strings – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 0 i ‘Y’‘e’‘s’‘\0’ A

Equal strings – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 0 i ‘Y’‘e’‘s’‘\0’ A

Equal strings – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 1 i ‘Y’‘e’‘s’‘\0’ A

Equal strings – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 1 i ‘Y’‘e’‘s’‘\0’ A

Equal strings – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 2 i ‘Y’‘e’‘s’‘\0’ A

Equal strings – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 2 i ‘Y’‘e’‘s’‘\0’ A

Equal strings – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 3 i ‘Y’‘e’‘s’‘\0’ A

Equal strings – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 3 i ‘Y’‘e’‘s’‘\0’ A

Different length – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 0 i ‘Y’‘e’‘\0’ A

Different length – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 0 i ‘Y’‘e’‘\0’ A

Different length – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 1 i ‘Y’‘e’‘\0’ A

Different length – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 1 i ‘Y’‘e’‘\0’ A

Different length – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 2 i ‘Y’‘e’‘\0’ A

Different length – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 1 i ‘Y’‘e’‘\0’ A

Different length – step by step for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0; B ‘Y’‘e’‘s’‘\0’ 1 i ‘Y’‘e’‘\0’ A

String library Like in the case of stdio.h and math.h, we have a special library for handling strings We should #include string.h Functions:  strlen(s) – returns the length of s  strcmp(s1, s2) – compares s1 with s2  strcpy(s1, s2) – copies to contents of s2 to s1  and more…

Example easy_compare.c

If there’s time… Exercise: write a program that:  gets an input string from the user (up to 100 chars, no white spaces)  gets 2 characters from the user  replaces each occurrence of the first character in the string by the second character, and prints the result. Example:  input: “papa”, ‘p’, ‘m’  output: “mama”

Solution replace.c

If there’s time… Exercise: write a function:  void my_to_lower(char str[]);  The function receives an arbitrary string and converts all its letters to lower-case letters Demonstrate the use of your function by some example that prints the input and output on the screen