Suppose we are using C strings. Show a declaration for a string that could conta
ID: 643272 • Letter: S
Question
Suppose we are using C strings.
Show a declaration for a string that could contain a
Assume that we have two C strings declared, say str1 and str2. word of maximum size 10.
Write a statement to read a word from the standard input stream (cin) and place it into the string word. You may assume that there are no blanks in the word.
Write a statement to print the size (length) of word.The maximum length of these strings is stored in the constant MAX_SIZE.
Write code to read a string from a file myin and place it into str1. Read until end-of-line is reached or until the length of the string has been reached.
Write a statement to copy the contents of str1 to str2.
Write a statement to compare the contents of str1 and str2. If they contain the same string, print "same" and print one of the strings.
*Write both using C language.
Explanation / Answer
hope this may helps you............
int main()
{
int i = 0;
int MAX_SIZE= 1000;
char* str1[10];
char* str2[10];
FILE *fp = fopen("myin .txt", "r");
FILE *fp2 = fopen("myin .txt", "r");
if (fp == 0){
fprintf(stderr, "Error while opening");
return 0;
}
words[i] = (char*)malloc(MAX_SIZE);
words2[i] = (char*)malloc(MAX_SIZE);
while (fgets(words[i], MAX_SIZE, fp)) {
i++;
words[i] = (char*)malloc(MAX_SIZE);
}
// reset i back to zero
i = 0;
while (fgets(words2[i], MAX_SIZE, fp2)) {
i++;
words2[i] = (char*)malloc(MAX_SIZE);
}
printf("Output: ");
srand(time(NULL));
int j = rand()%i;
int k = (j+1)%i;
fflush(stdout);
printf("%d - %s %d -%s", j, words[j], k, words[k]);
int x;
for(x = 0; x<i; x++){
free(words[x]);
free(words2[x]);
}
scanf("%d", x);
fclose(fp);
fclose(fp2);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.