-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathCP5CharArraygetdata.C
More file actions
49 lines (48 loc) · 1.29 KB
/
CP5CharArraygetdata.C
File metadata and controls
49 lines (48 loc) · 1.29 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
/* Title: WAP for getting characters from user and store in character array;
Programmer: Hitesh Patel Year/Div: FYBCA-1 RollNo: 999
Date: October 5, 2023
CP5CharArraygetdata.C
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
system("cls");
int i=0,n;
bool flag=true;
char cname[100], userchar;
// //1 approach get user data as characters fixed size loop
// printf("Enter single characters of name[amrolibca] as input:\n");
// for (i = 0; i < 9; i++)
// {
// scanf("\n%c",&cname[i]);
// }
// // terminating array with '\0' manually
// cname[i]= '\0';
// printf("Your created array of characters {String} = %s\n",cname);
// for (i = 0; i < 10; i++)
// {
// printf("%d\t",cname[i]);
// }
//2 approach of getting characters until Enter is pressed
printf("Enter characters -press Enter to finish:\n");
//Read characters until Enter is pressed
while ((userchar = getchar()) != '\n' && i < sizeof(cname) - 1)
{
cname[i++] = userchar;
}
//3 approach using boolean flag.
// while(flag)
// {
// userchar=getchar();
// if(userchar=='\n'){
// flag = false;
// }
// cname[i++]=userchar;
// }
// Null-terminate the character array
cname[i] = '\0';
// Display the character array
printf("Character array: %s\n", cname);
}