<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Math + CS = 11 &#187; K&amp;R2</title>
	<atom:link href="http://geoffsquared.com/tag/kr2/feed/" rel="self" type="application/rss+xml" />
	<link>http://geoffsquared.com</link>
	<description>Because Orange \gg Green</description>
	<lastBuildDate>Tue, 09 Mar 2010 20:04:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>It&#039;s My String&#8230;</title>
		<link>http://geoffsquared.com/2009/06/its-my-string/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://geoffsquared.com/2009/06/its-my-string/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 09:44:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[awesome]]></category>
		<category><![CDATA[bar codes]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Delicious Library 2]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[Geoff's Theorem]]></category>
		<category><![CDATA[K&R2]]></category>
		<category><![CDATA[orange]]></category>
		<category><![CDATA[Shoes]]></category>
		<category><![CDATA[Zappos]]></category>

		<guid isPermaLink="false">http://geoffsquared.com/?p=104</guid>
		<description><![CDATA[Okay, I just had way too much fun writing this so I thought I&#8217;d post it. It&#8217;s exercise 4-1 in K&#038;R2. It&#8217;s pretty much &#8220;write a function which returns the rightmost index of some character in some string.&#8221;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include &#60;stdio.h&#62;
#include &#60;string.h&#62;
&#160;
int strindex&#40;char s&#91;&#93;, int t&#41;;
&#160;
char mystring&#91;&#93; = &#34;This is my string and I can cry if [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, I just had way too much fun writing this so I thought I&#8217;d post it. It&#8217;s exercise 4-1 in K&#038;R2. It&#8217;s pretty much &#8220;write a function which returns the rightmost index of some character in some string.&#8221;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;string.h&gt;</span>
&nbsp;
<span style="color: #993333;">int</span> strindex<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> s<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #993333;">int</span> t<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">char</span> mystring<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;This is my string and I can cry if I want to.&quot;</span><span style="color: #339933;">;</span>
&nbsp;
main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> mychar <span style="color: #339933;">=</span> <span style="color: #ff0000;">'z'</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%c is at %d in %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> mychar<span style="color: #339933;">,</span> strindex<span style="color: #009900;">&#40;</span>mystring<span style="color: #339933;">,</span> mychar<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> mystring<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> strindex<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> s<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #993333;">int</span> t<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> strlen<span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&gt;=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>t <span style="color: #339933;">==</span> s<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #b1b100;">return</span> i<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>On a somewhat more serious note, I have finally bought some new shoes (yay?) from Zappos.com. They&#8217;re orange and like all orange things, they are good. <strong>Geoff&#8217;s Theorem: If something is orange, then it is good.</strong></p>
<p>Another cool thing that every Mac user should play around with is Delicious Library 2. If you have never heard of it, think of it as a digital book shelf where you can display books, movies, video games, electronics, ect. that you own. The best part is that it lets you use your iSight camera to scan in the bar codes for everything! I got it as part of the Macheist bundle and have never played with it until now and I gotta tell you, scanning everything in is way more fun than it should be.</p>
]]></content:encoded>
			<wfw:commentRss>http://geoffsquared.com/2009/06/its-my-string/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C</title>
		<link>http://geoffsquared.com/2009/06/c/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://geoffsquared.com/2009/06/c/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 06:47:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[K&R2]]></category>
		<category><![CDATA[language]]></category>

		<guid isPermaLink="false">http://geoffsquared.com/?p=92</guid>
		<description><![CDATA[I&#8217;ve decided that I want to learn C instead of Haskell and Python now and am using the book to learn from. I gotta say, I&#8217;m very impressed with K&#38;R2. Absolutely great. The best part about the book is that it contains many exercises at the end of each section which gives you practice writing [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve decided that I want to learn C instead of Haskell and Python now and am using <em>the book</em> to learn from. I gotta say, I&#8217;m very impressed with K&amp;R2. Absolutely great. The best part about the book is that it contains many exercises at the end of each section which gives you practice writing C programs. I just finished the 2-4 exercises and it was quite a challenge for some reason. The problem was to write a function that takes two strings and remembers the characters in one string from the other. I thought it&#8217;s worth posting my solution. I&#8217;m not sure how &#8216;good&#8217; my style is, but I&#8217;m following my style from K&amp;R so it can&#8217;t be very bad&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #993333;">void</span> squeeze<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #993333;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">char</span> s1<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;Hello, world!&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">char</span> s2<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;Hlo&quot;</span><span style="color: #339933;">;</span>
    squeeze<span style="color: #009900;">&#40;</span>s1<span style="color: #339933;">,</span> s2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> s1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* squeeze: deletes all characters in s2 from s1 */</span>
<span style="color: #993333;">void</span> squeeze<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> s1<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #993333;">char</span> s2<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">,</span> j<span style="color: #339933;">,</span> k<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> s1<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #ff0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #993333;">int</span> found <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>k <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> s2<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #ff0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #339933;">;</span> k<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>s1<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> s2<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                found <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>found<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            s1<span style="color: #009900;">&#91;</span>j<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> s1<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    s1<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The stuff in the main is just some testing that I did. From what I can tell, it seems to work as expected. Yay C!</p>
]]></content:encoded>
			<wfw:commentRss>http://geoffsquared.com/2009/06/c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
