- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// String Copy
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
int main()
{
char str1[] = "APPLE";
char str2[] = "abc";
strncpy(str1, str2, 3);
cout << str1 <<
endl;
return 0;
}
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Copying n characters
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
int main()
{
char str1[] = "APPLE";
char str2[] = "abc";
strncpy(str1, str2, 4);
cout << str1 <<
endl;
return 0;
}
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Finding a character in a string
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
int find(char *s, char c)
{
int len = 0;
while (*++s != '\0')
if (*s == c)
len++;
return len;
}
int main()
{
char str1[] = "This is an island.";
char str2[] = "That is a dog.";
cout << find(str1, 'i') << endl;
cout << find(str2, 'T') << endl;
return 0;
}