forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathstrings.c.html
36 lines (35 loc) · 4.28 KB
/
strings.c.html
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
30
31
32
33
34
35
36
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="GNU source-highlight
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite">
<title>strings.c</title>
</head>
<body style="background-color:white">
<pre><b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><stdlib.h></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><string.h></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><stdio.h></span>
<span style="color:#009900">int</span> <b><span style="color:#000000">main</span></b> <span style="color:#990000">(</span><span style="color:#009900">int</span> argc<span style="color:#990000">,</span> <span style="color:#009900">char</span> <span style="color:#990000">**</span>argv<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<span style="color:#009900">int</span> i <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<i><span style="color:#9A1900">// allocate a space in memory for result</span></i>
<span style="color:#009900">char</span> <span style="color:#990000">*</span>result <span style="color:#990000">=</span> <span style="color:#990000">(</span><span style="color:#009900">char</span> <span style="color:#990000">*)</span> <b><span style="color:#000000">malloc</span></b> <span style="color:#990000">(</span><b><span style="color:#0000FF">sizeof</span></b> <span style="color:#990000">(*</span>result<span style="color:#990000">));</span>
<span style="color:#990000">*</span>result <span style="color:#990000">=</span> <span style="color:#FF0000">'</span><span style="color:#CC33CC">\0</span><span style="color:#FF0000">'</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">while</span></b> <span style="color:#990000">(</span>i <span style="color:#990000"><</span> argc<span style="color:#990000">)</span> <span style="color:#FF0000">{</span> <i><span style="color:#9A1900">// while there are still arguments</span></i>
<span style="color:#009900">char</span> <span style="color:#990000">*</span>s <span style="color:#990000">=</span> <span style="color:#990000">(</span><span style="color:#009900">char</span> <span style="color:#990000">*)</span> <b><span style="color:#000000">malloc</span></b> <span style="color:#990000">(</span><b><span style="color:#0000FF">sizeof</span></b> <span style="color:#990000">(*</span>s<span style="color:#990000">)</span> <span style="color:#990000">*</span>
<span style="color:#990000">(</span><b><span style="color:#000000">strlen</span></b><span style="color:#990000">(</span>result<span style="color:#990000">)</span> <span style="color:#990000">+</span> <b><span style="color:#000000">strlen</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span>i<span style="color:#990000">])</span> <span style="color:#990000">+</span> <span style="color:#993399">1</span><span style="color:#990000">));</span>
<b><span style="color:#000000">strcpy</span></b> <span style="color:#990000">(</span>s<span style="color:#990000">,</span> result<span style="color:#990000">);</span>
<i><span style="color:#9A1900">// add "free(result); here...</span></i>
<b><span style="color:#000000">strcat</span></b> <span style="color:#990000">(</span>s<span style="color:#990000">,</span> argv<span style="color:#990000">[</span>i<span style="color:#990000">]);</span>
<i><span style="color:#9A1900">// ... or here</span></i>
result <span style="color:#990000">=</span> s<span style="color:#990000">;</span>
i<span style="color:#990000">++;</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#000000">printf</span></b> <span style="color:#990000">(</span><span style="color:#FF0000">"Concatenation: %s</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">,</span> result<span style="color:#990000">);</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
</pre>
</body>
</html>