idastan97

CSCI151 L24 P2

Nov 1st, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.18 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3.  
  4. void stringCopy(char *fromStr, char *toStr) {
  5.     while (*fromStr!='\0'){
  6.         *toStr=*fromStr;
  7.         toStr++;
  8.         fromStr++;
  9.     }
  10.     *toStr='\0';
  11. }
  12.  
  13. _Bool areEqual(char *str1, char *str2) {
  14.     while (*str1!='\0' && *str2!='\0'){
  15.         if (*str1==*str2){
  16.             str1++;
  17.             str2++;
  18.         }
  19.         else
  20.             return 0;
  21.     }
  22.     if (*str1==*str2)
  23.         return 1;
  24.     else
  25.         return 0;
  26. }
  27.  
  28. void reverse(char *fromStr, char *toStr) {
  29.     int n;
  30.     for (n=0; *fromStr!='\0'; n++, fromStr++);
  31.     fromStr--;
  32.     int i;
  33.     for (i=0; i<n; i++){
  34.         *toStr=*fromStr;
  35.         fromStr--;
  36.         toStr++;
  37.     }
  38.     *toStr='\0';
  39. }
  40.  
  41. void concatenate(char *str1, char *str2, char *resultStr) {
  42.     int i;
  43.     while (*str1!='\0'){
  44.         *resultStr=*str1;
  45.         resultStr++;
  46.         str1++;
  47.     }
  48.        
  49.     while (*str2!='\0'){
  50.         *resultStr=*str2;
  51.         resultStr++;
  52.         str2++;
  53.     }
  54.     *resultStr='\0';
  55. }
  56.  
  57. int main(void) {
  58.  
  59.     char word1[] = "Hello";
  60.     char word2[] = "Hi there";
  61.     char word3[] = {'H', 'e', 'l', 'l', 'o', '\0', 'X'};
  62.     char word4[] = "Hi";
  63.  
  64.     char result[50];
  65.  
  66.     // Test code for stringCopy
  67.     stringCopy(word1, result);
  68.     printf("Test 1.1 result: %s\n", result);
  69.     stringCopy(word2, result);
  70.     printf("Test 1.2 result: %s\n", result);
  71.     stringCopy(word3, result);
  72.     printf("Test 1.3 result: %s\n", result);
  73.  
  74.     // Test code for areEqual
  75.     printf("Test 2.1 result: %i\n", areEqual(word1, word2));
  76.     printf("Test 2.2 result: %i\n", areEqual(word1, word3));
  77.     printf("Test 2.3 result: %i\n", areEqual(word2, word4));
  78.     printf("Test 2.4 result: %i\n", areEqual(word4, word2));
  79.  
  80.     // Test code for reverse
  81.     reverse(word1, result);
  82.     printf("Test 3.1 result: %s\n", result);
  83.     reverse(word2, result);
  84.     printf("Test 3.2 result: %s\n", result);
  85.     reverse(word3, result);
  86.     printf("Test 3.3 result: %s\n", result);
  87.  
  88.     // Test code for concatenate
  89.     concatenate(word1, word2, result);
  90.     printf("Test 4.1 result: %s\n", result);
  91.     concatenate(word3, word4, result);
  92.     printf("Test 4.2 result: %s\n", result);
  93.     concatenate(word1, word1, result);
  94.     printf("Test 4.3 result: %s\n", result);
  95.  
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment