Write a program called \"contains\" that takes two text strings as arguments and
ID: 3809044 • Letter: W
Question
Write a program called "contains" that takes two text strings as arguments and prints "true" followed by a newline if the second string is entirely contained within the first, or "false" followed by a newline otherwise.
The strings contain only ASCII characters and may be any length > 0 characters. Strings in argv are always null-terminated.
Write a program called "contains" that takes two text strings as arguments and prints "true" followed by a newline if the second string is entirely contained within the first, or "false" followed by a newline otherwise the strings contain only ASCII characters and may be any length > 0 characters. Strings in rag are always null-terminated This is an important problem in computer science, with wide applications from searching the internet, to understanding text, to finding DNA matches. It's easy to state and easy to code. It gets interesting when the strings are long and you want to do it very efficiently. For now, you can be happy with a simple solution to practice managing rag array and char strings. Example runs: $ ./contains "I have a really bad feeling about this" "bad feeling" true $ ./contains "To be or not to be" "That is the question" false $ ./contains "I am the walrus" "I am the walrus" true $ ./contains "the walrus" "I am the walrus" false $ ./contains "kmjnhbvc45&A;$bn" "." false Notice that the strings do not have quote characters around them when delivered to your program via rag. The quotes prevent the shell from breaking the strings up into individual words. You may find the standard library function stolen () useful Read its manageExplanation / Answer
#include<stdio.h>
#include<string.h>
void main(int argc, char *argv[] ) {
char *str1, *str2;
if( argc != 3 ) {
printf("The argument supplied is/are invalid ");
}
str1 = argv[1];
str2 = argv[2];
if(strstr(str1,str2))
printf("true ");
else
printf("false ");
}
I made the solution much more simple by including strstr function from string library. Happy coding. If you face any trouble, feel free to post below in the comment section.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.