Skip to content

Commit

Permalink
functions and character arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
jainkhere committed Feb 8, 2022
1 parent 805cfe2 commit 25ebca7
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 0 deletions.
66 changes: 66 additions & 0 deletions function_array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* @file function_array.c
* @author kunal jain ([email protected])
* @brief Program to demonstrate how functions
* work with arrays as arguments.
* Given an integer array, we will increase
* all elements of array by one using function.
* @version 0.1
* @date 2022-02-05
*
* @copyright Copyright (c) 2022
*
*/

#include<stdio.h>

// Function to print array.
// Parameters - array, length of array.
// Returns - void
void printa(int arr[], int len) {
int i;
for (i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

// Function to increase all elements in an array by one
// Parameters - array, length of array
// Returns - void
void increase_by_one(int arr[], int len) {
int i;
for (i = 0; i < len; i++) {
arr[i] = arr[i] + 1;
}
}

int main(void) {

int i;
// Array declared of two values.
int arr[2] = {1, 2};

// Print array before calling function.
// printa function expects two arguments - array and length of array
printf("Initial array values\n");
printa(arr, 2);

// Function called to increase value
// increase_by_one expects one argument - array
//
// Notice that function is not returning any value.
// When array is passed as an argument, the value passed to the
// function is the address of the beginning of the array.
// All changes to array in function will be seen here as well.
increase_by_one(arr, 2);

// When array is printed after calling increase_by_one function,
// Values of array are incremented even though function did not
// return updated array.
printf("Final Array values\n");
printa(arr, 2);

return 0;

}
44 changes: 44 additions & 0 deletions function_call-by-reference.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @file function_call-by-reference.c
* @author kunal jain ([email protected])
* @brief Program to write a function to swap 2 numbers
* @version 0.1
* @date 2022-02-02
*
* @copyright Copyright (c) 2022
*
*/

#include<stdio.h>

// Declare and define a function to swap two numbers
// Pointer variable -> Variable to store address of another variable.

// In this swap function, we will change the value of variable present
// at the memory location where pointer is pointing.
void swap (int *x, int *y) {

int temp;

temp = *x;
*x = *y;
*y = temp;

}

int main(void) {
int a = 7, b = 5;

printf("%d %d\n", a, b);

// Pass address instead of variable.

// Address of(&) -> Address of operator gives address of
// the variable.
swap(&a, &b);

// Values of a and b changes once address is passed as argument.
printf("%d %d\n", a, b);

return 0;
}
137 changes: 137 additions & 0 deletions function_char_longest-line.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/**
* @file function_char_longest-line.c
* @author kunal jain ([email protected])
* @brief Program to read input line by line and
* return longest line from the input.
* This program uses functions, character I/O,
* and arrays that we learnt in past classes. This
* single program sums it up all.
* Assume maximum length of any line in input is 1000 characters.
* @version 0.1
* @date 2022-02-05
*
* @copyright Copyright (c) 2022
*
*/

// while (there is another line)
// if (it's longer than the previous longest)
// save it
// save its length
// print longest line

#include<stdio.h>

// Function to read input till new line character and
// get length of line.
// Paramters - char array to store characters that are being read.
// Return - length of a line as integer
int getlinelength(char input[]) {
// Read a character till new line or EOF.

// If first character is new line then length of line = 1

// Put null ('\0') character at the end of the array
// to mark end of string of character.

// Because length of line = number of characters read,
// return i

int character, i;

// There are two test conditions with AND operator.
// Loop will stop if any one of the condition is false.
//
// Any changes in char input[] means changes in char line[],
// because array passed is pointer to its beginning location.
for (i = 0; (character = getchar()) != EOF && character != '\n'; ++i) {
input[i] = character;
}

// Added new line character in the end of line as
// line with only new line character has length of one.
if (character == '\n') {
input[i] = character;
++i;
}

// Put character '\0' (null character) at the end of the array
// to mark the end of the string of characters.
input[i] = '\0';

// Return length of line which is also equal to number
// of characters read
return i;
}

// Function to copy characters from one char array to another
// This copies 'from' into 'to'.
// Parameter - char from[], char to[]
// Return - void
void copy(char from[], char to[]) {

int i = 0;

// Copy 'from' into 'to' one character at a time
// until null character encountered.
while ((to[i] = from[i]) != '\0') {
i++;
}
// Any changes in 'to' array means changes in longestline array.

// This function does not return anything.
// Such functions have "void" return type.
}

int main(void) {

// Variable declarations

// Variable len to hold length of each line.
// Variable max to hold length of max line length.
int len, max = 0;

// Character array to store line from input.
char line[1000];

// Character array to store longest line.
// We will copy char array line into char array
// longestline if lenght of line is greater
// than current max length.
char longestline[1000];

// Get length of each line using getlinelength function
while ((len = getlinelength(line)) > 0) {
// If length of line greater than current max length
// then update max variable and copy line into
// longestline.
if (len > max) {
max = len;
copy(line, longestline);
}
}

// If max length of any line read is greater than zero
// then print longestline.
if (max > 0) {

printf("Longest line in the input is -> ");
// Two ways to print string in C.

// First way - using loop

// int i;
// for (i = 0; i < max; i++) {
// printf("%c", longestline[i]);
// }

// Second way

// %s -> Format specifier to print sequence of characters
printf("%s", longestline);

printf("\n");
}

return 0;
}

0 comments on commit 25ebca7

Please sign in to comment.