cs:c_language:functions_and_arrays_1
Return to Home page
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/c_language/functions_and_arrays_1?do=diff&rev2%5B0%5D=1551188115&rev2%5B1%5D=1551188115&difftype=sidebyside
Differences
This shows you the differences between two versions of the page.
— | cs:c_language:functions_and_arrays_1 [2024/04/08 22:35] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
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; | ||
+ | } | ||
+ | </ |
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/c_language/functions_and_arrays_1?do=diff&rev2%5B0%5D=1551188115&rev2%5B1%5D=1551188115&difftype=sidebyside
/web/htdocs/www.skenz.it/home/data/pages/cs/c_language/functions_and_arrays_1.txt · Last modified: 2024/04/08 22:35 by 127.0.0.1