Chapter 11 - Strings - Character Arrays

Ace your homework & exams now with Quizwiz!

strcmp - Definition

Compares two strings. strcmp(str1, str2); Comes out true or false based off the memory locations.It is not the sum of the differences that is used when calculating the difference (comparison) it is the first difference in order from lowest to highest in the array.

Declaration

Declared as a character array. char str[1] = "Good Day";

strlen - Definition

Returns the number of characters in the string. This function does not count the terminal delimiter character. strlen(str1);

Delimiter - Definition

Tells us where the string ends. '\0', represents the end of the data within a single character array.

The %s Placeholder - Definition

The %s placeholder represents a string and is used in both input and output functions. scanf("%s", stri1); NO AND SIGN BECAUSE ITS AN ARRAY AND THEREFORE AN ADDRESS printf("The string you entered was %s.\n", str1);

What is "variable" about the length of string?

The amount of the space reserved for the array that is actually used at any given time or during any given execution of the program.

What happens to the \n character used in the function with strings?

The function will accept input terminated by a new line and make a delimiter-terminated string out of it.

Why does the compiler not approve of str2 = str1?

The memory allocated to a statically declared array is fixed in terms of size and location. Based on the previous statement we would say the memory address to which an array refers is constant.

Why do we need a character to mark the end of the array?

There may come a time when the meaningful characters that compose a string do not fill the array, then how are we able to differentiate between meaningful and garbage character data? If all strings ALWAYS filled the array, then we would not need a character to act as a delimiter.

Is there a concern when using strcpy with more data being copied than there were spaces reserved in memory?

Yes, similar to potential problems with input the use of this function can result in exceeding the defined capacity of an array.

What do get input with strings?

do { x[index++] = getchar(); } while(index < SIZE && x[index - 1] != '\n'); //REPLACE THE '\n' WITH A DELIMITER CHARACTER x[index - 1] = 0 The do0while will terminate the accepting of the input when the user enters a new line character or when the defined size of the array has been reached. Because it handles only individual character input it does not place the delimiter character in the array, but it will place the new line character (ASCII Value 10) into the array, the delimiter must be assigned to the array and will overwrite the new line character int he function above.

Copying Strings

strcpy Takes the second argument and copy its value to the first. strcpy(str2, str1);

String - Definition

A series (character array) of characters with the capability to be treated as a single unit. C has variable length strings, you can declare it to be any size.

What are unused elements in a string?

Garbage/unknown values. 0 of the ASCII if you initialize.

What makes gets/scanf "dangerous" with strings?

Neither function will stop accepting input when the character array has been filled to its capacity. Exceeding the defined capacity of the array will result in the same problems associated with range checking issues.

Ca you use equality statements with two strings?

No. If you were to attempt to do this you're comparing two memory addresses not numbers. The output would always be false because the name of arrays represent a memory address. Two character arrays will never share the same memory address. Name represents a location NOT the context.


Related study sets

Chapter 4. Networking—It's Always Who You Know

View Set

Business Info Systems: Pivot Tables

View Set

Accounting Ch. 5 - Balance Sheets

View Set

Wk 4 - Practice: Ch. 7, Business Strategy: Innovation [due Day 5]

View Set