Character & String Knowledge

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.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Copyright  Hannu Laine C++-programming Part 5 Strings.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
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.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Character and String definitions, algorithms, library functions Characters and Strings.
1 DATA ABSTRACTION: USER DEFINED TYPES AND THE CLASS.
Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
1 Structure Knowledge Understand the concept of structure data types Skills Able to write application program using structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
1 Dimension of String Array An array of string is two dimension array.
Declarations/Data Types/Statements. Assignments Due – Homework 1 Reading – Chapter 2 – Lab 1 – due Monday.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Computer Science Department FTSM Input and Output Knowledge: Understand various types of input and output syntaxes Skill: Develop a program to read/capture.
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
BBS514 Structured Programming (Yapısal Programlama)1 Character Processing, Strings and Pointers,
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
CISC105 – General Computer Science Class 9 – 07/03/2006.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
Road map char data type Reading –Liang 5: Chapter 2: 2.7.4; 2.9; –Liang 6: Chapter 2: 2.7.4; 2.9 –Liang 7: Chapter 2: 2.7.4; 2.9.
Number Representation Lecture Topics How are numeric data items actually stored in computer memory? How much space (memory locations) is.
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).
Characters and Strings
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
Pointers and Arrays Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
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.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
INC 161 , CPE 100 Computer Programming
Fundamentals of Characters and Strings
Characters and Strings
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 1 Introduction to C Programming
Lecture 8 String 1. Concept of strings String and pointers
Module 7: Input/Output Operations ITEI102 Introduction to Programming
Lecture-5 Arrays.
CSC 113: Computer Programming (Theory = 03, Lab = 01)
Strings A string is a sequence of characters treated as a group
Arrays in C.
CS111 Computer Programming
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Engr 0012 (04-1) LecNotes
INC 161 , CPE 100 Computer Programming
Computer Science Department
Strings A collection of characters taken as a set:
C Characters and Strings – Review Lab assignments
Systems Programming Concepts
File Manipulation Knowledge Understand the concept of file input and output Skills Able to write an application program involving input output files.
Chapter 8 Character Arrays and Strings
Line at a time I/O with fgets() and fputs()
Example: ? str1 str2 _ void salin(char sasaran[], char sumber[]);
POINTER CONCEPT 4/15/2019.
Arrays.
Lecture 19: Working with strings
Character Arrays char string1[] = “first”;
Characters and Strings
Variables and Constants
POINTER CONCEPT 8/3/2019.
Systems Programming Concepts
Files Chapter 8.
Presentation transcript:

Character & String Knowledge Understand the basic concept of character and string Skills Able to write an application program to manipulating character and string

String String is a list of character. In C programming, string constant must be declared in a pair of “ “ Examples: "FTSM“ "03-8921 0001 samb 2004"

Memory representation for string Example (array of character) char model[ ] = {'S', '-', '1', '2', '4', '\0'}; [0] 'S' [1] '-' [2] '1' [3] '2' [4] '4' [5] '\0' model

Memory representation for string In C, memory representation for string can be depicted by an array of character array. Bear in mind, the last character in character string must be null character(i.e ‘\0’)

Memory representation for string Example: Memory representation for "S-124" 'S' '-' '1' '2' Null character '4' '\0'

Memory representation for string Compare the memory representation for char model[ ] = {'S', '-', '1', '2', '4', '\0'}; [0] 'S' [1] '-' [2] '1' [3] '2' [4] '4' [5] '\0' model

_ Examples of String The input specification using scanf() for string character is %s. Example: char jenama[15]; printf("Masukkan jenama: "); scanf("%s", jenama); ? jenama

Masukkan jenama: Examples of String The input specification using scanf() for string character is %s. Example: char jenama[15]; printf("Masukkan jenama: "); scanf("%s", jenama); ? jenama

Masukkan jenama: Singer Examples of String The input specification using scanf() for string character is %s. Example: char jenama[15]; printf("Masukkan jenama: "); scanf("%s", jenama); 'S' 'i' 'n' 'g' 'e' 'r' '\0' ? jenama

_ Examples of String The input specification using scanf() for string character is %s. Example: char jenama[15]; printf("Masukkan jenama: "); scanf("%s", jenama); 'M' 'i' 't' 's' 'u' 'b' 'h' '\0' ? jenama

String output Output of String Jenama: Mitsubishi _ String output Output of String Output specification for string character is %s . Example : char jenama[15] = "Mitsubishi"; printf(“Jenama: %s\n", jenama); 'M' 'i' 't' 's' 'u' 'b' 'h' '\0' ? jenama

Accessing String Individual character in string can be referred as an array.

Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: "); while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i _

Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: "); while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i Nama: _

Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: "); while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i Nama: _

Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: "); while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i Nama: A_

Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: "); while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 1 Nama: A_

Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: "); while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 1 Nama: A_

Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: "); while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 1 Nama: A _

Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: "); while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 2 Nama: A _

Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: "); while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 2 Nama: A _

Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: "); while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 2 Nama: A B_

Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: "); while (nama[i] != '\0') { printf("%c", nama[i]); i++; } Until … 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 3 Nama: A B_

Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: "); while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 6 Nama: A Bin _

Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: "); while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 6 Nama: A Bin B_

Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: "); while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 7 Nama: A Bin B_

Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: "); while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 7 Nama: A Bin B_

Example : char nama[15] = "A Bin B"; int i = 0; printf("Nama: "); while (nama[i] != '\0') { printf("%c", nama[i]); i++; } 'A' ' ' 'B' 'i' 'n' '\0' ?? nama i 7 Nama: A Bin B_

String assignment String cannot be directly assign to an array (except in the case of initialization of character) Example: char digit[11]; digit = "0123456789"; /* Cannot be */

String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; ?? digit aksara ??

String assignment Item by item Example: char digit[15], aksara; Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; ?? digit aksara '0'

String assignment Item by item Example: char digit[15], aksara; Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; ?? digit aksara '0'

String assignment Item by item Example: char digit[15], aksara; Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' ?? digit aksara '0'

String assignment Item by item Example: char digit[15], aksara; Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' ?? digit aksara '1'

String assignment Item by item Example: char digit[15], aksara; Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' ?? digit aksara '1'

String assignment Item by item Example: char digit[15], aksara; Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' '1' ?? digit aksara '1'

String assignment Item by item Example: char digit[15], aksara; Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' '1' ?? digit aksara '2'

String assignment Item by item Example: char digit[15], aksara; Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' '1' ?? digit aksara '2'

String assignment Item by item Example: Until … Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' '1' '2' ?? digit aksara '2' Until …

String assignment Item by item Example: char digit[15], aksara; Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' ?? digit aksara '9'

String assignment Item by item Example: char digit[15], aksara; Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' ?? digit aksara ':'

String assignment Item by item Example: char digit[15], aksara; Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' ?? digit aksara ':'

String assignment Item by item Example: char digit[15], aksara; Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' '\0' ?? digit aksara ':'

String assignment Item by item Example: char digit[15], aksara; Nilai ASCII '0' = 48 Nilai ASCII '1' = 49 Nilai ASCII '2' = 50 Nilai ASCII '3' = 51 Nilai ASCII '4' = 52 Nilai ASCII '5' = 53 Nilai ASCII '6' = 54 Nilai ASCII '7' = 55 Nilai ASCII '8' = 56 Nilai ASCII '9' = 57 String assignment Item by item Example: char digit[15], aksara; for (aksara = '0'; aksara <= '9'; aksara++) digit[aksara-'0'] = aksara; digit[aksara-'0'] = '\0'; '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' '\0' ?? digit aksara ':'