Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Week 14 Pointers.cpp : Defines the entry point for the console application.
- //
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- void charAddresses(char *string); // 1st function to do
- void commaToSpace(char *str); // 2nd function to do
- void printWord(char *start); //3rd function to do
- char *nextWord(char *start); // 4th function to do
- void main()
- {
- int i = 0;
- char location[200] = "7825,CREEK VALLEY,SACRAMENTO,95828,CA";
- char *ptr;
- // 1. call function to print out the address and content of each character in the input array
- charAddresses(location);
- // 2. call function to replace all the commas in the input string with tabs
- commaToSpace(location);
- puts("Without commas:");
- puts(location);
- puts("");
- // 3. instead of printing characters (using putchar) until a '\0', printWord prints characters until a space ' '
- puts("First Word");
- printWord(location);
- puts("Second Word");
- printWord(location + 5);
- puts("Third Word");
- printWord(location + 11);
- puts("");
- // starting from the first character in the input string, each call to "nextWord" should return the next word in the string
- // e.g. if the input string is "Hi there everyone" :
- // first call to nextWord should return the address of the letter 't' of "there"
- // second call to nextWord should return the address of the first letter 'e'of "everyone"
- // third call to nextWord should return NULL
- ptr = location;
- while (ptr)
- {
- // instead of printing characters (using putchar) until a '\0', printWord prints characters until a space ' '
- printWord(ptr);
- printf("\n");
- ptr = nextWord(ptr);
- }
- }
- void charAddresses(char *string)
- {
- // insert your code here
- int stringLenght = strlen(string);
- for (int i = 0; i<stringLenght; i++)
- {
- printf("Adress: %p Content: %c \n", &string[i], string[i]);
- }
- }
- void commaToSpace(char *str)
- {
- // insert your code here
- int stringLenght = strlen(str);
- for (int i = 0; i<stringLenght; i++)
- {
- if (str[i] == ',')
- {
- str[i] = ' ';
- }
- }
- }
- void printWord(char *start)
- {
- // insert your code here
- while (*start != ' ')
- {
- printf("%c", *start);
- start++;
- }
- printf("\n");
- }
- char *nextWord(char *start)
- {
- // insert your code here
- // of course it should only return NULL if there are no more words
- // HINT: 'return (start+i)' will return member [i] of the array (string) pointed at by 'start'
- for (int i = 5; i<strlen(start); i++)
- {
- if (start[i] == ' ')
- {
- printf("\n\n");
- i++;
- }
- printf("%c", start[i]);
- }
- printf("\n");
- return NULL;
- }
- //by Jaroslaw Janas
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement