-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.html
More file actions
446 lines (360 loc) · 17.2 KB
/
string.html
File metadata and controls
446 lines (360 loc) · 17.2 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
<title>CodingElite</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark navm">
<div class="container-fluid">
<a class="navbar-brand" href="index.html">CodingElite</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link " aria-current="page" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link " href="/codingelite.org/blog.html">Blog</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link active dropdown-toggle" href="#" id="navbarDropdown" role="button"
data-bs-toggle="dropdown" aria-expanded="false">
C Tutorials
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="/codingelite.org/introduction.html">introduction</a></li>
<li><a class="dropdown-item" href="/codingelite.org/branching.html">Branching Statements</a></li>
<li><a class="dropdown-item" href="/codingelite.org/looping.html">looping statements</a></li>
<li><a class="dropdown-item" href="/codingelite.org/function.html">Function</a></li>
<li><a class="dropdown-item" href="/codingelite.org/array.html">Array</a></li>
<li><a class="dropdown-item" href="/codingelite.org/string.html">string</a></li>
<li><a class="dropdown-item" href="/codingelite.org/pointer.html">pointers</a></li>
<li><a class="dropdown-item" href="/codingelite.org/structure.html">structures</a></li>
<li>
<hr class="dropdown-divider">
</li>
<li><a class="dropdown-item" href="/codingelite.org/blog.html">More Topics</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="/codingelite.org/book.html">Books</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/codingelite.org/about.html">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/codingelite.org/contact.html">Contact us</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="bg-light">
<div class="container bg-c">
<h1 class="hed hedi-h1"><strong> String </strong></h1>
<h3>Introduction to string</h3>
<p>In C programming, a string is a sequence of characters terminated with a null character. String is an one
dimentional character array. Strings are one-dimensional array of type characters terminated by a null
character '\0'. This null character indicates the end of the string. Strings are always enclosed by double
quotes. Whereas, character is enclosed by single quotes in C.
</p>
<h5>Example</h5>
<div class="row">
<div class="col-md-4">
<pre class="pre-bl">char c[ ]="c string";</pre>
</div>
</div>
<p>It is represent in string as follows</p>
<div class="row">
<div class="col-md-6">
<img class="img-fluid img-p" src="img/stringf.png" alt="string">
</div>
</div>
<h5>Declaration</h5>
<div class="row">
<div class="col-md-4">
<pre class="pre-bl">char a[5];</pre>
</div>
</div>
<p>Here, we declared a string of 5 characters.</p>
<div class="row">
<div class="col-md-4">
<img class="img-fluid img-p img-b" src="img/stringsec.png" alt="string">
</div>
</div>
<br>
<h4>String functions and its purposes:</h4>
<h5><b> 1.strlen(str); </b></h5>
<p>Returns the length of string str.</p>
<h6><b>Example</b></h6>
<div class="col-md-8">
<pre class="pre-bl" title="">
// c program
// example of strlen() function.
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<string.h></span>
</span>
int main()
{
char ch[]={'h', 'e', 'l', 'l', 'o','\0'};
printf("Length of string is: %d", strlen(ch));
return 0;
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
Length of string is: 5
</pre>
</div>
<h5><b> 2.strcpy(str1,str2);</b></h5>
<p> Copies string str2 into str1.</p>
<h6><b>Example</b></h6>
<div class="col-md-8">
<pre class="pre-bl" title="">
// C program
// example of strcpy() function.
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<string.h></span>
</span>
int main ()
{
char str1[]="Hello Guys!";
char str2[] = "C programming";
strcpy(str2, str1);
printf ("str1: %s\nstr2: %s, str1, str2,);
return 0;
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
str1: Hello Guys!
str2: Hello Guys!
</pre>
</div>
<h5><b>3.strcmp(Str1, Str2);</b></h5>
<p>Returns 0 if Str1 and Str2 are the same; less than 0 if Str1 < Str2; greater than 0 if Str1>Str2.</p>
<h6><b>Example</b></h6>
<div class="col-md-8">
<pre class="pre-bl" title="">
// C program
// example of strcmp() function.
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<string.h></span>
</span>
int main()
{
char Str1[] = "C programming";
char Str2[] = "C programming";
// Using strcmp()
int k = strcmp(Str1, Str2);
if (k==0)
printf("Strings are equal");
else
printf("Strings are unequal");
printf("\nValue returned by strcmp() is: %d" , k);
return 0;
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
Strings are equal
Value returned by strcmp() is: 0
</pre>
</div>
<h5><b>4.strcat(str1, str2);</b></h5>
<p>Concatenates string str2 onto the end of string str1.</p>
<h6><b>Example</b></h6>
<div class="col-md-8">
<pre class="pre-bl" title="">
// C program
// example of strcat() function.
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<string.h></span>
</span>
int main()
{
// Take any two strings
char str1[50] = "efghi";
char str2[50]= "abcd";
strncat(str2, str1);
// Prints the string
printf("Source string : %s\n", str1);
printf("Destination string : %s", str2);
return 0;
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
Source string : efghi
Destination string : abcdefghi
</pre>
</div>
<h5><b>5.strrev(str1);</b></h5>
<p>Reverse the string str1.</p>
<h6><b>Example</b></h6>
<div class="col-md-8">
<pre class="pre-bl" title="">
// C program
// example of strrev() function.
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<string.h></span>
</span>
int main()
{
char str1[50] = "hello";
printf("The given string is =%s\n",str1);
printf("After reversing string is =%s",strrev(str1));
return 0;
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
The given string is =hello
After reversing string is =olleh
</pre>
</div>
<h5><b> 6.strchr(str1, ch);</b></h5>
<p>Returns a pointer to the first occurrence of character ch in string str1.</p>
<h6><b>Example</b></h6>
<div class="col-md-8">
<pre class="pre-bl" title="">
// C program
// example of strchr() function.
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<string.h></span>
</span>
int main () {
char str1[] = "programming";
char ch = 'g';
char *p;
p = strchr(str1, ch);
printf("String starting from %c is: %s", ch, p);
return 0;
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
String starting from g is: gramming
</pre>
</div>
<h5><b>7.strstr(str1, str2);</b></h5>
<p>Returns a pointer to the first occurrence of string str2 in string str1.</p>
<h6><b>Example</b></h6>
<div class="col-md-8">
<pre class="pre-bl" title="">
// C program
// example of strstr() function.
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<string.h></span>
</span>
int main()
{
// Take any two strings
char str1[] = "learnCprogramming";
char str2[] = "Cpro";
char* p;
// Find first occurrence of str2 in str1
p = strstr(str1, str2);
// Prints the result
if (p) {
printf("String found\n");
printf("First occurrence of string '%s' in '%s' is '%s'", str2, str1, p);
} else
printf("String not found\n");
return 0;
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
String found
First occurrence of string 'Cpro' in 'learnCprogramming' is 'Cprogramming'
</pre>
</div>
<h5><b>Following example uses some of the above string functions</b> </h5>
<div class="col-md-8">
<pre class="pre-bl" title="">
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<conio.h></span>
</span>
int main () {
char s1[12] = "Hello";
char s2[12] = "World";
char s3[12];
int len ;
/* copy s1 into s3 */
strcpy(s3, s1);
printf("strcpy( s3, s1) : %s\n", s3 );
/* concatenates s2 and s3 */
strcat( s1, s2);
printf("strcat( s1, s2): %s\n", s1 );
/* total lenghth of s1 after concatenation */
len = strlen(s1);
printf("strlen(s1) : %d\n", len );
return 0;
}
</pre>
<p><b>Output-</b></p>
<pre class="pre-bl" title="">
strcpy( s3, s1) : Hello
strcat( s1, s2): HelloWorld
strlen(s1) : 10
</pre>
</div>
<h5>Functions used to read and write string </h5>
<ul>
<li>You can use the scanf() function to read a string. The scanf() function
reads the sequence of
characters until it encounters space or newline or tab etc.</li>
<li>We have used fgets() function to read a string from the user.</li>
<li>To print the string, we have used puts().</li>
</ul>
<h5>Examples For Practice</h5>
<ol>
<li>Implement C program which reads and displays your name on screen.</li>
<li>Implement C program to swap two strings.</li>
<li>Implement C program to find the length of the string.</li>
<li>Implement C program to calculate the blank spaces in string.</li>
<li>Implement C program for deciding whether given string is palindrome or
not. (Use string handling functions)</li>
</ol>
<a href="/codingelite.org/array.html"> <button type="button" class="btn btn-primary btn-m"> <<Previous</button> </a>
<a href="/codingelite.org/pointer.html"> <button type="button" class="btn btn-primary btn-m btn-r">Next >></button></a>
</div>
</div>
<footer class="page-footer font-small blue navbar-dark bg-light footer-ptt">
<div class="footer-copyright text-center py-3">
© Copyright 2021. All Right Reserved. | Designed by the <a href="/codingelite.org/about.html">CodingElite team</a>. |
<a class="color-b" href="/codingelite.org/privacyPolicy.html">Privacy policy</a> | <a class="color-b" href="/codingelite.org/contact.html">Contact us</a>
</div>
</footer>
<!-- Optional JavaScript; choose one of the two! -->
<!-- Option 1: Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW"
crossorigin="anonymous"></script>
<!-- Option 2: Separate Popper and Bootstrap JS -->
<!--
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js" integrity="sha384-q2kxQ16AaE6UbzuKqyBE9/u/KzioAlnx2maXQHiDX9d4/zp8Ok3f+M7DPm+Ib6IU" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.min.js" integrity="sha384-pQQkAEnwaBkjpqZ8RU1fF1AKtTcHJwFl3pblpTlHXybJjHpMYo79HY3hIi4NKxyj" crossorigin="anonymous"></script>
-->
</body>
</html>