

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The course covers important and advance elements of C and C plus plus programming language. This course provides the student with the skills required to design, code, and test and execute programs of simple to intermediate complexity. It includes: Prototype, Pointer, Memory, Input, Scanf, Function, String, Printf, Termination, While
Typology: Exercises
1 / 2
This page cannot be seen from the preview
Don't miss anything!
// solution for last lab #include <stdio.h> #include <stdlib.h> void mystery1( char *, const char * ); // prototype int mystery3( const char *s1, const char *s2 ); // prototype
int main() { char string1; char string2; int x; // if you are using pointer then you need to assign them memory to read user input through scanf function string1=(char) malloc(30); string2=(char) malloc(30); // if you dont do malloc then pointer is not pointing to a valid memory address and thus can not store it and give // segmentation fault or bus error
printf("Enter first string: "); scanf("%s",string1); printf("Enter first string: "); scanf("%s",string2);
printf("Calling mystery3 \n"); x=mystery3 (string1, string2); if(x==0) { printf(" String are not equal\n"); } else if(x==1) { printf(" String are equal\n"); }
printf("Calling mystery1 \n"); mystery1 (string1, string2); printf("%s\n",string1);
return 0; // indicates successful termination } // end main
// What does this function do? void mystery1( char *s1, const char *s2 ) { while ( *s1 != '\0' ) ++s1;
for ( ; *s1 = *s2; s1++, s2++ ); // empty statement } // end function mystery
int mystery3( const char *s1, const char *s2 )
for ( ; *s1 != '\0' && *s2 != '\0'; s1++, s2++ )
if ( *s1 != *s2 ) return 0;
return 1; } // end function mystery