Top Banner
STRING Dong-Chul Kim BioMeCIS CSE @ UTA 02/07/22 1
16

STRING Dong-Chul Kim BioMeCIS CSE @ UTA 10/7/2015 1.

Dec 30, 2015

Download

Documents

Toby Byrd
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: STRING Dong-Chul Kim BioMeCIS CSE @ UTA 10/7/2015 1.

STRING

Dong-Chul KimBioMeCISCSE @ UTA

04/19/23 1

Page 2: STRING Dong-Chul Kim BioMeCIS CSE @ UTA 10/7/2015 1.

• When declaring a 2D array, the number of columns must be stated.

• Example• int array1D[] = {1, 2, 3};• int array2D[][3] = { {4, 5, 6},{7, 8, 9} };

1D Array and 2D Array

Page 3: STRING Dong-Chul Kim BioMeCIS CSE @ UTA 10/7/2015 1.

• A char is a one byte integer type typically used for storing characters.

• Example:• char oneLetter = ’D’;• We enclose the character in single quotes, not double

quotes.

The char Data Type

Page 4: STRING Dong-Chul Kim BioMeCIS CSE @ UTA 10/7/2015 1.

• Plain text is often stored in a format called ASCII (American Standard Code for Information Interchange). There are 128 characters in ASCII, which includes the standard alphanumeric characters as well as some non-printable characters (e.g., tab, newline, and so forth).

ASCII

Page 5: STRING Dong-Chul Kim BioMeCIS CSE @ UTA 10/7/2015 1.

• A char variable can be initialized using the character or the decimal value of the character.• #include <stdio.h>• int main(void)• {• char letter_a = ’a’; /* note the use of single quotes */• char decimal_a = 97; /* 97 is the decimal value• for ’a’ in ASCII */• /* note the %c to print characters */• printf("letter_a is %d and %c\n", letter_a, letter_a);• printf("decimal_a is %d and %c\n", decimal_a, decimal_a);• }

• Output• letter_a is 97 and a• decimal_a is 97 and a

char Example

Page 6: STRING Dong-Chul Kim BioMeCIS CSE @ UTA 10/7/2015 1.

• If we check an ASCII chart, we see that the letters A–Z have ASCII values of 65–90 while a–z have values of 97–122. Because the letters are stored as numbers, we can perform numeric operations on them.• #include <stdio.h>• int main(void)• {• char uppercase = ’A’; /* 65 in ASCII */• char lowercase;• lowercase = uppercase + 32;• printf("Adding 32 to %c gives us %c\n", uppercase, lowercase);• }

• prints• Adding 32 to A gives us a

char cont.

Page 7: STRING Dong-Chul Kim BioMeCIS CSE @ UTA 10/7/2015 1.

• A character string is a series of one or more characters.• Example• “I am at UTA”

• The double quotation marks are not part of the string. They just inform the compiler they enclose a string, just as single quotation marks identify a character.

String

Page 8: STRING Dong-Chul Kim BioMeCIS CSE @ UTA 10/7/2015 1.

• C does not have a string type. Characters in a string are stored in adjacent memory cells, one character per cell, and an array consists of adjacent memory locations, so placing a string in an array is quite natural.

• We use an array of chars for storing a string.• We can declare this just like we did with other types.• Example• char letters[12] = “I am at UTA”;

Array of char

I a m a t U T A \0

Page 9: STRING Dong-Chul Kim BioMeCIS CSE @ UTA 10/7/2015 1.

• \0 is the null character which is used by C for marking the end of a string.

• The null character is not the digit zero; it is the nonprinting character.

• Its ASCII code value is 0.• The presence of the null character means the array must

have at least one more cell than the number of the characters to be stored• char letters[as long as > 11] = “I am at UTA”;

I a m a t U T A \0

(1) What if the input string has a length more than the size of the array ?

(2) Is there any application to use this point?

Page 10: STRING Dong-Chul Kim BioMeCIS CSE @ UTA 10/7/2015 1.

• Even though we define a char array much larger than a string, the null character will tell the compiler where is the end of the string. This is useful when we call printf function.

Page 11: STRING Dong-Chul Kim BioMeCIS CSE @ UTA 10/7/2015 1.

• #include <stdio.h>• int main(void)• {• char name[40];// you can’t use name[], the size has to

be fixed.• printf("What's your name?\n");• scanf("%s", name);• printf("Hello, %s.\n", name);• return 0;• } • %s is the format specifier for a string.• The name of a char array represents an address in computer

memory, therefore, we don’t need “&” when we call scanf function.

Scanf a string and print it out

Page 12: STRING Dong-Chul Kim BioMeCIS CSE @ UTA 10/7/2015 1.

• #include <stdio.h> • int main(void) • { • char text[] = "I am at UTA"; • int i = 0; • i = 0; • while(text[i] != '\0') /*use '\0' as terminal*/• { • printf("%c %3d\n", text[i], text[i]); • i++; • } • }

Access characters in a string

Page 13: STRING Dong-Chul Kim BioMeCIS CSE @ UTA 10/7/2015 1.

• #include <stdio.h>• #include <string.h>

• int main(void)• {• char text[100];• int size, i;

• printf("Enter a string: ");• scanf("%s", text);

• printf("you entered %s\n", text);

• size = strlen( text );• printf("The number of characters in the input string is %d.\n", size);• return 0;• }• The strlen function returns the number of characters in a string, excluding ‘\0’.• strlen(the name of a char array)• Call strlen function needs “#include <string.h>”

String length

Page 14: STRING Dong-Chul Kim BioMeCIS CSE @ UTA 10/7/2015 1.

• If your input is • ABCD edEc asdetE• Just calling scanf() once will only store ABCD in the char

array as a string. The remaining of the input will be ignored. The space is used to separate strings in a sentence.

Note

Page 15: STRING Dong-Chul Kim BioMeCIS CSE @ UTA 10/7/2015 1.

• Now if we access all characters of a string in a loop, we have two approaches to terminate the loop:

• (1) the length of the string ---- strlen()• (2) ‘\0’

Page 16: STRING Dong-Chul Kim BioMeCIS CSE @ UTA 10/7/2015 1.

• #include <stdio.h>• #include <string.h>

• int main(void)• {• char text[100];• int size, i;

• printf("Enter a string: ");• scanf("%s", text);

• printf("you entered %s\n", text);

• size = strlen( text );• for(i = size - 1; i >= 0; i--)• printf("%c", text[i]);

• printf("\n");• }

Reverse a string