0% found this document useful (0 votes)
4 views

Tutorial7(4)

Uploaded by

xuanvinhmel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Tutorial7(4)

Uploaded by

xuanvinhmel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

HIT365 C Programming – Tutorial 7

1. What do these programs do? Which of the following ways that the programs used to pass a
pointer to the function? (1) a non-constant pointer to non-constant data, (2) a constant pointer
to nonconstant data, (3) a non-constant pointer to constant data, and (4) a constant pointer to
constant data.

#include <stdio.h>
#include <ctype.h>

void convertToUppercase(char *sPtr);

int main( void ){


char string[] = "characters and $32.98";

printf( "The string before conversion is: %s", string );


convertToUppercase( string );
printf( "\nThe string after conversion is: %s\n", string );
return 0;
}

void convertToUppercase(char *sPtr){


while (*sPtr != '\0') {
if (islower( *sPtr )) {
*sPtr = toupper( *sPtr );
}
++sPtr;
}
}

#include <stdio.h>

void printCharacters(const char *sPtr);

int main( void ){


char string[] = "print characters of a string";

printf( "The string is:\n" );


printCharacters( string );
printf( "\n" );
return 0;
}

void printCharacters( const char *sPtr ){


for ( ; *sPtr != '\0'; sPtr++ ) {
printf( "%c", *sPtr );
}
}
2. What, if anything, prints when each of the following C statements is performed? If the
statement contains an error, describe the error and indicate how to correct it. Assume the
following variable definitions:
char s1[ 50 ] = "jack", s2[ 50 ] = " jill", s3[ 50 ], *sptr;
a) printf( "%c%s", toupper( s1[ 0 ] ), &s1[ 1 ] );
b) printf( "%s", strcpy( s3, s2 ) );
c) printf( "%s", strcat( strcat( strcpy( s3, s1 ), " and " ), s2 ) );
d) printf( "%u", strlen( s1 ) + strlen( s2 ) );
e) printf( "%u", strlen( s3 ) );

3. Find the error in each of the following program segments and explain how to correct it:
a) char s[ 10 ];
strncpy( s, "hello", 5 );
printf( "%s\n", s );
b) printf( "%s", 'a' );
c) char s[ 12 ];
strcpy( s, "Welcome Home" );
d) if ( strcmp( string1, string2 ) ) {
printf( "The strings are equal\n" );
}

4. Write a program that inputs a character from the keyboard and tests the character with each
of the functions in the character-handling library. The program should print the value
returned by each function.

5. Write a program that uses function strcmp to compare two strings input by the user. The
program should state whether the first string is less than, equal to or greater than the second
string.

6. Write a program that uses function strncat to concatenate two strings input by the user. The
program should input the number of characters to be concatenated from second string to the
first string.

You might also like