PART B
QUESTION 1
INPUT
#include <iostream>//for cin, cout....
#include <iomanip> //for setprecision(), fixed, setw()....
#include <cstring> //for string manipulated functions eg (strlen, strcpy....)
#include <cmath> //for pow(), sqrt()....
#include <cctype> //for character manipulated functions eg (isdigit, isalpha....)
using namespace std;
int main(void)
{
char chIn, chOut;
cout << "Enter a character: ";
cin >> chIn;
if (isalpha(chIn))
{
if (chIn != 'z' &&chIn != 'Z')
chOut = chIn + 1;
else
if (chIn == 'z')
chOut = 'a';
else
chOut = 'A';
cout << "Next Character is " << chOut << endl;
}
else
cout << "This is not a letter\n";
return 0;
}
OUTPUT
PART B
QUESTION 2
INPUT
#include <iostream>//for cin, cout....
#include <iomanip> //for setprecision(), fixed, setw()....
#include <cstring> //for string manipulated functions eg (strlen, strcpy....)
#include <cmath> //for pow(), sqrt()....
#include <cctype> //for character manipulated functions eg (isdigit, isalpha....)
using namespace std;
int main(void)
{
char word[21];
int i, len;
cout << "Enter a word: ";
cin >> word;
cout << "The word in uppercase: ";
len = strlen(word);
for (i = 0; i < len; i++)
cout << (char)toupper(word[i]);
cout << endl;
return 0;
}
OUTPUT
PART B
QUESTION 3
INPUT
int countCharacters(char str[])
{
int count = 0;
for (int i = 0; str[i] != '\0'; i++)
count++;
return count;
}
PART B
QUESTION 4
INPUT
#include <iostream>//for cin, cout....
#include <iomanip> //for setprecision(), fixed, setw()....
#include <cstring> //for string manipulated functions eg (strlen, strcpy....)
#include <cmath> //for pow(), sqrt()....
#include <cctype> //for character manipulated functions eg (isdigit, isalpha....)
using namespace std;
int countUpper(char str[]);
int main(void)
{
char str[20];
int count;
cout << "Enter a string: ";
cin.getline(str, 20);
count = countUpper(str);
cout << "The count is " << count << endl;
return 0;
}
int countUpper(char str[])
{
int i, len, count;
count = 0;
len = strlen(str);
for (int i = 0; i<len; i++)
if(isupper(str[i]))
count++;
return count;
}
OUTPUT
PART B
QUESTION 5
INPUT
#include <iostream>//for cin, cout....
#include <iomanip> //for setprecision(), fixed, setw()....
#include <cstring> //for string manipulated functions eg (strlen, strcpy....)
#include <cmath> //for pow(), sqrt()....
#include <cctype> //for character manipulated functions eg (isdigit, isalpha....)
using namespace std;
int searchChar(char str[], char ch);
int main(void)
{
char str[20]; //only and key in 19 character's words, 剩下的一个位 for \0
char ch;
int count;
cout << "Enter a string: ";
cin.getline(str, 20);
cout << "Enter a character: ";
cin>>ch;
count = searchChar(str,ch);
cout << "The count is " << count << endl;
return 0;
}
int searchChar(char str[], char ch)
{
int i, len, count;
count = 0;
len = strlen(str);
for (int i = 0; i<len; i++)
if (str[i]==ch)
count++;
return count;
}
OUTPUT
PART B
QUESTION 6
INPUT
#include <iostream>//for cin, cout....
#include <iomanip> //for setprecision(), fixed, setw()....
#include <cstring> //for string manipulated functions eg (strlen, strcpy....)
#include <cmath> //for pow(), sqrt()....
#include <cctype> //for character manipulated functions eg (isdigit, isalpha....)
using namespace std;
int findlargest(int table[][4], int rows);
int main(void)
{
int table[3][4] = {
{1,1,1,1},
{2,2,2,2},
{3,3,3,3}
};
int largest;
largest = findlargest(table, 3);//3 is num of row
cout << "The largest is " << largest << endl;
return 0;
}
int findlargest(int table [][4], int rows)
{
int j, largest,i;
largest = table[0][0];
for (i = 0; i < rows; i++)
for (j = 0; j < 4; j++)
if (largest < table[i][j])
largest = table[i][j];
return largest;
}
OUTPUT
PART B
QUESTION 7
INPUT
#include <iostream>//for cin, cout....
#include <iomanip> //for setprecision(), fixed, setw()....
#include <cstring> //for string manipulated functions eg (strlen, strcpy....)
#include <cmath> //for pow(), sqrt()....
#include <cctype> //for character manipulated functions eg (isdigit, isalpha....)
using namespace std;
int findsmallest(int table[][4], int rows);
int main(void)
{
int table[3][4] = {
{ 1,1,1,1 },
{ 2,2,2,2 },
{ 3,3,3,3 }
};
int smallest;
smallest = findsmallest(table, 3);//3 is num of row
cout << "The smallest is " << smallest << endl;
return 0;
}
int findsmallest(int table[][4], int rows)
{
int j, smallest, i;
smallest = table[0][0];
for (i = 0; i < rows; i++)
for (j = 0; j < 4; j++)
if (smallest > table[i][j])
smallest = table[i][j];
return smallest;
}
OUTPUT
PART D
QUESTION 1
INPUT
#include <iostream>//for cin, cout....
#include <iomanip> //for setprecision(), fixed, setw()....
#include <cstring> //for string manipulated functions eg (strlen, strcpy....)
#include <cmath> //for pow(), sqrt()....
#include <cctype> //for character manipulated functions eg (isdigit, isalpha....)
#define A_ASCII_CODE 97
using namespace std;
int main(void)
{
char ch;
int position;
cout << "Enter a letter: ";
cin >> ch;
if (isalpha(ch))
{
ch = tolower(ch);
position = ch - A_ASCII_CODE + 1;
cout << "The position in the alphabet is " << position << endl;
}
else cout << "This is not a letter\n";
return 0;
}
OUTPUT
PART D
QUESTION 2
INPUT
#include <iostream>//for cin, cout....
#include <iomanip> //for setprecision(), fixed, setw()....
#include <cstring> //for string manipulated functions eg (strlen, strcpy....)
#include <cmath> //for pow(), sqrt()....
#include <cctype> //for character manipulated functions eg (isdigit, isalpha....)
using namespace std;
void convertCase(char word[]);
int main(void)
{
char word[20];
cout << "Enter a word: ";
cin >> word;
convertCase(word);
cout << "The word is now: " << word << endl;
return 0;
}
void convertCase(char word[])
{
int i, len;
len =strlen(word);
for (i = 0; i < len; i++)
word[i] = toupper(word[i]);
return;
}
OUTPUT
PART D
QUESTION 3
INPUT
#include <iostream>//for cin, cout....
#include <iomanip> //for setprecision(), fixed, setw()....
#include <cstring> //for string manipulated functions eg (strlen, strcpy....)
#include <cmath> //for pow(), sqrt()....
#include <cctype> //for character manipulated functions eg (isdigit, isalpha....)
using namespace std;
int findtotal(int table[][4], int rows);
int main(void)
{
int table[3][4] = {
{ 1,1,1,1 },
{ 2,2,2,2 },
{ 3,3,3,3 }
};
int total;
total = findtotal(table, 3);//3 is num of row
cout << "The total is " << total << endl;
return 0;
}
int findtotal(int table[][4], int rows)
{
int j, total, i;
total = 0;
for (i = 0; i < rows; i++)
for (j = 0; j < 4; j++)
total += table[i][j];
return total;
}
OUTPUT
PART D
QUESTION 4
INPUT
#include <iostream>//for cin, cout....
#include <iomanip> //for setprecision(), fixed, setw()....
#include <cstring> //for string manipulated functions eg (strlen, strcpy....)
#include <cmath> //for pow(), sqrt()....
#include <cctype> //for character manipulated functions eg (isdigit, isalpha....)
using namespace std;
void reverseword(char word[]);
int main(void)
{
char word[20];
cout << "Enter a word: ";
cin >> word;
reverseword(word);
cout << "The word reversed is: " << word << endl;
return 0;
}
void reverseword(char word[])
{
int half_len, front, back;
char temp;
back = strlen(word) - 1;
half_len = strlen(word) / 2;
for (front = 0; front < half_len; front++, back--)
{
temp = word[front];
word[front] = word[back];
word[back] = temp;
}
return;
}
OUTPUT