The goal of today is to partner up and use what we learned about C from the last class to write some programs!

Problem 1

Write a C program that solves one of our classic early problems: The user enters input like 34 + 7 - 8 + 2 = and the program prints out the answer.
$ clang -o sol1 sol1.c
$ ./sol1
34 + 7 - 8 + 2 = 
35 

Problem 2

When you readin strings with scanf, you give scanf an array of chars to read into. Note you don't pass scanf a pointer to the char*, but just the char* itself ... that means no "&" for strings. Also, if we want to read in a string and the string isn't going to live for very long, we can use a static array.
char str[20];    // static array of length 20 (s is a char*)
scanf("%s",str); // no & here!	
printf("str = %s\n",str); // prints out string str
      

Note: what happens if the user enters a string of more than 20 characters?

Write a C program that reads in a string from the user and prints "palindrome" if it is a palindrome (reads the same backwards as forwards) and "not palindrome" otherwise. Note: you must make a function that tests whether you have a palendrome. In C++ this function would return type bool, but C does not have this type, so just have your function return an int, and make it 0 for false (not a palindrome) and 1 otherwise.
$ clang -o sol2 sol2.c 
$ ./sol2  
enter string (no spaces): racecar 
palindrome
$ ./sol2  
enter string (no spaces): raccar 
palindrome
$ ./sol2  
enter string (no spaces): rockercor 
not palindrome
$ ./sol2  
enter string (no spaces): solarlos 
not palindrome
$ ./sol2  
enter string (no spaces): amanaplanacanalpanama 
palindrome

After you get this working, try entering a very long string. What happens?

Problem 3

Write a C program that reads in points given as ordered pairs and prints out the point that is closest to the origin. Note: use functions to make this nice!
$ clang -o sol2 sol2.c -lm
$ ./sol2
(5,7) (4,8) (-5,5) (0,11) (6,3) (5,9) (12,12) ;
(6.000000,3.000000)
Note: To do this nicely with functions requires thinking about all the ways we've discussed C being different from C++. One thing to keep in mind: in C we use the return value of scanf to figure out whether or not our reads have been successful!
Note: Of course you will want a struct for this. However, remember the struct gotcha from last lecture!