Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- /** numara spatii recursiva*/
- int count_spaces(char *s){
- if(*s=='\0'){
- return 0;
- } else {
- int nr_spatii_rest = count_spaces(s+1);
- if(*s == ' '){
- return 1 + nr_spatii_rest;
- } else {
- return nr_spatii_rest;
- }
- }
- }
- int count_spaces_iterativ(char *s){
- int i,nr_spatii =0;
- for(i=0;s[i]!='\0';i++){
- if(s[i] == ' '){
- nr_spatii++;
- }
- }
- return nr_spatii;
- }
- int main(){
- char s[100];
- printf("Dati sirul:");
- gets(s);
- printf("Numarul de spatii este: %d\n", count_spaces(s));
- printf("Numarul de spatii este: %d\n", count_spaces_iterativ(s));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment