char* rec2stringsCPointrLongest(char Ac[], char A2c[]);
char rec2stringsCPointrLongest(char Ac[], char A2c[]);
// Jones, Bradley L.; Peter Aitken; Dean Miller. C Programming in One Hour a Day, Sams Teach Yourself (p. 400). Pearson Education. Kindle Edition. // // Exercise 6, lesson 10. // 6. Write a function that accepts two strings. // Count the number of characters in each, and // return a pointer to the longer string. #include <stdio.h> char A[18]; char A2[18]; char* rec2stringsCPointrLongest(char Ac[], char A2c[]); int main( void ) { printf("\nEnter the string: "); gets(A); printf("\nEnter the string number 2: "); gets(A2); printf("\n%p\n", rec2stringsCPointrLongest(A, A2)); return 0; } char* rec2stringsCPointrLongest(char Ac[], char A2c[]) { int i = 0; int i2 = 0; while(Ac[i]!='\0') { i++; } while(A2c[i2]!='\0') { i2++; } printf("\nLength of [ %s ] is [ %d ]\n", Ac, i); printf("\nLength of [ %s ] is [ %d ]\n", A2c, i2); if(i > i2) { char *p_Ac; p_Ac = &Ac; printf("\nLength of the longest string ([ %s ]) is: [ %d ]. Returning pointer to the longest string (%p).\n", Ac, i, p_Ac); return p_Ac; } if(i < i2) { char *p_A2c; p_A2c = &A2c; printf("\nLength of the longest string ([ %s ]) is: [ %d ]. Returning pointer to the longest string (%p).\n", A2c, i2, p_A2c); return p_A2c; } if(i == i2) { printf("\nLengths of both strings ([ %s ] & [ %s ]) are the same: [ %d ] & [ %d ]. Returning pointer to first string.\n", Ac, A2c, i, i2); char *p_Ac; p_Ac = &Ac; return p_Ac; } }
// Jones, Bradley L.; Peter Aitken; Dean Miller. C Programming in One Hour a Day, Sams Teach Yourself (p. 400). Pearson Education. Kindle Edition. // // Exercise 6, lesson 10. // 6. Write a function that accepts two strings. // Count the number of characters in each, and // return a pointer to the longer string. #include <stdio.h> char A[18]; char A2[18]; char rec2stringsCPointrLongest(char Ac[], char A2c[]); int main( void ) { printf("\nEnter the string: "); gets(A); printf("\nEnter the string number 2: "); gets(A2); printf("\n%p\n", rec2stringsCPointrLongest(A, A2)); return 0; } char rec2stringsCPointrLongest(char Ac[], char A2c[]) { int i = 0; int i2 = 0; while(Ac[i]!='\0') { i++; } while(A2c[i2]!='\0') { i2++; } printf("\nLength of [ %s ] is [ %d ]\n", Ac, i); printf("\nLength of [ %s ] is [ %d ]\n", A2c, i2); if(i > i2) { char *p_Ac; p_Ac = &Ac; printf("\nLength of the longest string ([ %s ]) is: [ %d ]. Returning pointer to the longest string (%p).\n", Ac, i, p_Ac); return p_Ac; } if(i < i2) { char *p_A2c; p_A2c = &A2c; printf("\nLength of the longest string ([ %s ]) is: [ %d ]. Returning pointer to the longest string (%p).\n", A2c, i2, p_A2c); return p_A2c; } if(i == i2) { printf("\nLengths of both strings ([ %s ] & [ %s ]) are the same: [ %d ] & [ %d ]. Returning pointer to first string.\n", Ac, A2c, i, i2); char *p_Ac; p_Ac = &Ac; return p_Ac; } }
strcpy(dest, source);
// Jones, Bradley L.; Peter Aitken; Dean Miller. C Programming in One Hour a Day, Sams Teach Yourself (pp. 339-340). Pearson Education. Kindle Edition. /* ptr_math.c--Demonstrates using pointer arithmetic to access array elements with pointer notation. */ #include <stdio.h> #define MAX 10 // Declare and initialize an integer array. int i_array[MAX] = { 0,1,5,3,4,5,6,7,8,9 }; // Declare a pointer to int and an int variable. int *i_ptr, *i2_ptr, *i3_ptr, count; // Declare and initialize a float array. float f_array[MAX] = { .0, .1, .2, .3, .4, .5, .6, .7, .8, .9 }; // Declare a pointer to float. float *f_ptr, *f2_ptr, *f3_ptr; int main( void ) { /* Initialize the pointers. */ i_ptr = i_array; i2_ptr = i_array[2]; i3_ptr = i_array[3]; f_ptr = f_array; f2_ptr = &f_array[2]; f3_ptr = &f_array[3]; /* Print the array elements. */ // for (count = 0; count < MAX; count++) // printf("%d\t%f\n", *i_ptr++, *f_ptr++); for (count = 0; count < MAX; count++) { printf("%d\t%f\n", *i_ptr, *f_ptr ); i_ptr++; f_ptr++; } printf("\n%d\n", i2_ptr - i3_ptr ); printf("\n%d\n", f2_ptr - f3_ptr ); return 0; }
printf("\n%d\n", i2_ptr - i3_ptr );
// Jones, Bradley L.; Peter Aitken; Dean Miller. C Programming in One Hour a Day, Sams Teach Yourself (pp. 339-340). Pearson Education. Kindle Edition. /* ptr_math.c--Demonstrates using pointer arithmetic to access array elements with pointer notation. */ #include <stdio.h> #define MAX 10 // Declare and initialize an integer array. int i_array[MAX] = { 0,1,2,3,4,5,6,7,8,9 }; // Declare a pointer to int and an int variable. int *i_ptr, *i2_ptr, *i3_ptr, count; // Declare and initialize a float array. float f_array[MAX] = { .0, .1, .2, .3, .4, .5, .6, .7, .8, .9 }; // Declare a pointer to float. float *f_ptr, *f2_ptr, *f3_ptr; int main( void ) { /* Initialize the pointers. */ i_ptr = i_array; i2_ptr = i_array[2]; i3_ptr = i_array[3]; f_ptr = f_array; f2_ptr = f_array[2]; f3_ptr = f_array[3]; /* Print the array elements. */ // for (count = 0; count < MAX; count++) // printf("%d\t%f\n", *i_ptr++, *f_ptr++); for (count = 0; count < MAX; count++) { printf("%d\t%f\n", *i_ptr, *f_ptr ); i_ptr++; f_ptr++; } printf("\n%d\n", i2_ptr - i3_ptr ); printf("\n%f\n", f2_ptr - f3_ptr ); return 0; }
#include <stdio.h> #include <stdlib.h> /* https://www.mikedane.com/programming-languages/c/building-a-guessing-game/ */ int main() { int secretNum = 7; int guess; int guessCount = 0; int guessLimit = 3; int outOfGuesses = 0; while (guess != secretNum && outOfGuesses == 0){ if (guessCount < guessLimit){ printf("Enter a guess: "); scanf("d", &guess); guessCount++; } else { outOfGuesses = 1; } } if (outOfGuesses != 0){ printf("You Lose!"); } else { printf("You Win!"); } }
#include <stdio.h> #include <stdlib.h> int main(){ float num1, num2; char op; printf("Enter num1: "); scanf("%f", &num1); getchar(); printf("Enter Operator: "); scanf("%c", &op); printf("Enter num2: "); scanf("%f", &num2); /* printf("%f\n", num1); */ if(op == '+'){ printf("%f", num1 + num2); } else if(op == '-'){ printf("%f", num1 - num2); } else if(op == '/'){ printf("%f", num1 / num2); } else if(op == '*'){ printf("%f", num1 * num2); } else { printf("Invalid Operator"); } return 0; }