Need help with my C homework Write a program which takes two

Need help with my C homework!

Write a program which takes two strings as input from the user. Ask the user to enter names in the first string and foods in the second string. In both the strings the names and foods are separated by space. Number of items in both lists can be different. Example Input Sequence Alpha Bravo Charlie delta Echo Pizza Burger Fries Tacos Pasta Write a function creates () which takes the above 2 strings and 2 integers as arguments. The first integer is used to pick a word from the first string and the second integer is used to pick a word from the second string. Use these 2 words to create a string which says \"World loves Word2\". Store this string in another argument. Choice of words from both the lists must not exceed their capacity which means if there are 6 words in the name string and 3 words in the food string, your choices for the first integer are restricted to 0 to 5 and choices for the second integer are restricted to 0 to 2. Example: creates (name, food, 0, 2, am) would produce the result ans=\"Alpha loves Fries\"; creates(name, food, 2, 4, ans) would produce the result ans=\"Charlie loves Pasta\"; Write a function find replace() which takes 3 strings as argument. The first string is the string you generated. If von find string 2 in string 1, then replace it with string 3, else display a message \"String not found!\" Example: str=\"Charlie loves Pasta\" find replace(str, \'\'loves\", \'\'hates\") would produce the result \"Charlie hates Pasta\" fireplace(str, \"likes\", \'\'doesn\'t like\") would produce the result String not found

Solution

#include<stdio.h>
#include<string>

#define ROW 100
#define LEN 50
int main()
{
   //create array of strings names andfood
   char names[ROW][LEN] = { \"Alpha\",\"Bravo\",\"Charlie\" ,\"Delta\" ,\"Echo\"};
   char food[ROW][LEN]= { \"Pizza\",\"Burger\",\"Fries\",\"Tacos\" ,\"Pasta\" };
   //declare a char array ans to hold ans from function call creates
   char ans[ROW][LEN];
   //declare str to hold string
   char str[]= \"Charlie loves Pasta\";
   //function declaration
   void creates(char name[][LEN], char food[][LEN], int i, int j, char ans[][LEN]);
   int findreplace(char str[],char *str1,char *str2);

   //call these functions
   creates(names,food,0,2,ans);
   printf(\"Ans = %s \ \",ans);

   creates(names,food,2,4,ans);
   printf(\"Ans = %s \ \",ans);

   //test find and replace
   if ( findreplace(str,\"loves\",\"hates\") < 0 )
   {
       printf(\"String not found\ \");
   }
   else
   {
       printf(\"str = %s \ \",str);
   }
   //test find and replace
   if ( findreplace(str,\"likes\",\"doesn\'t like\") < 0 )
   {
       printf(\"String not found\ \");
   }
   else
   {
       printf(\"str = %s \ \",str);
   }
}

void creates(char name[][LEN], char food[][LEN], int i, int j, char ans[][LEN])
{
   int k = 0 ,m = 0,l=0;
   char s1[LEN],s2[LEN];
   //search through name
   while(name[k])
   {
       if( k == i )
       {
           sprintf(s1,\"%s\",name[k]);
           break;
          
       }
       k++;
   }

   //search through food
   //reset array subscripts
   k = 0;

   while(food[k])
   {
       if( k == j )
       {
           sprintf(s2,\"%s\",food[k]);
           break;
       }
       k++;
   }
   sprintf(ans[m] ,\"%s loves %s\",s1,s2);
}

int findreplace(char str[],char *str1,char *str2)
{
   char buf[LEN];
   char *p;

   //if string is not in str return -1
   if(!(p = strstr(str, str1)))
       return -1;
   // Copy characters from \'str\' start to str1
   strncpy(buf, str, p-str);
   buf[p-str] = \'\\0\';

   sprintf(buf+(p-str), \"%s%s\", str2, p+strlen(str1));

   //copy from buf to original buffer
   strcpy(str,buf);

   return 0;
}

--------------------------------------------------------------------------------------------------

This is the output I got

Ans = Alpha loves Fries
Ans = Charlie loves Pasta
str = Charlie hates Pasta
String not found

Need help with my C homework! Write a program which takes two strings as input from the user. Ask the user to enter names in the first string and foods in the s
Need help with my C homework! Write a program which takes two strings as input from the user. Ask the user to enter names in the first string and foods in the s

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site