Following are the conditions :
Take input of multiple strings from the user having same parameter. for Example : String 1 : Voltage 230,Current 3.14,PF 0.91,KW 1889.91 String 2 : Voltage 240,Current 2.98,PF 0.84,KW 1889.92
Then split the string separated by commas and store the splited sub strings and then split again separated by spaces. for example : Voltage 230 (splited sub strings) Current 3.14 PF 0.91
/* splitting the sub string from spaces */ Voltage Current PF KW 230 3.14 0.91 1889.91 240 2.98 0.84 1889.92
Voltage Current PF KW 230 3.14 0.91 1889.91
#include <stdio.h>
#include <string.h>
void main()
{
char str[55];
char *vol[5], *cur[5], *pf[5], *kw[5];
int i, k = 0, m;
char delim1[] = ",";
for (i = 0; i < 5; i++)
{
printf("Enter value : ");
gets(str);
char *tk1 = strtok(str, delim1);
while (tk1 != NULL) {
if (tk1[0] == 'v') {
vol[k] = strchr(tk1, ' ') + 1;
}
else if (tk1[0] == 'c') {
cur[k] = strchr(tk1, ' ') + 1;
}
else if (tk1[0] == 'p') {
pf[k] = strchr(tk1, ' ') + 1;
}
else if (tk1[0] == 'k') {
kw[k] = strchr(tk1, ' ') + 1;
}
tk1 = strtok(NULL, delim1);
}
k++;
}
printf("Voltage \tCurrent \t PF\t\t\t kW\n");
for (m = 0; m < 5; m++) {
printf("%s\t\t\t%s\t\t%s\t\t%s\n", vol[m], cur[m], pf[m], kw[m]);
}
return 0;
}
Expected Output :
Enter value : Voltage 230,Current 7.89,PF 0.91,KW 1289.33 (1st user input)
Enter value : Voltage 230,Current 3.20,PF 0.84,KW 1100.32 (2nd user input)
Enter value : Voltage 240,Current 4.78,PF 0.91,KW 1278.87 (3rd user input)
Enter value : Voltage 230,Current 7.45,PF 0.91,KW 1945.34 (4th user input)
Enter value : Voltage 210,Current 5.13,PF 0.81,KW 998.33 (5th user input)
Voltage Current PF kW
210 5.13 0.81 998.33 (latest string output i.e 5th string)
230 7.45 0.91 1945.34
240 4.78 0.91 1278.87
230 3.20 0.84 1100.32
230 7.89 0.91 1289.33 (oldest string output i.e 1st string)
Obtained Output :
Enter value : Voltage 230,Current 7.89,PF 0.91,KW 1289.33
Enter value : Voltage 230,Current 3.20,PF 0.84,KW 1289.32
Enter value : Voltage 240,Current 4.78,PF 0.91,KW 1278.87
Enter value : Voltage 230,Current 7.45,PF 0.91,KW 1945.34
Enter value : Voltage 210,Current 5.13,PF 0.81,KW 998.33
Voltage Current PF kW
210 5.13 0.81 998.33
210 5.13 0.81 998.33
210 5.13 0.81 998.33
210 5.13 0.81 998.33
210 5.13 0.81 998.33
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char input[100];
int index = 0;
float voltage, current, pf, KW;
float readings[100][4];
printf("INPUT FORMAT : Voltage 270.45,Current 70.89,PF 0.91,KW 128.33\n");
printf("Enter string of readings:\n");
for (int i = 0; i < 5; i++) {
fgets(input, 100, stdin);
sscanf(input, "Voltage %f, Current %f, PF %f,KW %f", &voltage, ¤t,
&pf, &KW);
if (pf > 0.75 && (voltage > 260 || voltage < 180) &&
(current > 60 || current < 0)) {
readings[index][0] = voltage;
readings[index][1] = current;
readings[index][2] = pf;
readings[index][3] = KW;
index++;
} else {
printf("Given values does not match the following condition\n1.PF> 0.75\n2.180 <Voltage< 260\n3. 0 <Current< 60");
}
}
printf("Voltage\t\tCurrent\t\tPF\t\t\tKW\n");
for (int i = index - 1; i >= 0; i--) {
printf("%.2f\t\t%.2f\t\t%.2f\t\t%.2f\n", readings[i][0], readings[i][1],
readings[i][2], readings[i][3]);
}
return 0;
}
**Expected and Obtained Output :**
INPUT FORMAT : Voltage 270.45,Current 70.89,PF 0.91,KW 128.33
Enter string of readings:
Voltage 270.45,Current 70.89,PF 0.91,KW 128.33 // first input
Voltage 170.45,Current 70.89,PF 0.81,KW 128.33 // second input
Voltage 270.45,Current 55.89,PF 0.91,KW 128.33 /* third input with
wrong current
value*/
Given values does not match the following condition
1.PF> 0.75 /* Error because of current*/
2.180 <Voltage< 260
3.Current>60
Voltage 270.45,Current 70.89,PF 0.71,KW 128.33 /*fourth input with
wrong PF value*/
Given values does not match the following condition
1.PF> 0.75 /* Error because of PF*/
2.180 <Voltage< 260
3.Current>60
Voltage 290.45,Current 75.89,PF 0.91,KW 128.33 //fifth input
Voltage Current PF KW
290.45 75.89 0.91 128.33 //output of 5th input(Recent)
170.45 70.89 0.81 128.33 //output of second input
270.45 70.89 0.91 128.33 //output of first input(oldest)
NOTE : Output for 3rd and 4th didn't log because of error.