Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6. void espace(char* ch,int n,int* res,char** adrch2){
  7. int i,nbEspaces = 1,bInf,bSup;
  8. char *ch2;
  9.    
  10.     for(i=0;i<strlen(ch);i++){
  11.         if (nbEspaces == n){
  12.             bSup = i;
  13.         }
  14.         if (ch[i] == ' '){
  15.             nbEspaces++ ;
  16.         }
  17.     }
  18.     i=bSup;
  19.     while(i>0){
  20.         i--;
  21.         if (ch[i] == ' '){
  22.             bInf = i;
  23.             break;
  24.         }
  25.     }
  26.    
  27.     *res = nbEspaces <= n;
  28.  
  29.     ch2 = (char*) malloc((bSup-bInf+2)*sizeof(char));
  30.     for (i=bInf;i<=strlen(ch);i++){
  31.         ch2[i-bInf]=ch[i];
  32.     }
  33.    
  34.     *adrch2 = ch2;
  35.  
  36. }
  37. int main(){
  38.     char ch[20] ;
  39.     int n,res;
  40.     int *pres = &res;
  41.     char *ch2 = NULL;
  42.    
  43.     gets(ch);
  44.     printf("\n donner n: ");
  45.     scanf("%d",&n);
  46.    
  47.     espace(ch,n,pres,&ch2);
  48.     res = *pres;
  49.     printf("\nch2: %s;  res: %d",ch2,res);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement