Advertisement
Guest User

שאלה 1 - שאדי

a guest
Mar 26th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.77 KB | None | 0 0
  1. //פונקציה שמקבלת מחרוזת ומחזירה מחרוזת דינאמית שמכילה רק את המילה הראשונה//
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. char * func(char *str)
  7. {
  8.     char *newstr;
  9.     int i, k = 0, count = 0;
  10.  
  11.     for (i = 0; i < strlen(str); i++)
  12.     {  
  13.             count++;
  14.     }
  15.     newstr = (char *)malloc(sizeof(char)*(count + 1));
  16.  
  17.     if (newstr == NULL)
  18.     {
  19.         exit(1);
  20.     }
  21.        
  22.     for (i = 0; i < count; i++)
  23.     {
  24.         if (str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z')
  25.         {
  26.  
  27.  
  28.             *(newstr + k) = str[i];
  29.             k++;
  30.             if (str[i + 1] == ' ')
  31.                 i = count;
  32.         }
  33.     }
  34.  
  35.     {
  36.         *(newstr + k) = '\0';
  37.         return newstr;
  38.     }
  39.  
  40.  
  41. }
  42.  
  43. void main()
  44. {
  45.     char str[100], *a;
  46.     gets(str);
  47.     a = func(str);
  48.     puts(a);
  49.     free(a);
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement