IFM 2243 - Intro. To Programming
Exercise - String
- What value is returned by the following call to strlen?strlen("robot");
- 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");
- Complete the following statements based on this declaration:
char taste[10] = "delicious";
- The value of strlen(taste) is _____ .
- The null character is the value of taste[??]. Fill in the ??.
- The value of strcmp(taste, "terrible") is (less than/ greater than/ equal to) zero. Choose one.
- What is the value of the expression that follows?strcmp("5", "49");
- What is the new content of s1?
char s1[20];
strcpy(s1, “Hello”);
strcat(s1, “World”);
- What is the new content of s?
char h[6] = "happy";
char p[6] = "life";
char s[20];
strcpy(s, p);
strcat(s, h);
- 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);
- 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]);
- To compare if a string called A is identical to another string called B, the selection statement needed is ______.
- What will be returned by the program segment below?
- strcmp(“Petaling Jaya”, “Petaling Jaya”)
- strcmp(“their”, “there”)
- strcmp(“The”, “the”);
- strcmpi(“THE”, “the”);
- strncmp(“Beatles”,”Beetles”, 2);
- strrev(“ramadhan”)
- strlwr(“CFSIIUM”);
- strupr(“HellO”);
IFM 2243 - Intro. To Programming
Exercise - String
Strings-Lab exercise
- 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
- 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
- Using only the given variables, write a program that:
- reads a string with maximum length of 30 characters
- asks user for a character to be replaced in that sentence
- asks user character to replace in that sentence
- 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
- 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.
- 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