-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.c
More file actions
239 lines (196 loc) · 5.41 KB
/
Copy pathstring.c
File metadata and controls
239 lines (196 loc) · 5.41 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
#include<stdio.h>
int main(){
char name[] = {'s','h','o','b','h','i','t','\0'};
char name1[] = "shobhit";
printf("%s \n",name);
printf("%s",name1);
return 0;
}
// Que.1--> Create a string firstname and last name to store details of usre and print all the characters using loop.
#include<stdio.h>
void printString(char arr[]);
int main(){
char firstname[] = "shobhit";
char lastname[] = "kumar";
printString(firstname);
printString(lastname);
return 0;
}
void printString(char arr[]) {
for (int i = 0; arr[i] !='\0'; i++){
printf("%c", arr[i]);
}
printf("\n");
}
// String Format Specifier-----------------------------------------
#include<stdio.h>
int main(){
char name[40];
scanf("%s",name);
printf("your name is : %s", name);
return 0 ;
}
// Que.--> Ask the user to enter their firstname and print it back to them.---------------
// Also try this with their fullname.
#include<stdio.h>
int main(){
char firstname[50];
scanf("%s",firstname);
printf("your name is : %s \n", firstname);
char fullname[150];
scanf("%s",fullname);
printf("your full name is : %s", fullname);
return 0 ;
}
// string function.----------------------------------------
#include<stdio.h>
int main(){
char str[100];
fgets(str, 100, stdin);
puts(str);
return 0 ;
}
// String using pointer.-----------------------------------------
#include<stdio.h>
int main(){
char *canchange = "hello world";
puts(canchange);
canchange = "hello";
puts(canchange);
}
// Make a program that inputs users name and prints its length.---------------------------------
#include<stdio.h>
int countLength(char arr[]);
int main(){
char name[100];
fgets(name, 100, stdin);
printf("Length is : %d", countLength(name));
return 0 ;
}
int countLength(char arr[]){
int count = 0;
for (int i = 0; arr[i]!= '\0'; i++){
count++;
}
return count - 1;
}
// standard library function.-----------------------------------------------
// print length.
#include<stdio.h>
#include<string.h>
int main(){
char name[100];
printf("Enter the characters : ");
scanf("%s" , name);
printf("Length is : %d", strlen(name));
return 0 ;
}
// print new string to old string.------------------------------------------
#include<stdio.h>
#include<string.h>
int main(){
char str1[] = "shobhit";
char str2[] = "kumar";
strcpy(str1 , str2);
puts(str1);
}
// concatenates first string with second string.----------------------------------
#include<stdio.h>
#include<string.h>
int main(){
char str1[] = "shobhit ";
char str2[] = "kumar";
strcat(str1 , str2);
puts(str1);
}
// print comperission of 2 strings in ASCII value (A=65, B=66 and soo on ). --------------------------
#include<stdio.h>
#include<string.h>
int main(){
char str1[] = "Apple";
char str2[] = "Banana";
printf("%d" , strcmp(str1 , str2));
}
// Que.--> Take a string input from the user using %c.------------------------------------------
#include<stdio.h>
#include<string.h>
int main(){
char str[1000];
char ch;
int i = 0;
while (ch !='\n'){
scanf("%c" , &ch);
str[i] = ch;
i++;
}
str[i] = '\0';
puts(str);
}
// Que.--> Find the salted form of a password entered by user if the salt is "123" and added at the end.
#include<stdio.h>
#include<string.h>
void salting(char password[]);
int main(){
char password[100];
scanf("%s", password);
salting(password);
}
void salting(char password[]){
char salt[] = "123";
char newpassword[200];
strcpy(newpassword, password); // newpassword = "test"
strcat(newpassword, salt); // newpassword = "test" + "123"
puts(newpassword);
}
// Que. --> Write a function named slice, which takes a string and returns a sliced string from index n to m.
#include<stdio.h>
#include<string.h>
void slice(char str[], int n, int m);
int main(){
char str[] = "helloworld";
slice(str, 3, 6);
}
void slice(char str[], int n, int m){
char newstr[100];
int j = 0;
for (int i = n; i <= m; i++, j++){
newstr[j] = str[i];
}
newstr[j] = '\0';
puts(newstr);
}
// Que. --> Write a function to count the occurence of vowels in a string.-------------------------------
#include<stdio.h>
#include<string.h>
int countVowels(char str[]);
int main(){
char str[] = "helloeworld";
printf("vowels are : %d", countVowels(str));
}
int countVowels(char str[]){
int count = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u'){
count++;
}
}
return count;
}
// Que. --> Check if a given character is present in a stirng or not.------------------------------
#include<stdio.h>
#include<string.h>
void checkchar(char str[], char ch);
int main (){
char str[] = "shobhitkumar";
char ch = 'a';
checkchar(str,ch);
}
void checkchar(char str[], char ch){
for (int i = 0; str[i] != '\0'; i++){
if (str[i] == ch){
printf("character is present");
return;
}
}
printf("character is not present");
}