INC 161 , CPE 100 Computer Programming

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() 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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
1. An array is a collection of a fixed number of components wherein all of the components are of the same type Example: Suppose that there is a list of.
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
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
CHAPTER 8 CHARACTER AND STRINGS
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
Characters and Strings File Processing Exercise C Programming:Part 3.
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.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
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. 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.
Characters and Strings
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.
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,
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.
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.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
C Characters and Strings
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
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
CSE 303 Lecture 14 Strings in C
C Programming:Part 3 Characters and Strings File Processing Exercise.
Strings A string is a sequence of characters treated as a group
Arrays in C.
Chapter 8 - Characters and Strings
Lecture 8b: Strings BJ Furman 15OCT2012.
C Stuff CS 2308.
Strings.
INC 161 , CPE 100 Computer Programming
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.
EECE.2160 ECE Application Programming
Chapter 8 Character Arrays and Strings
CPS120: Introduction to Computer Science
C++ Programming Lecture 20 Strings
Lecture 19: Working with strings
Strings #include <stdio.h>
Characters and Strings Functions
C Characters and Strings
Introduction to Problem Solving and Programming
Presentation transcript:

INC 161 , CPE 100 Computer Programming Lecture 7 String

Characters Using char to declare a character variable. Characters such as letters and punctuation are stored as integers according to a certain numerical code, such as ASCII code that ranges from 0 to 127 and only requires 7 bits to represent it. Character constant is an integral value represented as a character in single quotes. ’a’ represents the integer value of a. If ASCII code is used to represent characters, the ASCII value for ’a’ is 97. Using char to declare a character variable. char c = ’a’; // the same as char c = 97;

Character Input and Output Functions in <stdio.h> int getchar(void); Reads the next character from the standard input. int putchar(int c); Print the character stored in c int scanf(const char *, …); with conversion specifier c. char c; scanf(“%c”, &c); int printf(char *, …); char c=’a’; printf(“%c”, c);

Strings Strings are series of characters treated as a single unit Can include letters, digits, and certain special characters (*, /, $) String literal (string constant) - a sequence of multi-byte characters enclosed in double quotes "Hello" Declare a string as a character array or a variable of type char * char str1[7] = {’S’, ’t’, ’r’, ’i’, ’n’, ’g’, ’\0’}; char str2[ ] = {’S’, ’t’, ’r’, ’i’, ’n’, ’g’, ’\0’}; char str3[ ] = “String”; char *strPtr = “String”; String must have an end character 0 or ‘\0’ or NULL

char a[10] = “Hello” ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’ Don’t care what it stores End Character

String Input and Output Functions in <stdio.h>

Example 2 : sprintf, sscanf #include <stdio.h> main() { char str[40]; int a = 123, b = 0; printf("Please input a string.\n"); scanf(“%s”,str); printf(“The input is: ”); printf(“%s”,str); sprintf(str,“The value of a %d\n”,a); sscanf(str,“The value of a %d\n”,&b); printf(“After sscanf b is %d\n”,b); }

String Manipulation Header file string.h has functions to Determining string length strlen Copying strings strcpy Appending strings strcat Comparing strings strcmp Searching strings Tokenizing strings

Copying strings Function Description char *strcpy(char *s1, const char *s2) Copies string s2 into s1. The value of s1 is returned. char *strncpy(char *s1, const char *s2, size_t n) Copies at most n characters of string s2 into s1. If there are fewer than n characters in s2 before the null terminating character, then null characters are added into s1 as padding until the specified number has been written.The value of s1 is returned.

Comparing strings Function Description int strcmp(const char *s1, const char *s2) Compare string s1 to s2. int strncmp(const char *s1, const char *s2, size_t n) Compare up to n characters of string s1 to s2.

Example 3: strcmp, strcpy, strcat #include <stdio.h> #include <string.h> main() { char s1[10]="ABCD", s2[10]="ABCD", s3[10]="abcd"; printf("strcmp(s1, s2) = %d\n", strcmp(s1, s2)); printf("strcmp(s1, s3) = %d\n", strcmp(s1, s3)); printf("strcmp(s3, s1) = %d\n", strcmp(s3, s1)); strcpy(s1, s3); printf("s1 = %s\n", s1); strcpy(s1, "1234"); strcat(s1, "Hello"); printf("strlen(s1) = %d\n", strlen(s1)); }

Output: strcmp(s1, s3) = -1 strcmp(s3, s1) = 1 s1 = abcd s1 = 1234 s1 = 1234Hello strlen(s1) = 9