C Strings - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

C Strings

Description:

As long as they are part of the character coding scheme (we ... Don't use the ampersand (&), the name of the string is already an address. scanf('%s', yourName) ... – PowerPoint PPT presentation

Number of Views:1544
Avg rating:3.0/5.0
Slides: 27
Provided by: scie263
Category:

less

Transcript and Presenter's Notes

Title: C Strings


1
Chapter 11
  • C Strings

2
What is a String?
  • A string is a collection of characters
  • Any characters
  • As long as they are part of the character coding
    scheme (we use ASCII)
  • The computer itself attaches no significance to
    the characters
  • It thinks characters are numbers
  • Only our instructions give meaning to them

3
String Values
  • Strings in C are just arrays of char values
  • Memory is allocated for each character when we
    use a string
  • Each character is 8 bits (a char data type)
  • The string "Computer" is stored as ?????????
  • C uses the null character '\0' to end the string

4
String Variables
  • A String variable is an array of char variables
  • You can allocate the array if you know (ahead of
    time) how long the string might be
  • char yourName10
  • The name of the string variable is yourName
  • Remember to leave space for the null at the end
  • You can initialize strings

char yourName10 'R','o','b','e','r','t'
char yourName10 "Robert"
  • Values are given to the first six elements, and
    the others are set to zero (which is the null
    character)

5
String Assignments
  • Once a String variable is declared, you can
    assign values to it one character at a time only
  • char yourName10
  • yourName0 'R'
  • yourName1 'o'
  • yourName2 'b'
  • yourName3 'e'
  • yourName4 'r'
  • yourName5 't'
  • yourName6 '\0'
  • Once a String variable is declared, you could
    not, however, do this
  • yourName "Robert"

6
String Input
  • Use the s conversion code in scanf()
  • Dont use the ampersand (), the name of the
    string is already an address
  • scanf("s", yourName)
  • This will take characters from stdin and store
    them starting at the address name
  • Starts at the current position and goes until it
    finds some white space
  • Also adds the null to the end automatically

7
String Output
  • Just like scanf(), use s in printf()
  • char yourName10 "Robert"
  • printf("s", yourName)
  • Prints all the characters till it finds a null
    character

8
Example of string I/O
include ltstdio.hgt void main(void) char
yourName10 float testScore
printf("Enter your name and test score ")
scanf("s f", yourName, testScore)
printf("The test score of s is .2f\n",
yourName, testScore)
Output Enter your name and test score John
87.5 The test score of John is 87.50
9
Width and precision with string output
  • Width and precision can be used with string
    output
  • Width is the minimum characters to print
  • Precision is the maximum characters to print
  • Example
  • char yourName10 "Robert"
  • printf("Name10s\n", yourName)
  • printf("Name.3s\n", yourName)
  • Output
  • Name Robert
  • NameRob

10
Reading an Entire Line
  • The s in scanf() only reads to the first white
    space and you cant read more than one word
  • However, gets() function can be used to read more
    than one word till you get to a new line
  • include ltstdio.hgt
  • main()
  • char yourName20
  • printf("Enter your name ")
  • gets(yourName)
  • puts(yourName)
  • The puts() can be used for string output

11
String functions
  • Found in string.h
  • There are functions for
  • Finding the length of a string
  • Copying one string to another
  • Concatenating strings
  • Comparing two strings

12
Finding a Strings Length
  • Find the length of a string (up to null)
  • size_t strlen(char string)
  • The return value is the number of characters in
    the string (not counting the null)
  • The size_t data type is implementation dependent,
    but is typically an unsigned int

13
Example
  • include ltstdio.hgt
  • include ltstring.hgt
  • void main(void)
  • char yourName10 "Roberts"
  • int count
  • count strlen(yourName)
  • printf("Your name consists of i
    characters.\n",count)
  • Output
  • Your name consists of 7 characters.

14
Copying Strings
  • Two functions to copy one string into another
  • Copy the whole string
  • char strcpy(char dest, char src)
  • Copies the string src (including null) to the
    string dest
  • Copy part of the string
  • char strncpy(char dest, char src, size_t max)
  • Copies the first max number of characters
  • Can result in the loss of the null character

15
Example
include ltstdio.hgt include ltstring.hgt void
main(void) char name110 "Robson",
name210 "Mark" strcpy(name1,
name2) printf("s\n", name1)
include ltstdio.hgt include ltstring.hgt void
main(void) char name110 "Robson",
name210 "Williams" strncpy(name1,
name2, 3) printf("s\n", name1)
Output Wilson
Output Mark
16
Concatenating Strings
  • Append one string at the end of another
  • Append a whole string
  • char strcat(char string, char add)
  • Appends the string add to the end of string
  • Append part of a string
  • char strncat(char string, char add, size_t
    max)
  • Adds the first max number of characters to the
    end of string
  • Does not have the problem of losing the null

17
Example of string Concatenation
include ltstring.hgt include ltstring.hgt void
main(void) char name120 "Computer",
name220 "Science" strncat(name1, name2,
2) printf("s\n", name1)
  • include ltstring.hgt
  • include ltstring.hgt
  • void main(void)
  • char name120 "Computer",
  • name220 "Science"
  • strcat(name1, name2)
  • printf("s\n", name1)

Output ComputerSc
Output ComputerScience
18
Comparing Two Strings
  • Which is greater? "Mark" or "Anderson"
  • Strings are compared one character at a time,
    using the ASCII values for each character
  • int strcmp(char string1, char string2)
  • int strncmp(char string1, char string2, size_t
    max)
  • Returns
  • Positive if string1 is greater (lexically)
  • Negative if string1 is less (lexically)
  • Zero if they are equal (lexically)
  • The max in strncmp() is the maximum number of
    characters that will be compared

19
Example of string Comparison
  • include ltstdio.hgt
  • include ltstring.hgt
  • void main(void)
  • char name112 "Mark", name212
    "Anderson"
  • int count
  • count strcmp(name1, name2)
  • printf("Comparing s and s i\n", name1,
    name2, count)
  • count strcmp(name2, name1)
  • printf("Comparing s and s i\n", name2,
    name1, count)
  • count strcmp(name1, name1)
  • printf("Comparing s and s i\n", name1,
    name1, count)

Output Comparing Mark and Anderson 1 Comparing
Anderson and Mark -1 Comparing Mark and Mark 0
20
Arrays of Strings
  • A string is a 1-D array of characters, so a 2-D
    array of characters would be an array of strings
  • char names410 "Robert","Adam",
    "Charles","Benjamin"
  • This will declare an array of 4 strings with each
    string having a maximum of 10 characters
  • names0 names1 names2 names3

21
Example
  • / This program declares an array of 5 strings
  • and gets the values from the user /
  • include ltstdio.hgt
  • void main(void)
  • char names510
  • int i
  • for ( i0 ilt5 i)
  • scanf("s", namesi)
  • for ( i0 ilt5 i)
  • printf("s ", namesi)
  • printf("\n")

22
Converting numbers to strings
  • Can be done with sprintf() function
    ltstdio.hgt
  • The printf() function converts values to
    characters and writes them to the output buffer
  • The sprintf() function converts values to
    characters and then writes them to a string
    stored in memory

include ltstdio.hgt void main(void) char
paycheck40 double hours 36.7, rate
12.36 sprintf(paycheck, ".2f", hours
rate) printf(Pay is s.\n, paycheck)
Output Pay is 453.61.
23
Converting strings to numbers
  • Can be done with sscanf() function ltstdio.hgt
  • The scanf() function converts characters from the
    input buffer and writes them to variables stored
    in memory
  • The sscanf() function converts characters from a
    string and writes them to variables stored in
    memory

Output 453.630000
include ltstdio.hgt void main(void)
char paycheck "Pay is 453.63." float
pay sscanf(paycheck, "Pay is f.", pay)
printf("f", pay)
24
Converting strings to numbers (Cont..)
  • Three other functions defined in ltstdlib.hgt
  • String to double
  • double atof(char string)
  • String to int
  • int atoi(char string)
  • String to long
  • long atol(char string)
  • Conversion begins at the first character and
    skips leading whitespaces
  • Conversion ends at the first inappropriate
    character or the end of the string (null)

25
Example
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • void main(void)
  • char string "123.456 is a number"
  • int x
  • double y
  • x atoi(string)
  • y atof(string)
  • printf("The string s\n", string)
  • printf("The integer i, the double f\n",
    x, y)

Output The string 123.456 is a number The
integer 123, the double 123.456000
26
Character functions
  • Some character classification and character
    conversion functions are defined in ltctype.hgt
  • int isalpha(int character)
  • int isdigit(int character)
  • int islower(int character)
  • int isupper(int character)
  • int tolower(int character)
  • int toupper(int character)
  • See details in F-4 p. 814
Write a Comment
User Comments (0)
About PowerShow.com