240-222 CPT: Strings/101 240-222 Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.

Slides:



Advertisements
Similar presentations
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
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.
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.
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.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
C programming---String. String Literals A string literal is a sequence of characters enclosed within double quotes: “hello world”
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
Introduction to C Programming CE Lecture 13 Strings in C.
CS1061 C Programming Lecture 14: Strings A. O’Riordan, 2004.
Tutorial #8 Summer strings #include int main() { char str1[] = {‘h’,’e’,’l’,’l’,’o’}; char str[] = {‘h’,’e’,’l’,’l’,’o’,’\0’}; char p[] = ”hello”;
C-Strings Joe Meehean. C-style Strings String literals (e.g., “foo”) in C++ are stored as const char[] C-style strings characters (e.g., ‘f’) are stored.
 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.
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
Introduction to C programming
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Strlen() implementation /* strlen : return length of string s */ int strlen(char *s) { int n; for (n = 0 ; s[n] != ‘\0’ ; n++) ; return n; } /* strlen.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
CS 162 Introduction to Computer Science Chapter 17 C++ String Objects Herbert G. Mayer, PSU (Copied from Prof. Phillip Wong at PSU) Status 11/30/2014.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
C Program Design C Characters and Strings 主講人:虞台文.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
Xuan Guo Review for the Final Exam Xuan Guo July 29 8:30AM – 10:30AM Classroom South 300 CSC
Chapter 8 Characters and Strings Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
1 This chapter covers both string constants (or literals, as they're called in the C standard) and string variables, which can change during the execution.
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.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
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.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
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).
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
Chapter 1 Basic C Programming
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
1 Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character Handling Library 8.4String Conversion.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
ECE Application Programming
C Characters and Strings
Fundamentals of Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
Chapter 8 - Characters and Strings
INC 161 , CPE 100 Computer Programming
EECE.2160 ECE Application Programming
Strings What is a string? It is an array of characters terminated with
Strings #include <stdio.h>
Characters and Strings Functions
C Characters and Strings
EECE.2160 ECE Application Programming
Presentation transcript:

CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship to pointers 10. Strings Chapter 8

CPT: Strings/102 Overview: 1.What is a String? 2.Accessing Strings with Pointers 3.Word Counting 4.String Conversion Functions 5.I/O of Strings 6.String Manipulation

CPT: Strings/ What is a String? A string is a sequence of charcters which end with the NULL character '\0'. e.g: "hello" " Call" "9"

CPT: Strings/104 Use: char color[] = "blue"; l or equivalently: char color[] = {'b','l','u','e','\0'};

CPT: Strings/105 "a" is not 'a' char example[] = "a"; looks like: A character array for a string must always have 1 extra element for the '\0' character. ‘a’‘\0’

CPT: Strings/ Accessing Strings with Pointers char name[] = "Andrew"; l is equivalent to: char *name = "Andrew"; printf("%s %s\n", name, name+2); Print out: Andrew drew ‘A’‘n’‘d’‘r’‘e’‘w’‘\0’ name

CPT: Strings/ Word Counting #include #include /* for isspace() */ int words_cnt(char *); int main() { char *test = "hello world"; printf("Word count: %d\n", words_cnt(test); return 0; } continued

CPT: Strings/108 int words_cnt(char *s) /* count the number of words */ { int cnt = 0; while (*s != '\0') { while (isspace(*s)) /* skip spaces */ s++; if (*s != '\0') { /* found word */ cnt++; while (!isspace(*s) && *s != '\0') s++; /* skip word */ } } return cnt; }

CPT: Strings/ String Conversion Functions Sec. 8.4 #include #include int main() { double d; d = atof("99.0"); printf("\"99.0\" converted to double is %.3f\n", d); return 0; }

CPT: Strings/1010 Output: "99.0" converted to double is Function prototype in stdlib.h : double atof(const char *nptr);

CPT: Strings/ Input and Output of Strings #include int main() { char word[7]; scanf("%s", word); printf("%s", word); puts(word); return 0; }

CPT: Strings/1012 Reversing (Again)Fig #include #include void reverse(char *); int main() { char sentence[80]; printf("Enter a line of text:\n"); gets(sentence); printf("\nLine backwards is:\n"); reverse(sentence); return 0; } continued

CPT: Strings/1013 void reverse(char *s) /* print s in reverse order */ { int i; for(i=strlen(s)-1; i >= 0; i--) putchar(*(s+i)); /* same as putchar(s[i]); */ }

CPT: Strings/1014 Output Enter a line of text: hello Line backwards is: olleh..... ‘h’‘e’‘l’ ‘o’‘\0’ s

CPT: Strings/ String Manipulation 6.1.Copying a String: strcpy() 6.2.Concatenating Strings: strcat() 6.3.Comparing Strings: strcmp() 6.4.Searching a String: strchr() 6.5.String Length: strlen()

CPT: Strings/ Copying a String: strcpy() Fig #include #include int main() { char x[] = "Happy Birthday to You"; char y[25]; printf("String in x is: %s\n String in y is: %s\n", x, strcpy(y, x)); return 0; }

CPT: Strings/1017 Output: String in x is Happy Birthday to You String in y is Happy Birthday to You Function prototype in string.h : char *strcpy(char *s1, const char *s2);

CPT: Strings/1018 Coding strcpy() /* array version */ char *strcpy(char *s, const char *t) { int i; i = 0; while ((s[i] = t[i]) != '\0') i++; return s; } continued

CPT: Strings/1019 /* pointer version no. 1 */ char *strcpy(char *s, const char *t) { char *temp = s; while ((*s = *t) != '\0') { s++; t++; } return temp; } continued

CPT: Strings/1020 /* pointer version no. 2 */ char *strcpy(char *s, const char *t) { char *temp = s; while ((*s++ = *t++) != '\0') ; return temp; } continued

CPT: Strings/1021 /* pointer version no. 3 */ char *strcpy(char *s, const char *t) { char *temp = s; while (*s++ = *t++) ; return temp; }

CPT: Strings/ Concatenating Strings: strcat() Fig #include #include int main() { char s1[20] = "Happy "; char s2[] = "New Year "; printf("s1 = %s\ns2 = %s\n", s1, s2); printf("strcat(s1, s2) = %s\n", strcat(s1, s2)); printf("s1 = %s\n", s1); return 0; }

CPT: Strings/1023 Output: s1 = Happy s2 = New Year strcat(s1, s2) = Happy New Year s1 = Happy New Year Function prototype in string.h : char *strcat(char *s1, const char *s2);

CPT: Strings/1024 Coding strcat() char *strcat(char *s1, const char *s2) { char *p = s1; while(*p) /* while (*p != '\0') */ p++; while (*p++ = *s2++) ; return s1; }

CPT: Strings/1025 In picture form: ‘H’‘o’‘\0’ ‘b’‘o’‘\0’ p s1 s2

CPT: Strings/ Comparing Strings: strcmp() int strcmp(const char *s1, const char *s2); l Compares the string s1 to the string s2 Returns 0 if s1 > s2

CPT: Strings/1027 Using strcmp() Fig #include #include int main() { char *s1 = "Happy New Year"; char *s2 = "Happy New Year"; char *s3 = "Happy Holidays"; printf("%s%s\n %s%s\n %s%s\n %s%2d\n %s%2d\n %s%2d\n", "s1 = ", s1, "s2 = ", s2, "s3 = ", s3, "strcmp(s1, s2) = ", strcmp(s1, s2), "strcmp(s1, s3) = ", strcmp(s1, s3), "strcmp(s3, s1) = ", strcmp(s3, s1)); return 0; }

CPT: Strings/1028 Output: s1 = Happy New Year s2 = Happy New Year s3 = Happy Holidays strcmp(s1, s2) = 0 strcmp(s1, s3) = 6 strcmp(s3, s1) = -6

CPT: Strings/1029 Coding strcmp() The array version: int strcmp(const char *s, const char *t) { int i; for (i=0; s[i] == t[i]; i++) if (s[i] == '\0') return 0; return s[i] - t[i]; } continued

CPT: Strings/1030 The pointer version: int strcmp(const char *s, const char *t) { for ( ; *s == *t; s++, t++) if (*s == '\0') return 0; return *s - *t; }

CPT: Strings/ Searching a String: strchr() Sec. 8.8 #include #include int main() { char *rest, *sh = "Hello"; char c = 'l'; rest = strchr(sh, c); printf("%s", rest); /* gives llo */ return 0; }

CPT: Strings/1032 Function prototype in string.h : char *strchr(const char *s, int c); ‘h’‘e’‘l’ ‘o’‘\0’ sh rest

CPT: Strings/ String Length: strlen() Fig Function prototype in string.h : size_t strlen(const char *s); size_t is equivalent to the type unsigned int (see string.h ).

CPT: Strings/1034 Coding strlen() size_t strlen(const char *s) { int n; for (n = 0; *s != '\0'; s++) n++; return (size_t) n; }