34 + 7 - 8 + 2 =
and the program prints out the answer.
$ clang -o sol1 sol1.c $ ./sol1 34 + 7 - 8 + 2 = 35
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 typebool, 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?
$ 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!
struct
for this. However, remember
the struct gotcha
from last lecture!