Strings What is a string? It is an array of characters terminated with

Slides:



Advertisements
Similar presentations
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Advertisements

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.
Chapter Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
Characters and Strings 4 Character constants (single quotes): –‘a’, ‘A’, ‘0’, ‘1’, ‘\n’, ‘\0’, … 4 String constants (double quotes): –“Mary had a little.
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.
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.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
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’};
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.
Slides from Shane Griffith (TA and guest instructor in Fall 2008) CprE 185: Intro to Problem Solving.
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.
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.
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.
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.
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.
1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions.
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
Computer Organization and Design Pointers, Arrays and Strings in C
C Characters and Strings
Strings CSCI 112: Programming in C.
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
Characters and Strings
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
Strings A string is a sequence of characters treated as a group
Arrays in C.
Programming Languages and Paradigms
CS111 Computer Programming
Object Oriented Programming COP3330 / CGS5409
Strings.
Engr 0012 (04-1) LecNotes
INC 161 , CPE 100 Computer Programming
C Characters and Strings – Review Lab assignments
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.
Strings.
CprE 185: Intro to Problem Solving (using C)
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
EECE.2160 ECE Application Programming
Chapter 16 Pointers and Arrays
Chapter 8 Character Arrays and Strings
CPS120: Introduction to Computer Science
C Strings Prabhat Kumar Padhy
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Strings in C Array of characters is called a string.
Strings #include <stdio.h>
Characters and Strings Functions
Strings Adapted from Dr. Mary Eberlein, UT Austin.
C Characters and Strings
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
cout << str1;  pear
Introduction to Problem Solving and Programming
Presentation transcript:

Strings What is a string? It is an array of characters terminated with an extra character '\0' NULL character e.g. char name[5] = {'B','i','l','l','\0'}; equivalent to char name[5] = "Bill"; double quotes Constant string C tacks on '\0' automatically at the end of the string or char name[] = "Bill"; 142 R -1

In the memory: name[0] is 'B' name[4] is '\0' tells the computer where the string ends 142 R -2

Using strings do not forget Initialization like arrays of numbers char name[5] = {'B','i','l','l','\0'}; or char name[5]; name[0]='B'; name[1]='i'; name[2]='l'; name[3]='l'; name[4]='\0'; or char name[5] = "Bill"; particular to string arrays or char name[] = "Bill"; Cannot do char name[5]; /* OK */ name = "Bill"; /* NOT OK */ 142 R -3

Input/Output of strings with scanf char name[5]; scanf("%s",name); placeholder for strings no & (why?) no length check (if name is not big enough, you will get run time problems) skips initial white spaces insert '\0' at the next white space input: CSC142 is a lot of fun name contains 'C', 'S', 'C','1','4','2','\0' with printf printf("%s",name); Specific for strings. There is no placeholder to input or output arrays of numbers. 142 R -4

Operations on strings As for arrays of numbers, there are no operators in C that work directly on strings. Be Careful! char name[5] = "Bill"; /*OK*/ char name2[5]; /*OK*/ name2 = name; /*NOT OK*/ Instead for(i=0; i<5; i++) name2[i] = name[i]; Or more clever! for(i=0; name[i]!='\0'; i++) name2[i] = name[i]; name2[i] = '\0'; don’t need the length of the string But, we don't need to write our own functions. Check the standard string library At the beginning of the program: #include <string.h> 142 R -5

Some string functions Copy string s2 into string s1 strcpy(s1,s2); In C, The C code for strcpy is similar to void strcpy(char s1[], char s2[]) { int i; for(i=0; s2[i]!='\0', i++) s1[i] = s2[i]; s1[i] = '\0'; } Careful, C doesn't check if s2 fits in s1 142 R -6

#include <string.h> ... char line1[5]; Don't do: #include <string.h> ... char line1[5]; char line2[]="The Universe" strcpy(line1,line2); Trouble ahead! In the memory T h e U n i v r s '\0' space reserved for line1 just erased some (important?) data 142 R -7

When using strcpy, check the length of the strings How? Use the function strlen char s[]={'H','e','l','l','o','\0'}; strlen(s) is 5 5 because '\0' is not counted When using strcpy, write char s2[] = "Once upon a time"; char s1[SIZE]; if (strlen(s2)<SIZE-1) strcpy(s1,s2); else printf("String is too short!"); 142 R 8

Could also copy only the part of s2 that fits in s1 strncpy The C code for strncpy is similar to void strncpy(char dest[], char source[], int destlen) { int i; for(i=0; i<destlen; i++) if (i<=strlen(source)) dest[i] = source[i]; else /* end of dest is padded with nulls */ dest[i] = '\0' } copy at most destlen characters from source to dest Note: '\0' might not be added to dest When? destlen <= strlen(source) 142 R 9

String concatenation strcat strcat(s1,s2) concatenates s2 to the end of s1 e.g. char s1[14]="New York";/*8 characters*/ char s2[]=" city"; /*5 characters*/ strcat(s1,s2); printf("%s",s1); printf prints "New York city" 142 R 10

String comparison How to compare strings? use the (extended) alphabetical order include characters like [, ], @,etc... "a" < "d" "ant" < "antelope" "&123" < "@0" But, cannot use <, >, == for strings in C Use the function strcmp strcmp(str1,str2) is not C operators <0 if str1 < str2 =0 if str1 = str2 >0 if str1 > str2 142 R 11

Pitfalls of strings char str1[] = "Hello"; char str2[] = "Bonjour"; Can we write? str1 = str2 /* ERROR */ if (str1 == str2)... OK, but compare pointers (not the string contents!) if (str1 > str2) OK, but compare pointers (not the string contents!) 142 R 12

Arrays of strings Same as multidimensional arrays char day[7][10]={"Monday","Tuesday", "Wednesday","Thursday","Friday", "Saturday","Sunday"}; 10 for Wednesday: 9 characters + 1 (for '\0') Examples printf("CSC142 lab is on %s",day[2]); or char name[NUM_NAMES][MAX_NAME+1]; int age[NUM_NAMES]; int i; for(i=0; i<NUM_NAMES; i++) { scanf("%s%d",name[i],&age[i]); printf("%s %d",name[i],age[i]); } no & 142 R 13

Check other functions in <string.h> strncat, strncmp strtod, strtol, strtoul (to convert a string to a number) See also the related function library for characters: <ctype.h> 142 R 14

Summary string: Null terminated array of chars No string assignment or comparison in C Use built in functions in <string.h> assignment: strcpy, strncpy comparison: strcmp, strncmp length: strlen printf and scanf with %s Pitfall: overrunning the allowed length of a string 142 R 15