In: Computer Science
Write a program using C language that
-ask the user to enter their name or any other string (must be able to handle multiple word strings)
- capture the epoch time in seconds and the corresponding nanoseconds
- ask the user to type in again what they entered previously
- capture the epoch time in seconds and the corresponding nanoseconds
-perform the appropriate mathematical calculations to see how long it took in seconds and nanoseconds (should show to 9 decimal places)
- If the strings entered don't match, tell the user, do no calculations and exit the program
If you have any doubts, please ask in the comments, I will try to solve it as soon as possible. If you find my answer helpful, do UPVOTE.Thanks
Code:
#include <stdio.h>
#include <time.h>
#include <string.h>
int main()
{
//variables to store starting and ending time
time_t start, end;
//variables to store the string and its duplicate
char s[50], d[50];
printf("Enter the string: ");
//get the original string
gets(s);
printf("Enter the string again: ");
//clock() returns the number of clock ticks elapsed since the program was launched.
start = clock();
gets(d);
end = clock();
if (strcmp(s, d) == 0)
{
//to get the time we divide it by CLOCKS_PER_CYCLE
double timeElapsed = double(end - start) / double(CLOCKS_PER_SEC);
//we use %.9lf to print double with 9 decimal places
printf("Time taken in Seconds: %.9lf\n", timeElapsed);
printf("Time taken in Nanoseconds: %.9lf\n", timeElapsed * 1000000000);
}
else
printf("The strings do not match");
}
Code Snippet:
Please refer to the code snippet below for the indentation
Output:
#1:
#2: