Advertisement
Guest User

Pointers Week 14

a guest
Jan 22nd, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.85 KB | None | 0 0
  1. // Week 14 Pointers.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7.  
  8. void charAddresses(char *string); // 1st function to do
  9. void commaToSpace(char *str); // 2nd function to do
  10. void printWord(char *start); //3rd function to do
  11. char *nextWord(char *start); // 4th function to do
  12.  
  13.  
  14. void main()
  15. {
  16.     int i = 0;
  17.     char location[200] = "7825,CREEK VALLEY,SACRAMENTO,95828,CA";
  18.  
  19.  
  20.     char *ptr;
  21.  
  22.     // 1. call function to print out the address and content of each character in the input array
  23.     charAddresses(location);
  24.  
  25.     // 2. call function to replace all the commas in the input string with tabs
  26.     commaToSpace(location);
  27.  
  28.     puts("Without commas:");
  29.     puts(location);
  30.     puts("");
  31.  
  32.     // 3. instead of printing characters (using putchar) until a '\0', printWord prints characters until a space ' '
  33.     puts("First Word");
  34.     printWord(location);
  35.  
  36.     puts("Second Word");
  37.     printWord(location + 5);
  38.  
  39.     puts("Third Word");
  40.     printWord(location + 11);
  41.     puts("");
  42.  
  43.     // starting from the first character in the input string, each call to "nextWord" should return the next word in the string
  44.     // e.g. if the input string is "Hi there everyone" :
  45.     // first call to nextWord should return the address of the letter 't' of "there"
  46.     // second call to nextWord should return the address of the first letter 'e'of "everyone"
  47.     // third call to nextWord should return NULL
  48.     ptr = location;
  49.     while (ptr)
  50.     {
  51.         // instead of printing characters (using putchar) until a '\0', printWord prints characters until a space ' '
  52.         printWord(ptr);
  53.         printf("\n");
  54.         ptr = nextWord(ptr);
  55.     }
  56. }
  57.  
  58. void charAddresses(char *string)
  59. {
  60.     // insert your code here
  61.     int stringLenght = strlen(string);
  62.     for (int i = 0; i<stringLenght; i++)
  63.     {
  64.         printf("Adress: %p   Content: %c   \n", &string[i], string[i]);
  65.     }
  66.  
  67. }
  68. void commaToSpace(char *str)
  69. {
  70.     // insert your code here
  71.     int stringLenght = strlen(str);
  72.     for (int i = 0; i<stringLenght; i++)
  73.     {
  74.         if (str[i] == ',')
  75.         {
  76.             str[i] = ' ';
  77.         }
  78.     }
  79.  
  80.  
  81. }
  82.  
  83. void printWord(char *start)
  84. {
  85.     // insert your code here
  86.  
  87.     while (*start != ' ')
  88.     {
  89.         printf("%c", *start);
  90.         start++;
  91.     }
  92.     printf("\n");
  93. }
  94.  
  95. char *nextWord(char *start)
  96. {
  97.     // insert your code here
  98.     // of course it should only return NULL if there are no more words
  99.     // HINT: 'return (start+i)' will return member [i] of the array (string) pointed at by 'start'
  100.  
  101.     for (int i = 5; i<strlen(start); i++)
  102.     {
  103.         if (start[i] == ' ')
  104.         {
  105.             printf("\n\n");
  106.             i++;
  107.         }
  108.         printf("%c", start[i]);
  109.     }
  110.     printf("\n");
  111.     return NULL;
  112. }
  113. //by Jaroslaw Janas
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement