Exercises: Pointers and Strings
1.What does this program do:
#include <iostream.h>

void mystery1(char * , const char * );

int main()
{
	char string1[80], string2[80];
	cout << "Enter two strings: ";
	cin >> string1 >> string2;
	mystery1(string1, string2);
	cout << string1 << endl;
	exit(0);
}

void mystery1(char *s1, const char *s2)
{
	while (*s1) s1++;
	for ( ; *s1++ = *s2++; );
}
2.What does this program do?
#include <iostream.h>

int mystery2(const char * );

int main()
{
	char string[80];
	cout << "Enter a string: ";
	cin >> string;
	cout << mystery2(string) << endl;
	exit(0);
}

int mystery2(const char *s)
{
	for (int x = 0; *s; s++)
		++x;
	return x;
}
3.What does this program do?
#include <iostream.h>

int mystery3(const char * , const char * );

int main()
{
	char string1[80], string2[80];
	cout << "Enter two strings: ";
	cin >> string1 >> string2;
	cout << "The result is " << mystery3(string1, string2) << endl;
	exit(0);
}

int mystery3(const char *s1, const char *s2)
{
	for ( ; *s1 && *s2 ; s1++, s2++)
		if (*s1 != *s2)
			return (0);
		return(1);
}
4.Write a program with a function named flip_string, that will receive a string and reverse it. Display the string in main() before the function call, and display the reversed string after the function call. Note: you must use only one character array to store the string; do not create a new array to store the reversed string



Back to Previous Page

Document:
Local Date:
Last Modified On: