Question

In: Computer Science

```please convert this code to make only using for loop not while loop #include #include #define...

```please convert this code to make only using for loop not while loop

#include
#include
#define MAX_SIZE 500

int main()
{
char str[MAX_SIZE];
char tosearch[MAX_SIZE];
char part1[100];
char part2[100];
int cursor = 0, i, cnt1 = 0, cnt2 = 0, cnt = 0;
int j = 0;
int a = 0;
int b = 0;
int total = 0;
printf("Enter any string: ");
gets(str);
printf("Enter word to search occurrences: ");
gets(tosearch);

for (i = 0; i < strlen(tosearch); i++)
{
if (tosearch[i] == '*')
{
for (j = 0; j < i; j++)
{
part1[j] = tosearch[j];

}

a = j;
}

}

for (j = 0; j < strlen(tosearch) - a; j++)
{
part2[j] = tosearch[a + j + 1];
}

for (j = 0; j < a; j++)
{
printf("%c", part1[j]);

}
printf("\n");

for (j = 0; j < strlen(tosearch) - a - 1; j++)
{
printf("%c", part2[j]);
}

printf("\n");
b = strlen(part2);


while (cursor < strlen(str)) { // while the cursor is smaller than length of main string while loop goes on

while (cnt1 != a) { // if the cnt1 is smaller than the part1 length the while loop goes on
  
if (str[cursor] == part1[0]) { // if the str[cursor] is correspond to the first word of part1 go to for loop

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

if (str[cursor + i] == part1[i])
{
cnt1++; // count the number of matching letter of part1 and main string

}
else { // else the cnt1 goes to 0 again and cursor goes forward
cnt1 = 0;
cursor++;
break;
}
}
}
if (cursor > strlen(str) || cnt1 == a)
{
  
break;
}
cursor++; // cursor goes to the next letter
}

cursor += a; printf("the value of cursor %d ", cursor);
  

while (cnt2 != b) { // if cnt2 is less than the length of part2 the while loop goes on


if (str[cursor] == part2[0]) { // if the str[cursor] correspond with part2[0] for loop goes on

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

if (str[cursor + i] == part2[i])
{
cnt2++; // count the matching letter of str and part2
}

else {
cnt2 = 0; // else cnt2 goes to 0 and cursor move on break free from the for loop
cursor++;
break;
}
}
}
if (cursor > strlen(str) || cnt2 == b) // if the cursor is larger than the strlen or cnt2 == (the length of part2) break free
break;

cursor++; // move on to the next letter of the main string
  
}

if (cursor + b > strlen(str))
break;

cnt++;
cnt1 = 0;
cnt2 = 0;
}
printf("tot %d\n", cnt);
}

Solutions

Expert Solution

Ans

code:-

#include

#include

#define MAX_SIZE 500

int main()

{

char str[MAX_SIZE];

char tosearch[MAX_SIZE];

char part1[100];

char part2[100];

int cursor = 0, i, cnt1 = 0, cnt2 = 0, cnt = 0;

int j = 0;

int a = 0;

int b = 0;

int total = 0;

printf("Enter any string: ");

gets(str);

printf("Enter word to search occurrences: ");

gets(tosearch);

for (i = 0; i < strlen(tosearch); i++)

{

if (tosearch[i] == '*')

{

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

{

part1[j] = tosearch[j];

}

a = j;

}

}

for (j = 0; j < strlen(tosearch) - a; j++)

{

part2[j] = tosearch[a + j + 1];

}

for (j = 0; j < a; j++)

{

printf("%c", part1[j]);

}

printf("\n");

for (j = 0; j < strlen(tosearch) - a - 1; j++)

{

printf("%c", part2[j]);

}

printf("\n");

b = strlen(part2);

for (;cursor < strlen(str);cnt++) { // while the cursor is smaller than length of main string while loop goes on

for(;cnt1 != a;cursor++) { // if the cnt1 is smaller than the part1 length the while loop goes on

  

if (str[cursor] == part1[0]) { // if the str[cursor] is correspond to the first word of part1 go to for loop

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

if (str[cursor + i] == part1[i])

{

cnt1++; // count the number of matching letter of part1 and main string

}

else { // else the cnt1 goes to 0 again and cursor goes forward

cnt1 = 0;

cursor++;

break;

}

}

}

if (cursor > strlen(str) || cnt1 == a)

{

  

break;

}

// cursor goes to the next letter

}

cursor += a; printf("the value of cursor %d ", cursor);

  

for(;cnt2!=b;cursor++)

{ // if cnt2 is less than the length of part2 the while loop goes on

if (str[cursor] == part2[0]) { // if the str[cursor] correspond with part2[0] for loop goes on

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

if (str[cursor + i] == part2[i])

{

cnt2++; // count the matching letter of str and part2

}

else {

cnt2 = 0; // else cnt2 goes to 0 and cursor move on break free from the for loop

cursor++;

break;

}

}

}

if (cursor > strlen(str) || cnt2 == b) // if the cursor is larger than the strlen or cnt2 == (the length of part2) break free

break;

// move on to the next letter of the main string

  

}

if (cursor + b > strlen(str))

break;

cnt1 = 0;

cnt2 = 0;

}

printf("tot %d\n", cnt);

}

If any doubt ask in the comments.


Related Solutions

```please convert this code to make only using for loop not while loop #include #include #define...
```please convert this code to make only using for loop not while loop #include #include #define MAX_SIZE 500 int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE]; char part1[100]; char part2[100]; int cursor = 0, i, cnt1 = 0, cnt2 = 0, cnt = 0; int j = 0; int a = 0; int b = 0; int total = 0; printf("Enter any string: "); gets(str); printf("Enter word to search occurrences: "); gets(tosearch); for (i = 0; i < strlen(tosearch); i++) {...
Please code C# Convert the following for loop into a while loop: for(int count = 8;...
Please code C# Convert the following for loop into a while loop: for(int count = 8; count > 0; count--) { Console.WriteLine(count); }
Study the following code with a while-loop and convert it to a for-loop (fill in the...
Study the following code with a while-loop and convert it to a for-loop (fill in the blanks). int i=4, result=1; while(i>1) { result *= i; i--; } The following for-loop performs the same functionality: int result=1; for (__________ i=4; i _________1;____________) { result *= i; }
Create Python Code using a "for" loop and a "while" loop. You are starting a twelve...
Create Python Code using a "for" loop and a "while" loop. You are starting a twelve week program training to compete in a triathlon. The triathlon consists of three athletic events, 1.5 k swim, 40k bike, 10k run. In order to be prepared for the competition you want to print a training schedule. Starting with week 1 you will increase the distance of each activity so that you reach the race distance by week twelve. Due to rounding, you may...
All in C++ programming language 1. a.) convert for loop to while loop example b.) convert...
All in C++ programming language 1. a.) convert for loop to while loop example b.) convert while loop to for loop example 2.) pass one dimension array(and its size) to function example
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting...
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting clc, clear, format compact; %define variables k=1; b=-2; x=-1; y=-2; %while loop initialization for k <= 3 disp([num2str(k), ' ',num2str(b),' ',num2str(x),' ',num2str(y),]); y = x^2 -3; if y< b b = y; end x = x+1; k = k+1; end
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using namespace std; // M x N matrix #define M 5 #define N 5 // Naive recursive function to find the minimum cost to reach // cell (m, n) from cell (0, 0) int findMinCost(int cost[M][N], int m, int n) {    // base case    if (n == 0 || m == 0)        return INT_MAX;    // if we're at first cell...
Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel...
Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User is done entering grades ELSE Count each grade as it is entered. Compute a running total of the grades entered. END IF After the user enters the sentinel of -1, calculate the average of...
Please convert This java Code to C# (.cs) Please make sure the code can run and...
Please convert This java Code to C# (.cs) Please make sure the code can run and show the output Thank you! Let me know if you need more information. Intructions For this assignment you will be creating two classes, an interface, and a driver program: Class Calculator will implement the interface CalcOps o As such it will implement hexToDec() - a method to convert from Hexadecimal to Decimal. Class HexCalc will inherit from Calculator. Interface CalcOps will have abstract methods...
Use while loop for the num inputs #include #include using namespace std; int main() {   ...
Use while loop for the num inputs #include #include using namespace std; int main() {    long long int digit;    long long int num1, num2, num3, num4, num5;    int ave;       cout << "Enter a 10 digit number: ";    cin >> digit;       num5 = digit %100;    digit = digit / 100;       num4 = digit %100;    digit = digit / 100;    num3 = digit %100;    digit = digit /...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT