Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ONLY USE C LANGUAGE???? Let\'s make our own simple twitter... A \"tweet\" is a s

ID: 3698636 • Letter: O

Question

ONLY USE C LANGUAGE???? Let's make our own simple twitter... A "tweet" is a special object that is the fundamental building block of Twitter If you are curious about the composition of a tweet you can familiarize yourself with the developer pages available here: https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/tweet-object You can see that the tweet is quite a complex object, which is why we would focus instead on a much simpler version called "Simple Tweet". It is a good start for us to create a simple twitter application. A simple tweet will be composed of the following int; which uniquely identifies the simple tweet charl51]; that would store the UTC time and date. Note that UTC is the coordinated universal time, which is a common standard on the Internet. char[141];allow up to 140 characters. char[21]; the user name of the person who posted the tweet id created_at text user You are asked to create a structure definition for a simple tweet called "tweet". You will then need to create a simple application with the following menu functionality: 1. 2. 3. 4. 5. 6. Create a new tweet Search tweets Display tweets Save tweets to file Load tweets from file Exit When the user first starts the program he/she is prompted to enter his/her user name The menu is then displayed 1. Create a new tweet This option will automatically generate the next highest value for an id, the locate date/time for the created at in UTC, and prompt the user for the tweet's text. The new record is stored at the next available location in the array. If the array has reached capacity then no new records may be added 2. Search tweets This option will search all tweets' text for occurrences of a certain keyword string entered form the user It will search the text of each tweet, then display all the tweets that contained this keyword. 3. Display tweets This option will display to the console screen all the tweets.

Explanation / Answer

Code

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <time.h>

#define CAPACITY 10

struct tweet

{

int id;

char created_at[51];

char text[141];

char user[21];

};

int create_tweet(struct tweet tweets[], int n, char *text)

{

if (n == CAPACITY)

return 0;

time_t mytime;

struct tm *ptm;

time(&mytime); // Get local time in time_t

ptm = gmtime(&mytime); // Find out UTC time

time_t utctime = mktime(ptm); // Get UTC time as time_t

tweets[n].id = n + 1;

strcpy(tweets[n].created_at, ctime(&utctime));

tweets[n].created_at[strlen(tweets[n].created_at)-1] = '';

strcpy(tweets[n].text, text);

return 1;

}

void display(struct tweet tweets[], int n)

{

int i = 0;

for (i = 0; i < n; i++)

{

printf("ID: %d Created At: %s Text:%s User: %s ",

tweets[i].id, tweets[i].created_at, tweets[i].text,

tweets[i].user);

}

}

int substring(char str, char word){

int len = strlen(str);

int w_len = strlen(word)

;

int i, j;

for(i=0; i<len-w_len+1;i++){

if(word[0]==str[i]){

int c=1;

for(j=1; j<w_len; j++){

if(str[i+j]==word[j])

c++;

}

if(c==w_len)

return 1;

}

}

return 0;

}

void search_tweets(struct tweet tweets[], int n)

{

char word[255];

printf("Enter word to search: ");

scanf(" %[^ ]s", word);

int i, c=0;

for(i=0; i<n; i++){

if(substring(tweets[i].text, word)){

printf("ID: %d Created At: %s Text:%s User: %s ",

tweets[i].id, tweets[i].created_at, tweets[i].text,

tweets[i].user);

c++;

}

}

if(c==0){

printf("No tweets found with word %s ", word);

}

}

void save_to_file(struct tweet tweets[], int n)

{

char filename[255];

printf("Enter filename: ");

scanf("%s", filename);

FILE *fp;

fp = fopen(filename, "a");

if (fp == NULL)

printf("Unable to create...");

else

{

int i = 0;

for (i = 0; i < n; i++)

{

fprintf(fp, "%s %s %s ", tweets[i].created_at,

tweets[i].text, tweets[i].user);

}

}

fclose(fp);

}

int load_tweets(struct tweet tweets[])

{

char filename[255];

printf("Enter filename: ");

scanf("%s", filename);

FILE *fp = fopen(filename, "r");

if (fp == NULL)

return -1;

int i = 0;

int inp = fscanf(fp, "%[^ ]s", tweets[i].created_at);

while (inp!=-1)

{

fscanf(fp, " %[^ ]s", tweets[i].text);

fscanf(fp, " %[^ ]s", tweets[i].user);

tweets[i].id = i + 1;

i++;

if (i == CAPACITY)

{

break;

}

inp = fscanf(fp, " %[^ ]s", tweets[i].created_at);

}

return i;

}

int main()

{

char name[255];

printf("Enter your name: ");

scanf("%[^ ]s", name);

int choice, n = 0;

struct tweet tweets[CAPACITY];

do

{

printf(" **** MENU ******* ");

printf("1. Create a new tweet ");

printf("2. Search tweets ");

printf("3. Display tweets ");

printf("4. Save tweets to file ");

printf("5. Load tweets from file ");

printf("6. Exit ");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice)

{

case 1:

{

char text[141];

printf("Enter tweet's text: ");

scanf(" %[^ ]s", text);

if (create_tweet(tweets, n, text))

{

strcpy(tweets[n].user, name);

n++;

}

else

{

printf("Max capacity reached.... ");

}

}

break;

case 2:

search_tweets(tweets, n);

break;

case 3:

display(tweets, n);

break;

case 4:

save_to_file(tweets, n);

break;

case 5:

{

int t = load_tweets(tweets);

if (n == -1)

printf("Unable to open tweet file");

else

{

n = t;

printf("Successfully %d readed ", t);

}

}

break;

case 6:

printf("Thank you! ");

exit(1);

break;

}

} while (choice != 6);

return 0;

}