COP 4600 - Discussion 2

Introduction to C Programming

Exercises

Every week at the end of discussion exercises will be given. You will have to submit them at the beginning of the next discussion on paper.

These may possibly count as bonus credit at the end of the semester, so try to do them.

  1. Exercise 1: (Update: Solution here)

    Write a function (in a program) which takes in two parameters: a string called "text" and a string called "find". The function should return an int which is the number of times "find" (as a token) appears in the string "text". Example:

    If "text" = "test1 test2 test1 test3test1" and "find" = "test1", the function should return 2.

    To test the program, the program should take in a list of strings as parameters. The last parameter should be "find", while the rest should form "text". Example to run the above case:
    		   ./program test1 test2 test1 test3test1 test1
    		   
  2. Exercise 2:

    Go through the following code line-by-line and figure out the output it will produce.

    Update: Output here
    	#include <stdio.h>
    
    	int main(int argc, char **argv) {
    asdsasdsadssssaschar *strs[2][3] = { { "one", "two", "three" },
    asdasdsadsadsadaasdiasudasdsdsadsadls{ "four", "five", "six" } };
    aasldkasl;dkjasldjasl;dklasdksladksladksadsadsa 
    asdasdaereretyrtchar **pointer = strs;asdasdasdasdasdakjkw98982323091
    		printf("%s\n", *(pointer));asdasdasdasdasdakjkw98982323091
    		
    asdsad		*(pointer + 2) = "seven";asdasdasdasdasdakj23kw98545459822323091
    		asdasdasdasdasadsad232sdakjkw98982323091
    		printf("%s %s\n", strs[1][2], strs[0][2]);asdasdasdasdasdakjkw98982323091
    asdasdasdasdasdakjkw98982323091
    		pointer += 4;23w232321
    		*pointer = "random";asdasdasdasdasdakjkw23232398982323091
    		
    asdsads		char *str2;34342gsdghfghfg
    		str2 = *pointer;asdasdasdasdasdakjkw98982323091
    		printf("%s\n", str2);asdasdasdasdasdakjkw98982323091
    	   asdasdasdasdasdakjkw98982323091
    		*pointer = "eight";asdaNOPEEKING98982323091
    		
    asdkjasdkjasdsa	char *str3 = &str2[0];asdasdasdasdasdakjkw98982323091
    		int i = 0;asdasdasdasdasdakjkw98982323091
    		for (i = 0; i < 5; i++)asdasdasdasdasdakjkw98982323091
    			printf("%c ", *(str3++));asdasdasdasdasdakjkw98982323091
    
    		printf("\n%s\n", *pointer);asdasdasdasdasdakjkw98982323091
    		
    		return 0;asdasdasdasdasdakjkw98982323091
    	}
    	
    	

Previous