+ All Categories
Home > Documents > STRING Dong-Chul Kim BioMeCIS CSE @ UTA 10/7/2015 1.

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

Date post: 30-Dec-2015
Category:
Upload: toby-byrd
View: 224 times
Download: 1 times
Share this document with a friend
16
STRING Dong-Chul Kim BioMeCIS CSE @ UTA 02/07/22 1
Transcript

STRING

Dong-Chul KimBioMeCISCSE @ UTA

04/19/23 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

• 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

• 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

• 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

• 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.

• 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

• 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

• \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?

• 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.

• #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

• #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

• #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

• 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

• 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’

• #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


Recommended