Advertisement
Denis_Hristov

HomeWork C Day5

Jul 9th, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. //funkciq zadacha 1
  5. int strSize(const char* str){
  6.     int counter  = 0;
  7.     while(*str != '\0'){
  8.         counter++;
  9.         str++;
  10.     }
  11.     return counter;
  12. }
  13.  
  14. //funkciq zadacha 2
  15. int strCompare(const char* str1, const char* str2){
  16.     while(*str1){
  17.         if(*str1 != *str2){
  18.             break;
  19.         }
  20.         str1++;
  21.         str2++;
  22.     }
  23.     return (*str1 - *str2);
  24. }
  25.  
  26. //funkciq zadacha 3
  27. void strCopy(char* destination, const char* source){
  28.     char* p = destination;
  29.  
  30.     while(*source != '\0'){
  31.         *p = *source;
  32.         p++;
  33.         source++;
  34.     }
  35.     p = '\0';
  36. }
  37.  
  38. //funkciq zadacha 4
  39. char* strConcatenate(char* destination, const char* source){
  40.  
  41.     char* str1End = destination + strSize(destination);
  42.  
  43.     while (*source != '\0') {
  44.         *str1End++ = *source++;
  45.     }
  46.     *str1End = '\0';
  47.  
  48.     return destination;
  49. }
  50.  
  51. //funkciq zadacha 5
  52. int cointainsNano(const char* str){
  53.     int state = 0; // empty
  54.  
  55.     while (*str){
  56.         if (state == 0){
  57.             if (*str == 'n'){
  58.                 state = 1; // n
  59.             }
  60.         }else if (state == 1){
  61.             if (*str == 'a'){
  62.                 state = 2; //na
  63.             }else if (*str == 'n'){
  64.                 state = 1; //same state
  65.             }else{
  66.                 state = 0; //empty
  67.             }
  68.         }else if (state == 2){
  69.             if (*str == 'n'){
  70.                 state = 3; //nan
  71.             }else{
  72.                 state = 0; //empty
  73.             }
  74.         }else if (state == 3){
  75.             if (*str == 'o'){
  76.                 state = 4;
  77.                 return 1;
  78.             }else if (*str == 'n'){
  79.                 state = 1; // n
  80.             }else if (*str == 'a'){
  81.                 state = 2; // na
  82.             }else{
  83.                 state = 0; // empty
  84.             }
  85.         }
  86.         str++;
  87.     }
  88.     return 0;
  89. }
  90.  
  91. //funkciq zadacha 6
  92. int countNano(const char* str){
  93.  
  94.     int cnt = 0;
  95.     int state = 0; //empty
  96.  
  97.     while (*str){
  98.         if (state == 0){
  99.             if (*str == 'n'){
  100.                 state = 1; //n
  101.             }
  102.         }else if (state == 1){
  103.             if (*str == 'a'){
  104.                 state = 2; //na
  105.             }else if (*str == 'n'){
  106.                 state = 1; //same state
  107.             }else{
  108.                 state = 0; //empty
  109.             }
  110.         }else if (state == 2){
  111.             if (*str == 'n'){
  112.                 state = 3; //nan
  113.             }else{
  114.                 state = 0; //empty
  115.             }
  116.         }else if (state == 3){
  117.             if (*str == 'o'){
  118.                 state = 4;
  119.                 cnt++;
  120.             }else if (*str == 'n'){
  121.                 state = 1; //n
  122.             }else if (*str == 'a'){
  123.                 state = 2; //na
  124.             }else{
  125.                 state = 0; //empty
  126.             }
  127.         }else if (state == 4){
  128.             if (*str == 'n'){
  129.                 state = 1; //n
  130.             }else{
  131.                 state = 0; //empty
  132.             }
  133.         }
  134.         str++;
  135.     }
  136.     return cnt;
  137. }
  138.  
  139. int main(){
  140.  
  141.     //zadacha 1
  142.  
  143.     /*
  144.     char str[50];
  145.     printf("Enter string: ");
  146.     gets(str);
  147.     printf("Your string size is: %d", strSize(str));
  148.     */
  149.  
  150.     //zadacha 2
  151.  
  152.     /*
  153.     char str[50];
  154.     char str2[50];
  155.     printf("Enter first string: ");
  156.     gets(str);
  157.     printf("Enter second string: ");
  158.     gets(str2);
  159.     printf("String compare: %d", strCompare(str, str2));
  160.     */
  161.  
  162.     //zadacha 3
  163.  
  164.     /*
  165.     char str[50];
  166.     char str2[50];
  167.     printf("Enter first string: ");
  168.     gets(str);
  169.     strCopy(str2, str);
  170.     printf("String 2 is: %s", str2);
  171.     */
  172.  
  173.     //zadacha 4
  174.  
  175.     /*
  176.     char str1[50];
  177.     printf("Enter string: ");
  178.     gets(str1);
  179.  
  180.     char str2[50];
  181.     printf("Enter string: ");
  182.     gets(str2);
  183.  
  184.     printf("Your concatenated strings: %s", strConcatenate(str1, str2));
  185.     */
  186.  
  187.     //zadacha 5
  188.  
  189.     /*
  190.     char str1[50];
  191.     printf("Enter string: ");
  192.     gets(str1);
  193.     printf("Contains nano: %s\n", cointainsNano(str1) ? "yes" : "no");
  194.     */
  195.  
  196.     //zadacha 6
  197.  
  198.     /*
  199.     char str1[50];
  200.     printf("Enter string: ");
  201.     gets(str1);
  202.     printf("Count nano: %d\n", countNano(str1));
  203.     */
  204.  
  205.     return 0;
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement