VelizarAng

Day5 - Задачи 1, 2, 3 и 4 (Дълго решение)

Jul 9th, 2021 (edited)
671
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int strSize(const char* str){  //Дължина на Стринг
  6.     int counter  = 0;
  7.     while(*str != '\0'){
  8.         counter++;
  9.         str++;
  10.     }
  11.     return counter;
  12. }
  13. void strCopy(char* destination, const char* source){   //Копиране на Стринг
  14.     char* p = destination;
  15.  
  16.     while(*source != '\0'){
  17.         *p = *source;
  18.         p++;
  19.         source++;
  20.     }
  21.     p = '\0';
  22. }
  23.  
  24. int strCompare(const char* str1, const char* str2){   //Сравняване на Стринг
  25.     if(strSize(str1) != strSize(str2)){
  26.         return (*str1 - *str2) > 0 ? 1 : -1;
  27.     }
  28.  
  29.     while(*str1){
  30.         if(*str1 != *str2){
  31.             return (*str1 - *str2) > 0 ? 1 : -1;
  32.         }
  33.         str1++;
  34.         str2++;
  35.     }
  36.     return 0;
  37. }
  38.  
  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.    
  47.     *str1End = '\0';
  48.  
  49.     return destination;
  50. }
  51.  
  52. int main(){
  53.  
  54.    //MAIN ZADACHA 1 - Дължина на Стринг
  55.     /*
  56.     char str[50];
  57.     printf("Enter string: ");
  58.     gets(str);
  59.     printf("%d", strSize(str));
  60.     */
  61.  
  62.     //MAIN ZADACHA 2 - Сравняване на Стринг
  63.     /*
  64.     char str[50];
  65.     char str2[50];
  66.     printf("Enter first string: ");
  67.     gets(str);
  68.     printf("Enter second string: ");
  69.     gets(str2);
  70.     printf("String compare: %d", strCompare(str, str2));
  71.     */
  72.  
  73.     //MAIN ZADACHA 3 - Копиране на Стринг
  74.     /*
  75.     char str[50];
  76.     char str2[50];
  77.     printf("Enter first string: ");
  78.     gets(str);
  79.     strCopy(str2, str);
  80.     printf("String 2 is: %s", str2);
  81.     */
  82.  
  83.     //MAIN ZADACHA 4 - Конкатениране на Стринг
  84.  
  85.     char str1[50];
  86.     printf("Enter string: ");
  87.     gets(str1);
  88.  
  89.     char str2[50];
  90.     printf("Enter string: ");
  91.     gets(str2);
  92.  
  93.     printf("Your concatenated strings: %s", strConcatenate(str1, str2));
  94.  
  95.  
  96.     return 0;
  97. }
  98.  
Add Comment
Please, Sign In to add comment