This shows you the differences between two versions of the page.
cs:c_language:functions_and_arrays_1 [2019/02/26 14:35] |
cs:c_language:functions_and_arrays_1 [2020/11/26 23:18] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Functions and Arrays (Example 1) ====== | ||
+ | **Concepts: | ||
+ | Passage of arrays to functions (arrays in the C programming language are passed to functions by reference) | ||
+ | **Text:**\\ | ||
+ | Implement a function that: | ||
+ | * receives in input a vector of characters and its dimension | ||
+ | * transforms all the upper case characters present in the vector in lower case characters and vice versa | ||
+ | * do nothing for non alphabetic characters | ||
+ | * the function must return the number of transformations | ||
+ | |||
+ | The calling function ('' | ||
+ | |||
+ | **Solution: | ||
+ | <file C functions_arrays_1.c> | ||
+ | /* Implement a function that receives in input a vector of characters and its dimension. | ||
+ | The function must transform all the upper case characters present in the vector in lower case characters and vice versa. | ||
+ | It does not have to do anything for non alphabetic characters present in the array. | ||
+ | The function must return the number of transformations. | ||
+ | The calling function ('' | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define SIZE 8 | ||
+ | |||
+ | int trasform(char v[], int dim){ | ||
+ | int i; | ||
+ | int n_transf=0; | ||
+ | | ||
+ | for(i=0; i<dim; i++) | ||
+ | if (isalpha(v[i])){ | ||
+ | if (islower(v[i])) | ||
+ | v[i] = toupper(v[i]); | ||
+ | else | ||
+ | v[i] = tolower(v[i]); | ||
+ | n_transf++; | ||
+ | } | ||
+ | |||
+ | return n_transf; | ||
+ | } | ||
+ | |||
+ | int main(){ | ||
+ | char vet[SIZE]={' | ||
+ | int i; | ||
+ | int n_trasformations; | ||
+ | |||
+ | n_trasformations = trasform(vet, | ||
+ | | ||
+ | for(i=0; i< | ||
+ | printf(" | ||
+ | printf(" | ||
+ | | ||
+ | printf(" | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ |