Question

In: Computer Science

#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int...

#include <cstring>
#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
  
    const int SIZE = 20;
    char str[SIZE];
    char str1[SIZE];
    int n;
    int k =1;
  
    printf("Enter a word: \n");
    fgets(str,SIZE,stdin);
    printf("Enter another word: \n");
    fgets(str1,SIZE,stdin);
  
    if (str1[strlen(str1) - 1] == '\n')
    {
        str1[strlen(str1)-1] = '\0';
    }
    if (str[strlen(str) - 1] == '\n')
    {
        str[strlen(str)-1] = '\0';
    }
  
  
    printf("Enter a number between 1 and 20: \n");
    scanf("%d",&n);
    strcat(str, " ");
    strncat(str, str1,n);
    printf("%s", str);
  
    printf("\n");
  
    str[0] = toupper(str[0]);
    for (int i = 1; i < strlen(str); i++)
    {
        str[i] = tolower(str[i]);
        if(str[i-1] == ' ')
        {
            str[i]= toupper(str[i]);
        }
    }
    for (int j = 1; j < strlen(str); j ++)
    {
        for (int i = 1; i < strlen(str); i++)
        {
           // if(str[i]!= '\0')
            {
                if(!strstr(str, "here"))
                {
                    k++;
                 
                }
              if(strchr(str,'\0'))
                {
                    break;
                }
              
           }
        }
       
      
      
    }
    cout << "Found "<< k << " words 'here' in: \n" << str;
  
    return 0;
}

================================================================================

so im practicing working on c++ with c-strings and i when i compile and run this, at the end the loop iterates one time for the entire length of the cstring and i cannot figure out how to end it after finding the 'k' amount of times the word "here" is in the string.

Solutions

Expert Solution

check out the solution.

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

CODE:

#include <cstring>
#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
  
const int SIZE = 20;
char str[SIZE];
char str1[SIZE];
int n;
int k = 0;
  
printf("Enter a word: \n");
fgets(str,SIZE,stdin);
printf("Enter another word: \n");
fgets(str1,SIZE,stdin);
  
if (str1[strlen(str1) - 1] == '\n')
{
str1[strlen(str1)-1] = '\0';
}
if (str[strlen(str) - 1] == '\n')
{
str[strlen(str)-1] = '\0';
}
  
  
printf("Enter a number between 1 and 20: \n");
scanf("%d",&n);
strcat(str, " ");
strncat(str, str1,n);
printf("%s", str);
  
printf("\n");
  
str[0] = toupper(str[0]);
for (int i = 1; i < strlen(str); i++)
{
str[i] = tolower(str[i]);
if(str[i-1] == ' ')
{
str[i]= toupper(str[i]);
}
}
// loop through all characters in string till strlen-4
for (int i=0; i<=(strlen(str)-4); i++)
{
// search for characters 'here' as here is converted to 'Here' in above code so check for 'Here' instead ' here'
if(str[i] == 'H' && str[i+1] == 'e' && str[i+2] == 'r' && str[i+3] == 'e')
k++;
}
  
cout << "Found "<< k << " words 'here' in : " << str;
  
return 0;
}

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

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

OUTPUT :

====================================================


Related Solutions

#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int&...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } int search(const int a[], int numberUsed, int target) {...
#include <iostream> #include <cmath>using namespace std; const int A_CONSTANT = 3; void functionA(int a[], int aNumber);...
#include <iostream> #include <cmath>using namespace std; const int A_CONSTANT = 3; void functionA(int a[], int aNumber); void functionB(int a[], int anotherNumber); void functionC(const int anArray[], int aNumber); void functionD(int& sum); int functionE(double number); void functionF(int n); int main( ){ int production[A_CONSTANT]; cout << "This program displays a graph showing\n" << "production for each factory in the company.\n"; functionA(production, A_CONSTANT); functionB(production, A_CONSTANT); functionC(production, A_CONSTANT); return 0;} void functionA(int a[], int aNumber){ for (int someNumber = 1;someNumber <= aNumber; someNumber++) { cout...
in C++, #include<iostream> using namespace std; const int NUM = 10; void prepareArr(int a[]); int countEven...
in C++, #include<iostream> using namespace std; const int NUM = 10; void prepareArr(int a[]); int countEven (int b[]); int main() { int arr[NUM]; // write a statement to call prepareArr to set values for the array // write a statement to call countEven and print the data returned for(int i = 0; i<NUM; i++) cout << arr[i] <<" "; cout <<endl; return 0; } void prepareArr(int a[]) { //randomly generate NUM integers in the range [0,99] and save them in...
#include<vector> #include<iostream> using namespace std; void println(const vector<int>& v) {    for (int x : v)...
#include<vector> #include<iostream> using namespace std; void println(const vector<int>& v) {    for (int x : v)        cout << x << " ";    cout << endl; } void println(const vector<string>& v) {    for (const string& x : v)        cout << " "<< x << " ";    cout << endl; } int main() {    vector<int> v0;    cout << "An empty vector of integers: ";    println(v0);    vector<int> v1(3);    cout << "A...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1;...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1; hour <= 12; hour++)     {         for (min = 0; min <= 59; min++)         {             cout << hour << ":" << min << "AM" << endl;         }     }       return 0; } 1.      Type in the above program as time.cpp. Add a comment to include your name and date. Compile and run. 2.      What is the bug or logic error in the above program? Add the...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool a = true; bool b= true;    //Print the Conjunction function cout<<"\n\nConjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(P∧Q)" <<endl; cout<< a <<"\t"<< b <<"\t"<< conjunction(a,b) <<endl; cout<< a <<"\t"<< !b <<"\t"<< conjunction(a,!b) <<endl; cout<< !a <<"\t"<< b <<"\t"<< conjunction(!a,b) <<endl; cout<< !a <<"\t"<< !b <<"\t"<< conjunction(!a,!b)<<endl;    //Print the Disjunction function cout<<"\n\nDisjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(PVQ)" <<endl; cout<< a <<"\t"<< b <<"\t"<< disjunction(a,b) <<endl;...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool a = true; bool b= true;    //Print the Conjunction function cout<<"\n\nConjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(P∧Q)" <<endl; cout<< a <<"\t"<< b <<"\t"<< conjunction(a,b) <<endl; cout<< a <<"\t"<< !b <<"\t"<< conjunction(a,!b) <<endl; cout<< !a <<"\t"<< b <<"\t"<< conjunction(!a,b) <<endl; cout<< !a <<"\t"<< !b <<"\t"<< conjunction(!a,!b)<<endl;    //Print the Disjunction function cout<<"\n\nDisjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(PVQ)" <<endl; cout<< a <<"\t"<< b <<"\t"<< disjunction(a,b) <<endl;...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count =...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count = 0, maximum = 0, minimum = 0;     cout << "Enter your student ID number: ";     cin >> studentid;     cout << "Student ID Number = " << studentid << endl;     while (studentid != 0)     {          numberreverse[count] = studentid % 10;          if (count == 0)          {              minimum = numberreverse[count];              maximum = minimum;          }          else...
#include<iostream> using namespace std; int main() {    int number_resistors;    double Resistors;    double series;...
#include<iostream> using namespace std; int main() {    int number_resistors;    double Resistors;    double series;    double parellel;    cout << "how many resistors: ";    cin >> number_resistors;    while (number_resistors != 0)    {        cout << "Enter the values of" << number_resistors << " resistors ";        for (int i = 0; i < number_resistors; i++) {            cin >> Resistors;            series += Resistors;                parellel...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT