User Tools

Site Tools


cs:c_language:functions_and_arrays_1
Return to Home page

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

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 (''main'') must print the transformed vector as well as the number of transformations performed.
 +
 +**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 (''main'') must print the transformed vector as well as the number of transformations performed. */
 +
 +#include <stdio.h>
 +#include <ctype.h>
 +
 +#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]={'a', 'B', 'C', '1', '?', 'x', 'Y', '*'};
 +  int i;
 +  int n_trasformations;
 +
 +  n_trasformations = trasform(vet, SIZE);
 +  
 +  for(i=0; i<SIZE-1; i++)
 +    printf("%c ", vet[i]);
 +  printf("%c\n", vet[SIZE-1]);
 +  
 +  printf("Number of transformations: %d\n", n_trasformations);
 +
 +  return 0;
 +}
 +</file>

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=&rev2%5B1%5D=1551188115&difftype=sidebyside
/web/htdocs/www.skenz.it/home/data/pages/cs/c_language/functions_and_arrays_1.txt ยท Last modified: 2020/11/26 23:18 (external edit)