-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathString-examples.c
459 lines (336 loc) · 7.29 KB
/
String-examples.c
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
447
448
449
450
451
452
453
454
455
456
457
458
459
#include<stdio.h>
#include<string.h>
int main()
{
char A[] = "Vipin is my name";
char B[20] = "Empty :)";
void copy( char [] , char [] );
printf("Values before changes : \n\n");
puts(A);
puts(B);
copy(A,B);
printf("\nValues after changes : \n\n");
puts(A);
puts(B);
return 0;
}
void copy( char x[] , char y[])
{
int i;
for( i = 0 ; x[i] != 0 ; i++ )
// Here 0 condiction repersent NULL
{
y[i] = x[i];
}
y[i] = 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char A[] = "Vipin is my name";
int length_of_String;
int length( char [] );
length_of_String = length( A );
printf("Length of A is : %d.\n",length_of_String);
return 0;
}
int length( char x[] )
{
int i;
for( i = 0 ; x[i] != 0 ; i++ );
// This ';' will show that we are not writting anything in for loop
return --i;
}
#include<stdio.h>
#include<string.h>
int main()
{
char A[] = "Vipin is my name";
int number_or_vowels;
int count_vowel( char [] );
number_or_vowels = count_vowel( A );
printf("Numbers of vowels in A is : %d.\n",number_or_vowels);
return 0;
}
int count_vowel( char x[] )
{
int i;
int count;
for( i = 0, count = 0 ; x[i] != 0 ; i++ )
// Here 0 condiction repersent NULL
{
if ( x[i] == 'a' || x[i] == 'e' || x[i] == 'i' || x[i] == 'o' || x[i] == 'u' )
count++;
}
return count;
}
#include<stdio.h>
#include<string.h>
int main()
{
char A[50] = "my name is ";
char B[] = " Kumar Vipin Yadav";
void concate_string( char [] , char[] );
printf("Value of A before calling our function : \n\n");
puts( A );
concate_string( A , B );
printf("Value of A after calling our function : \n\n");
puts( A );
return 0;
}
void concate_string( char x[] ,char y[] )
{
int i;
int len;
for( i = 0 ; x[i] != 0 ; i++ );
len = --i;
for ( i = 0 ; y[i] != 0 ; i++ )
x[len+i] = y[i];
x[len+i] = 0;
// here we add NULL in last of array A
}
#include<stdio.h>
#include<string.h>
int main()
{
char A[50] = "my name is Vipin";
int words;
int counting_words( char[] );
words = counting_words( A );
printf("Number of word in our String is %d.\n",words );
return 0;
}
int counting_words( char y[] )
{
int i;
int count;
for ( i = 0, count = 0 ; y[i] != 0 ; i++ )
{
if ( y[i] == ' ' )
count++;
}
return ++count;
}
#include<stdio.h>
#include<string.h>
int main()
{
char A[50] = "my name is Vipin";
void print_alphabets_spaces_digites_symboles( char [] );
print_alphabets_spaces_digites_symboles( A );
return 0;
}
void print_alphabets_spaces_digites_symboles( char y[] )
{
int i;
int space,alphabets,digit,symoble;
space = 0 , alphabets = 0 , digit = 0 , symoble = 0 ;
for ( i = 0 ; y[i] != 0 ; i++ )
{
if ( y[i] == ' ' )
space++;
else if ( y[i] >= 'a' && y[i] <= 'z' || y[i] >= 'A' && y[i] <= 'Z' )
alphabets++;
else if ( y[i] >= '0' && y[i] <= '9' )
digit++;
else
symoble++;
}
printf("Alphabets = %d, Digit = %d, Space = %d and Symbole = %d.",alphabets,digit,space,symoble);
}
#include<stdio.h>
#include<string.h>
int main()
{
char A[50] = "My Name Is VIPIN";
void lower( char [] );
lower( A );
puts( A );
return 0;
}
void lower( char y[] )
{
int i;
for ( i = 0 ; y[i] != 0 ; i++ )
{
if ( y[i] >= 'a' && y[i] <= 'z' || y[i] == ' ')
continue;
else
y[i] = y[i] + 32 ;
// because ASCII value of upper case alphabets are 32 less then uppercase.
}
}
#include<stdio.h>
#include<string.h>
int main()
{
char A[50] = "My Name Is VIPIN";
void upper( char [] );
upper( A );
puts( A );
return 0;
}
void upper( char y[] )
{
int i;
for ( i = 0 ; y[i] != 0 ; i++ )
{
if ( y[i] >= 'A' && y[i] <= 'Z' || y[i] == ' ')
continue;
else
y[i] = y[i] - 32 ;
// because ASCII value of lower case alphabets are 32 greater then uppercase.
}
}
#include<stdio.h>
#include<string.h>
int main()
{
char A[] = "My Name Is VIPIN";
char B[] = "My Name Is VIPIN";
int res;
int compare( char [] , char [] );
res = compare( A , B );
if ( res == 0 )
printf("Both strings are equale.");
else if ( res > 0 )
printf("Strings A is greater.");
else
printf("Strings B is greater.");
return 0;
}
int compare( char x[] , char y[])
{
int i;
for ( i = 0 ; x[i] != 0 || y[i] != 0 ; i++ )
{
if ( x[i] != y[i] )
return x[i]-y[i];
}
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char A[] = "My Name Is VIPIN";
char B[50];
void revers_in_another( char [] , char [] );
revers_in_another( A , B );
puts(A);
puts(B);
return 0;
}
void revers_in_another( char x[] , char y[])
{
int i,j;
for ( i = 0 ; x[i] != 0 ; i++ );
i -= 1; // because we are not taking NULL which was in last
for ( j = 0 ; j <= i ; j++ )
y[j] = x[i-j];
y[j] = 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char A[] = "My Name Is VIPIN";
char B[50];
void revers_in_itself( char [] );
printf("A before calling function. \n\n");
puts(A);
revers_in_itself( A );
printf("A after calling function. \n\n");
puts(A);
return 0;
}
void revers_in_itself( char x[])
{
int i,j,temp;
for ( i = 0 ; x[i] != 0 ; i++ );
i -= 1;
for( j = 0 ; j <= i ; j++, i-- )
{
temp = x[j];
x[j] = x[i];
x[i] = temp;
}
}
#include<stdio.h>
#include<string.h>
int main()
{
char A[] = "VIPIN";
int res;
int palandrome( char [] );
res = palandrome( A );
if ( res == 1 )
printf("Yes String is palandrome.\n");
else
printf("No String is not palandrome.\n");
return 0;
}
int palandrome( char x[])
{
int i,j;
for ( i = 0 ; x[i] != 0 ; i++ );
i -= 1;
for( j = 0 ; j <= i ; j++, i-- )
{
if ( x[i] != x[j] )
return 0;
}
return 1;
}
#include<stdio.h>
#include<string.h>
int main()
{
char A[] = "Mohan Das Karam Chand Gandhi";
void abbrivation( char [] );
abbrivation( A );
return 0;
}
void abbrivation( char x[] )
{
int i,j;
i = 0 , j = 0 ;
do
{
if ( x[i] == ' ' )
{
printf("%c. ", x[j]);
j = i+1;
}
i++;
}while( x[i] != 0 );
for ( ; x[j] != 0 ; j++ )
printf("%c",x[j]);
}
#include<stdio.h>
#include<string.h>
int main()
{
char A[] = "My Name Is Vipin";
void remove_vowels( char [] );
printf("A before calling function\n");
puts(A);
remove_vowels( A );
printf("\nA after calling function\n");
puts(A);
return 0;
}
void remove_vowels( char x[] )
{
int i,j;
for ( i = 0 ; x[i] != 0 ; i++ )
{
if( x[i] == 'a' || x[i] == 'e' || x[i] == 'i' || x[i] == 'o' || x[i] == 'u' )
{
for ( j = i ; x[j] != 0 ; j++ )
x[j] = x[j+1];
i--;
}
}
}