Engr 0012 (04-1) LecNotes 24-01.

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

 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.
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.
 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.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
CS1061 C Programming Lecture 14: Strings A. O’Riordan, 2004.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes C++ errors/debugging build/compile compiler does not recognize a statement build/compile.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
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.
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
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.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes Contrasting MATLAB with C MATLABC language Workspace - interactive computation No real.
1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Computer Organization and Design Pointers, Arrays and Strings in C
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Computer Science 210 Computer Organization
Fundamentals of Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
Functions, Part 2 of 2 Topics Functions That Return a Value
Computer science C programming language Lesson 5
Strings A string is a sequence of characters treated as a group
Computer Science 210 Computer Organization
Arrays in C.
Programming Languages and Paradigms
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
CS111 Computer Programming
Engr 0012 (04-1) LecNotes
C Stuff CS 2308.
Engr 0012 (04-1) LecNotes
INC 161 , CPE 100 Computer Programming
Week 9 – Lesson 1 Arrays – Character Strings
Review for Final Exam.
Strings A collection of characters taken as a set:
Functions, Part 2 of 3 Topics Functions That Return a Value
CprE 185: Intro to Problem Solving (using C)
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
Strings What is a string? It is an array of characters terminated with
Chapter 8 Character Arrays and Strings
CPS120: Introduction to Computer Science
Lec 11.
Example: ? str1 str2 _ void salin(char sasaran[], char sumber[]);
Functions Extra Examples.
Arrays.
Chapter 9: Pointers and String
C++ Programming Lecture 20 Strings
Character Arrays char string1[] = “first”;
Strings #include <stdio.h>
Programming Languages and Paradigms
Library in c++ ( as cmath , iostream , …… etc.)
C Characters and Strings
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Introduction to Problem Solving and Programming
Presentation transcript:

Engr 0012 (04-1) LecNotes 24-01

strings strings are an array of type char string declaration // include libraries #include<string.h> // defined constants #define MAXSTRING 11 main() { // begin main // variable declaration char string1[ ] = "A string"; char string2[5]; char string3[MAXSTRING]; int numchar, order; string declaration include string library used defined constant for max length + 1 memory “map” |A| |s|t|r|i|n|g|| NULL |?|?|?|?|?| |?|?|?|?|?|?|?|?|?|?|?| Engr 0012 (04-1) LecNotes 24-02

strings string initialization “hardwire” at declaration // include libraries #include<string.h> // defined constants #define MAXSTRING 11 main() { // begin main // variable declaration char string1[ ] = "A string"; char string2[5]; char string3[MAXSTRING]; int numchar, order; string initialization “hardwire” at declaration |A| |s|t|r|i|n|g|| use strcpy function // string initialization strcpy(string2,"hi"); |h|i||?|?| get it from the keyboard // string initialization printf("\nEnter a string \n==> "); fflush(stdin); scanf("%[^\n]", string3); best - read until return |H|i| |t|h|e|r|e|!||?| // string initialization printf("\nEnter a string \n==> "); fflush(stdin); scanf(" %s", string3); not as good |H|i||?|?|?|?|?|?|?|?| Engr 0012 (04-1) LecNotes 24-03

strings string display string placeholder is %s string1 = A string // displaying strings printf( "\n\nstring1 = %s", string1 ); printf( "\n\nstring2 = %s", string2 ); printf( "\n\nstring3 = %s", string3 ); string placeholder is %s string1 = A string string2 = hi string3 = Hi there! Engr 0012 (04-1) LecNotes 24-04

strings copying strings - strcpy strcpy( string3, string1); printf( "\n\nstring1 = %s", string2 ); printf( "\n\nstring3 = %s", string3 ); copies contents of second argument into first argument ==> copies string1 into string3 string1 before |A| |s|t|r|i|n|g|| string3 before |H|i| |t|h|e|r|e|!||?| string3 after |A| |s|t|r|i|n|g|||?| string1 = A string string3 = A string display Engr 0012 (04-1) LecNotes 24-05

strings string length - strlen display numchar = strlen( string3 ); printf("\n\nnum char in \"%s\" : %d", string3, numchar ); strlen counts characters until NULL is reached string3 |A| |s|t|r|i|n|g|||?| num char in "A string" : 8 display Engr 0012 (04-1) LecNotes 24-06

strings comparing strings - strcmp display // compare strings order = strcmp( string1, string2); if ( order < 0 ) { printf( "\n\n%s before %s", string1, string2 ); } else if ( order == 0 ) { printf( "\n\n%s equals %s", string1, string2 ); } else { printf( "\n\n%s after %s", string1, string2 ); } strcmp determines alphabetical order of two strings string1 |A| |s|t|r|i|n|g|| string2 |h|i||?|?| A string before hi display Engr 0012 (04-1) LecNotes 24-07

strings parameters in function calls strings are an array of type char parameter useage follows same rules as any array // prototypes void getstring( char string[] ); formal parameter requires type (char), name, and [ ] to designate array main() { … getstring( string3 ); actual parameter requires local name Engr 0012 (04-1) LecNotes 24-08