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.

Slides:



Advertisements
Similar presentations
Chapter 9 Strings. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-2 Learning Objectives An Array Type for Strings C-Strings Character.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
 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.
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.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
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.
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.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
Strings String - a string is a series of characters treated as a unit. A C string is a variable-length array of characters that is delimited by the null.
C-strings Array with base type char One character per indexed variable
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
1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Array with base type char One character per indexed variable One extra character: '\0' Called ‘null character’ Delimiter of the string To declare a string,
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.
Strings in C are my friend, and they can be yours, too. An inspiring and epic tale by Erik Speyer, John Sullivan, and Dane Bennington.
9-1 Learning Objectives  An Array Type for Strings  C-Strings.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Chapter 9 Strings. Learning Objectives An Array Type for Strings – C-Strings Character Manipulation Tools – Character I/O – get, put member functions.
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.
An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character.
Copyright © 2002 Pearson Education, Inc. Slide 1.
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.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
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.
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
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.
Characters and Strings
C Characters and Strings
Fundamentals of Characters and Strings
Chapter 9 Strings Copyright © 2016 Pearson, Inc. All rights reserved.
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
CSCI206 - Computer Organization & Programming
CS111 Computer Programming
Object Oriented Programming COP3330 / CGS5409
C Stuff CS 2308.
Strings.
Pointers and Pointer-Based Strings
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
String in C++.
File Input and Output.
String What it is Why it’s useful
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Strings What is a string? It is an array of characters terminated with
CPS120: Introduction to Computer Science
Line at a time I/O with fgets() and fputs()
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
String manipulation string.h library
C++ Programming Lecture 20 Strings
Strings #include <stdio.h>
Introduction to Problem Solving and Programming
Presentation transcript:

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 chunk of data. C uses a delimiter (‘\0’, the null character) to designate the end of a string; hence, A C string is a variable-length null-terminated character array

C-strings Array with base type char One character per indexed variable One extra character: '\0' Called ‘null character’ Delimiter of the string To declare a string, declare a char array, e.g. char s[10]; Declares a c-string variable to hold up to 9 characters + one null character

String Variable Typically ‘partially-filled’ array Declare large enough to hold max-size string, including the null character. A standard array: char s[10]; If s contains the string “Hi Mom!”, it will be stored as:

String Initialization Can initialize string: char myMessage[20] = "C is fun!"; Need not fill the entire array Initialization places '\0' at the end

String Initialization Can omit array-size: char shortString[] = "abc"; Automatically makes size one more than the length of the quoted string NOT the same as: char shortString[] = {'a', 'b', 'c'}; IS the same as: char shortString[] = {'a', 'b', 'c', '\0'};

Strings and the assignment operator Recall that the name of a string is a pointer constant; it points to the first character of the string. Given char str1[6] = “Hello”; char str2[6]; str2 = str1; // compiler error

Accessing elements of a string A string IS an array; therefore, Can access indexed variables of: char str[7] = "Hello"; str[0] is 'H' str[1] is 'e' str[2] is 'l' str[3] is 'l' str[4] is 'o'; str[5] is ‘\0’ str[6] is unknown

String Index Manipulation Can manipulate indexed variables char happyString[7] = "DoBeDo"; happyString[6] = 'Z'; Be careful! Here, ‘\0’ (null) was overwritten by a ‘Z’! If null is overwritten, string no longer ‘acts’ like a string! Unpredictable results!

String Input - fgets char *fgets (char * strPtr, int size, FILE *fp) Inputs characters from the specified file pointer through \n or until specifed size is reached Puts newline (\n) in the string if size not reached!!! Appends \0 at the end of the string If successful, returns the string Example for stdin: const int MAX_LINE = 100; char lineIn[MAX_LINE+2]; // allow for \n and \0 fgets(lineIn, MAX_LINE, stdin);

String Output - fputs int fputs (const char *strPtr, FILE *fp) Takes a null-terminated string from memory and writes it to the specified file pointer Drops \0 Programmer's responsibility: Make sure the newline is present at the appropriate place(s) Example for stdout: char lineout[100] = "Hello!\n"; fputs(lineout, stdout);

Use of pointers in processing C-strings. Recall that a "C-string" is an array of characters whose logical end is denoted by a zero valued byte. The C standard library has a number of functions designed to work with C strings. The strtol() function is one of them. You can see most of them on a Linux system via the command: man string stpcpy, strcasecmp, strcat, strchr, strcmp, strcoll, strcpy, strcspn, strdup, strfry, strlen, strncat, strncmp, strncpy, strncasecmp, strpbrk, strrchr, strsep, strspn, strstr, strtok, strxfrm, index, rindex - string operations

String manipulation functions Must #include <string.h>

String manipulation functions (cont'd)

= with strings strings are not like other variables Cannot assign or compare: char aString[10]; aString = "Hello"; // ILLEGAL Can ONLY use ‘=‘ at declaration of string! Must use library function for assignment: strcpy(astring, "Hello");

= with strings strcpy(aString, "Hello") Sets value of aString equal to “Hello” NO checks for size! Up to programmer to verify size of aString is large enough to hold all characters of the source, just like other arrays!

== with strings Cannot use operator == char aString[10] = "Hello"; char anotherString[10] = "Goodbye"; if(aString == another String) // NOT allowed Must use strcmp library function to compare: if(strcmp(aString, another String)) printf("Strings NOT the same.\n"); else printf("Strings are the same.\n");

string Functions: strlen() String length Often useful to know the length of a string: char myString[10] = "dobedo"; printf("length = %d\n", strlen(myString)) Returns number of characters Not including null character Result here: 6

string Functions: strcat() String concatenate char stringVar[20] = "The rain"; strcat(stringVar, "in Spain"); Note result: stringVar now contains The rainin Spain Be careful! Incorporate spaces as needed!

string Functions In the following example, we will see how strcpy might be implemented. Its prototype is shown below. char *strcpy(char *destination, const char *source);

An implementation of strcpy The name zstrcpy is used to avoid potential nastygrams and name conflicts with the “real” strcpy. /* zstrcpy.c */ #include <stdio.h> #include <stdlib.h> /* Prototype: char *zstrcpy(char *dst, char *src); where "dst" is a pointer to the location that the string is to be copied to, "src" is a pointer to the location that the string is copied from. */

An Implementation of strcpy char *zstrcpy(char *dst, char *src) { /* dst - pointer to destination string */ /* src - pointer to source string */ char *dststart = dst; /* Remember start of destination string */ /* Copy characters from source to destination */ while (*src != '\0') { *dst = *src; dst++; src++; } *dst = *src; /* Copy null character */ return(dststart); } /* End zstrcpy */

An alternative implementation It is possible to shrink and obfuscate the code in an attempt to demonstrate ones C language machismo. This approach produces code that is difficult to read, painful to maintain, but may (or may not) produce a trivial improvement in performance . When tempted, just say no! /* zstrcpy.c */ #include <stdio.h> #include <stdlib.h> /* Prototype: Short, less readable version. char *zstrcpy(char *dst, char *src); where "dst" is a pointer to the location that the string is to be copied to, "src" is a pointer to the location that the string is copied from. */

An alternative implementation (cont'd) char *zstrcpy(char *dst, char *src) { /* dst - pointer to destination string */ /* src - pointer to source string */ char *dststart = dst; /* Copy characters from source to destination */ while (*src++ = *dst++) != '\0') { } return(dststart); } /* End zstrcpy */