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
- Now we have the condition that if Voltage is <230 && PF is < 0.85 do not log it. so the final output must be :
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