You are on page 1of 5

Lab 8

This manual covers:

C++ Character and String

Exercise 8.1

Write a program using programmer defined function that:

i. Reads 3 characters

ii. Select and display the first character among the three by writing
functions:

i. to find the first character

ii. to print out the first character

Exercise 8.2

Write the following program. It show a simple manipulation of character string.

#include<iostream>

int main(void)

Int i;

charcolour [] = "blue";

i=0;

while (colour[i] !='\0')

{
putchar (colour[i]);

putchar (' ');

i++;

Exercise 8.3

Write the following program. The program copy a string to another string

#include <cstring>
#include <iostream>
using namespace std;
#define COPY_SIZE 64 // maximum characters to copy
int main()
{
const char *Original = "I am the original!";
char Copy[COPY_SIZE + 1] = "I am the copy!";

cout << "Before strcpy(): " << endl;


cout << "Original = " << Original << endl;
cout << "Copy = " << Copy << endl;

strcpy(Copy, Original);
cout << "After strcpy(): " << endl;
cout << "Original = " << Original << endl;
cout << "Copy = " << Copy << endl;

return 0;

Exercise 8.4

Write the following program. The program lets you know the length of a string

#include <cstring>
#include <iostream>
using namespace std;

int main()
{
const char *String= "I am the string!";

cout << "Here is string:" << String << endl;


cout << "Its length is: " << strlen(String) << "
characters\n";

return 0;

Exercise 8.5

Write a program to convert a string in lowercase?

Exercise 8.6

Write a program to concatenate one string contents to another?

Exercise 8.7

Write a program to display string from backward?

Exercise 8.8

Compile and run following C++ code; The program demonstrates how strings are
passed to a function.

#include <iostream>
using namespace std;

void display(char s[]);

int main()
{
char str[100];
string str1;

cout << "Enter a string: ";


cin.get(str, 100);

cout << "Enter another string: ";


getline(cin, str1);

display(str);
display(str1);
return 0;
}

void display(char s[])


{
cout << "You entered char array: " << s <<;
}

void display(string s)
{
cout << "You entered string: " << s << endl;
}

Exercise 8.9

Make a call to binarysearch function defined below from the main function.Compile and
run the complete C++ code;

bool binarysearch(type items[], type element, int size)


{
bool found = false;
int bottom, middle, top;

bottom = 0;
top = size - 1;
while (!found && bottom <= top)
{
middle = (bottom + top)/2;

if (items[middle] == element)
found = true;
else
if (items[middle] > element)
top = middle - 1;
else
bottom = middle + 1;
}

return found;
}
LAB ASSIGNMENT 4

Write a program that reads in a two-dimensional array representing the coordinates


of points in space.

The program should determine the largest absolute value in the array and scale
each element of the array by dividing it by this largest value.

The new array, which is called the normalized array, should have the largest value.

The program consists of two functions:

main() obtains the number of data points, and the x, y, and z coordinates
of each point.
normalize() normalize the array.

You might also like