madalinaradu

p29-numarare-spatii

Apr 29th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /** numara spatii recursiva*/
  4. int count_spaces(char *s){
  5.     if(*s=='\0'){
  6.         return 0;
  7.     } else {
  8.        int nr_spatii_rest =  count_spaces(s+1);
  9.        if(*s == ' '){
  10.          return 1 + nr_spatii_rest;
  11.        } else {
  12.          return nr_spatii_rest;
  13.        }
  14.     }
  15. }
  16.  
  17. int count_spaces_iterativ(char *s){
  18.     int i,nr_spatii =0;
  19.     for(i=0;s[i]!='\0';i++){
  20.        if(s[i] == ' '){
  21.          nr_spatii++;
  22.        }
  23.     }
  24.     return nr_spatii;
  25. }
  26.  
  27. int main(){
  28.     char s[100];
  29.     printf("Dati sirul:");
  30.     gets(s);
  31.     printf("Numarul de spatii este: %d\n", count_spaces(s));
  32.     printf("Numarul de spatii este: %d\n", count_spaces_iterativ(s));
  33.  
  34.  
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment