IFM 2243 - Intro. To Programming

Exercise - String

  1. What value is returned by the following call to strlen?strlen("robot");
  1. In the fragment below, what is the minimum size of result required for successful and valid concatenation of "double" and "trouble"?

strcpy(result, "double");

strcat(result, "trouble");

  1. Complete the following statements based on this declaration:

char taste[10] = "delicious";

  1. The value of strlen(taste) is _____ .
  2. The null character is the value of taste[??]. Fill in the ??.
  3. The value of strcmp(taste, "terrible") is (less than/ greater than/ equal to) zero. Choose one.
  1. What is the value of the expression that follows?strcmp("5", "49");
  1. What is the new content of s1?

char s1[20];

strcpy(s1, “Hello”);

strcat(s1, “World”);

  1. What is the new content of s?

char h[6] = "happy";

char p[6] = "life";

char s[20];

strcpy(s, p);

strcat(s, h);

  1. Given the following strings, give the output of following string functions:

(Note: Assume each question is independent)

char one [20]= “banana”;
char two [20]= “grape”;
char three [20]= “apple”;

a.strcat (one, two);

printf(“%s %s”, one, two);

b.strcpy (two, three);

printf(“%s %s”, two, three);

c.printf (“%s %c %d”, one, two[3],strlen(three);

  1. What will be displayed by the program segment below?

char s[2][10] = {"petunia","marigold"};
s[0][3] = '\0';
s[1][5] = '\0';
printf("%s %s\n", s[0], s[1]);
  1. To compare if a string called A is identical to another string called B, the selection statement needed is ______.
  1. What will be returned by the program segment below?
  2. strcmp(“Petaling Jaya”, “Petaling Jaya”)
  3. strcmp(“their”, “there”)
  4. strcmp(“The”, “the”);
  5. strcmpi(“THE”, “the”);
  6. strncmp(“Beatles”,”Beetles”, 2);
  7. strrev(“ramadhan”)
  8. strlwr(“CFSIIUM”);
  9. strupr(“HellO”);

IFM 2243 - Intro. To Programming

Exercise - String

Strings-Lab exercise

  1. Create a program that will read a string and count the number of times the letter A or a appears in the string.

Sample output:

Input your string: International Islamic University Malaysia

Number of A/a:6

  1. Write a program that reads a string from the user. Count a total for consonants and vowels. Convert all uppercase letters to lowercase letters and vice versa. Use the string library functions for the conversion (e.g. isupper, islower, toupper, tolower).

Sample output:

Enter your sentence : InternatioNAL IslaMIC UNIversity MALaysia

Number of consonants:21

Number of vowels:17

After conversion : iNTERNATIOnal iSLAmic uniVERSITY malAYSIA

  1. Using only the given variables, write a program that:
  2. reads a string with maximum length of 30 characters
  3. asks user for a character to be replaced in that sentence
  4. asks user character to replace in that sentence
  5. displays the new sentence

Sample output:

Enter your sentence: Happy Birthday

Enter the character to be replaced: a

Enter the character to replace: *

Your new sentence is: H*ppy Birthd*y

  1. Given an array of characters of specified size, store a string into the array and determine the position of a specified character in the array. For example, in the string “I love Malaysia”, the indexes of the character ‘a’ are8, 10 and 14.
  1. Write a program that reads a username and password from the user. If the usernameand password matched (Refer to the table below), the program will display “LoginSuccessfully” and the user’s name. Otherwise, it will display “Invalid User!”.

UserName / Password / Name
090001 / hello123 / Wardina
090002 / welcome / Munawarah
090003 / $frt#a / Mawaddah