ece exam 1

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

what is a basic scan statement for a string

- scanf("%s", userString); - When using scanf for a string (in contrast to int, double, or char), the subsequent string variable is not preceded by a & symbol.

fgets

-Allows spaces in a user's string input -fgets(str, num, stdin) - num = max number of characters to be written into str

strlen()

-size_t strlen(sourceStr) Returns number of characters in sourceStr up to, but not including, first null character. size_t is integer type. EX: char orgName[100] = "United Nations"; char userText[20] = "UNICEF"; char targetText[10]; x = strlen(orgName); // Assigns 14 to x x = strlen(userText); // Assigns 6 to x x = strlen(targetText); // Error: targetText may lack null char

strncpy()

-strncpy(destStr, sourceStr, numChars) Copies up to numChars characters. -EX: char orgName[100] = "United Nations"; char userText[20] = "UNICEF"; char targetText[10]; strncpy(orgName, userText, 6); // orgName is "UNICEF Nations"

Suppose string str1 is initially "Hello" and str2 is "Hi". After strcpy(str1, str2) and then strcpy(str2, "Bye"), what is str1? Omit the quotes.

Hi

Suppose string str1 is initially "Hello" and str2 is "Hi". After strcpy(str1,str2), what is str1? Omit the quotes.

Hi

For the given input, indicate what string will be put into userString by: scanf("%s", userString); Hi there

Hi - only characters up to the first whitespace will be entered

Indicate the result of comparing the first string with the second string. "banana", "bananarama"

If existing characters are equal, the shorter string is less than.

For string "Light", what is L's index?

L

str1 greater than str2

Returns: Positive number - Expression: strcmp(str1, str2) > 0

Indicate the result of comparing the first string with the second string. "merry", "Merry"

The coded value for 'm' is greater than the coded value for 'M'

For the given input, indicate what string will be put into userString by: scanf("%s", userString); Very fun

Very -The leading whitespace before V is skipped, so the next characters V, e, r, y are put into userString, stopping at the whitespace after the y.

For string "You wish!", which character is at index 4?

W

For the given input, indicate what string will be put into secondString by: scanf("%s", firstString); scanf("%s", secondString); We all know

all

initialize a string call firstMonth with a size of 8 to the month january

char firstMonth[8] = "January"; -- NOTE: To hold the string "January", 8 characters are needed, 'J', 'a', 'n', 'u', 'a', 'r', 'y', '\0'.

Type one statement that declares a string named smallestPlanet initialized to "Mercury". Let the compiler determine the string's size.

char smallestPlanet[] = "Mercury";

Given the following char array declaration, write an appropriate for loop char userText[10] = "Car";

for (i = 0; userText[i] != '\0'; ++i) { // Print userText[i] }

errors with C strings

for the program user to enter a string larger than the character array. That may cause the input statement to write to memorylocations outside the array's locations, which may corrupt other parts of program or data, and typically causes the program to crash.

Given homePlanet[] ="Earth", what size array is created by the compiler?

homePlanet needs to hold 6 characters: 'E', 'a', 'r', 't', 'h', '\0'.

Write an expression that evaluates to true if myName is greater than yourName.

if ( strcmp(myName, yourName) > 0) { ... }

eliminate end of line character

if (nameArr[strlen(nameArr) - 1] == '\n') { nameArr[strlen(nameArr)-1] = '\0'; }

Write an expression that evaluates to true if authorName1 is less than authorName2.

if (strcmp(authorName1, authorName2) < 0 ) { ... }

strcmp()

int strcmp(str1, str2) Returns 0 if str1 and str2 are equal, non-zero if they differ. -usually used to compare for equality, returning 0 if the strings are the same length and have identical characters. A common error is to use == when comparing C strings, which does not work. str1 == str2 compares the strings' addresses, not their contents. Because those addresses will usually be different, str1 == str2 will evaluate to 0. -common error is to forget to compare the result of strcmp with 0, as in if (strcmp(str1, str2)) {...}. The code is not a syntax error, but is a logic error because the if condition will be false (0) when the strings are equal. The correct condition would instead be if (strcmp(str1, str2) == 0)

For string "You wish!", which character is at index 9?

no character

For string "Oh my", which character is at index 2?

space

strcat()

strcat(destStr, sourceStr) Copies sourceStr (up to and including null character) to end of destStr (starting at destStr's null character). strcat(orgName, userText); // orgName is "United NationsUNICEF"

strchr()

strchr(sourceStr, searchChar) Returns NULL if searchChar does not exist in sourceStr. (Else, returns address of first occurrence, discussed elsewhere). NULL is defined in the string.h library. EX: if (strchr(orgName, 'U') != NULL) { // 'U' exists in orgName? ... // 'U' exists in "United Nations", branch taken } if (strchr(orgName, 'u') != NULL) { // 'u' exists in orgName? ... // 'u' doesn't exist (case matters), branch not taken }

compare two strings

strcmp(str1, str2) == 0. The strcmp function returns 0 if the strings are equal, and some non-zero value otherwise.

strcpy()

strcpy(destStr, sourceStr) Copies sourceStr (up to and including null character) to destStr. -EX: char orgName[100] = "United Nations"; char userText[20] = "UNICEF"; char targetText[10]; strcpy(targetText, userText); // Copies "UNICEF" + null char to targetText strcpy(targetText, orgName); // Error: "United Nations" has > 10 chars targetText = orgName; // Error: Strings can't be copied this way

strncat()

strncat(destStr, sourceStr, numChars) Copies up to numChars characters to destStr's end, then appends null character. EX: strcpy(targetText, "abc"); // targetText is "abc" strncat(targetText, "123456789", 3); // targetText is "abc123"

str1 equal to str2

- Returns: 0 - Expression: strcmp(str1, str2) == 0

str1 less than str2

- Returns: Negative number - Expression: strcmp(str1, str2) < 0

whitespace character

- a character used to represent horizontal and vertical spaces in text, and includes spaces, tabs, and newline characters. -Ex: "Oh my goodness!" has two whitespace characters, one between h and m, the other between y and g.

For the given input, indicate what string will be put into secondString by: scanf("%s", firstString); scanf("%s", secondString); Oh my!

- my! - firstString will get "Oh". The secondString will get the next non-whitespace characters "my!".


Kaugnay na mga set ng pag-aaral

Asepsis and infection control (Final)

View Set

AP Chemistry: Unit 2 Practice Questions

View Set

Psych 105- Accumulated Quiz Questions

View Set

Ejemplos usando el verbo NOSOTROS SOMOS/ESTAMOS (we are)

View Set

Intro computer programming final

View Set

Mental health ( hard) first 29 questions!

View Set

Macroeconomics - 40% of assessment

View Set