-
Notifications
You must be signed in to change notification settings - Fork 0
/
34_atoi,itoa.c
143 lines (132 loc) · 3.07 KB
/
34_atoi,itoa.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
/*****************************************************************************************************************************
DOCUMENTATION:
NAME :V.KARTHIEKEYAN
DATE :8.07.2021
DESCRIPTION :Implement getword, atoi, itoa functions
OUTPUT :./a.out
1.getword
2.atoi
3.itoa
ENTER YOUR CHOICE:1
ENTER A WORD: Hello World
YOU ENTERED Hello and length is 5
DO YOU WANT TO CONTINUE(Y/y) :y
1.getword
2.atoi
3.itoa
ENTER YOUR CHOICE:2
ENTER A NUMBERIC STRING: -12345
STRING TO INTEGER IS -12345
DO YOU WANT TO CONTINUE(Y/y) :y
1.getword
2.atoi
3.itoa
ENTER YOUR CHOICE:3
ENTER A NUMBER: 1234
INTEGER TO STRING IS 1234
DO YOU WANT TO CONTINUE(Y/y) :n
*****************************************************************************************************************************/
#include<stdio.h>
#include<stdio_ext.h>
int get_word(char *);
int string_int(char *);
int is_digit(char);
int int_string(int, char *);
int main()
{
char choice;
do
{
int num,operation,string_length;
char char_string[200];
__fpurge(stdin);
printf("1.getword\n2.atoi\n3.itoa\nENTER YOUR CHOICE:");
scanf(" %d",&operation);
switch(operation)
{
case 1:
printf("ENTER A WORD: ");
__fpurge(stdin);
scanf("%[^\n]",char_string);
string_length=get_word(char_string);
printf("YOU ENTERED %s and length is %d",char_string,string_length);
break;
case 2:
printf("ENTER A NUMBERIC STRING: ");
__fpurge(stdin);
scanf("%[^\n]",char_string);
num=string_int(char_string);
printf("STRING TO INTEGER IS %d",num);
break;
case 3:
printf("ENTER A NUMBER: ");
__fpurge(stdin);
scanf(" %d",&num);
string_length=int_string(num,char_string);
printf("INTEGER TO STRING IS %s",char_string);
break;
default:
printf("ENTER VALID CHOICE OF OPERATION\n");
break;
}
__fpurge(stdin);
printf("\n\nDO YOU WANT TO CONTINUE(Y/y) :");
scanf(" %c",&choice);
}while(choice=='y' || choice=='Y');
}
int get_word(char *str)
{
int count=0;
while(str[count] && str[count] != 32 && str[count] != '\t') //cut the loop at ' ' or '\t'
count ;
str[count]='\0'; //join a '\0' at the end of first word
return count;
}
int string_int(char *str)
{
int num=0, flag=0;
for(int i=0;str[i]!='\0';i )
{
if(is_digit(str[i]))
{
num=(num*10) (str[i]-'0'); //convert char to int
}
else if(str[i]=='-' || str[i]==' ')
{
if(i==0 && str[i]=='-')
flag=1;
if(i>0) //if second time ' ' or '-' occurs break
break;
}
else
break;
}
if(flag==1)
return num*-1;
return num;
}
int int_string(int num, char *str)
{
int i=0,j,count=0,temp=num;
if(num<0)
{
str[i ]='-'; //if negative str[0]='-' && increment the i
num *= -1;
}
while(temp) //how many digits are in the number
{
i ;
temp=temp/10;
}
str[i]='\0'; //null at the end of 'i' characters
for(j=i-1;num!=0;j--) //for string i-1
{
str[j]=num 48; //get the digit convert to char
num=num/10; //reduce the last digit
}
return i;
}
int is_digit(char ch)
{
return ch>='0' && ch<='9'?1:0; //for digit check
}