Given the c language below, I am lost why the outpout shows random characters when I want them to be words. Could you help me improve my code, please thank you. A picture shows the error output.     #include #include #include   int main () {     char string[1000], word[1000], array[100][150]; //showing the maximum size for string, word and array     int i = 0, j = 0, k = 0, len1 = 0, len2 = 0, count = 0;       printf ("Enter the string:\n");      //user input a sentence     gets(string);       printf ("Enter the word to be removed:\n");     //inputs the words to be removed     gets (word);    // (Removing process condition)   for (i = 0; string[i] != '\0'; i++)     {         if (string[i] == ' ')         {             array[k][j] = '\0';             k ++;             j = 0;         }         else         {             array[k][j] = array[i];             j ++;         }     }       array[k][j] = '\0';       j = 0;     for (i = 0; i < k + 1; i++)     {         if (strcmp(array[i], word) == 0)         {             array[i][j] = '\0';         }     }       j = 0;          // calculate words before removing     printf("The words before removing: ");          // Returns first token      char* token = strtok(string, " ");        // always keep token's printing     while (token != NULL) {          count+=1;         token = strtok(NULL, " ");      }      // if word has any break it will count +1     printf("%d\n", count);          count = 0;      // printing the sentence after removing     printf ("String or sentence after removing: ", word);     for (i = 0; i < k + 1; i++)     {         if (array[i][j] == '\0')             continue;         else         {             printf ("%s ", array[i]);             count++;         }     }          printf ("\n");      //calculating words after removing of letter     printf("The words after removing: ");     printf("%d\n", count);     return 0; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
icon
Concept explainers
Question

Given the c language below, I am lost why the outpout shows random characters when I want them to be words. Could you help me improve my code, please thank you. A picture shows the error output.

 

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main ()
{
    char string[1000], word[1000], array[100][150]; //showing the maximum size for string, word and array

    int i = 0, j = 0, k = 0, len1 = 0, len2 = 0, count = 0;
 
    printf ("Enter the string:\n");
     //user input a sentence
    gets(string);
 
    printf ("Enter the word to be removed:\n");

    //inputs the words to be removed
    gets (word);

   // (Removing process condition)
  for (i = 0; string[i] != '\0'; i++)
    {
        if (string[i] == ' ')
        {
            array[k][j] = '\0';
            k ++;
            j = 0;
        }
        else
        {
            array[k][j] = array[i];
            j ++;
        }
    }
 
    array[k][j] = '\0';
 
    j = 0;
    for (i = 0; i < k + 1; i++)
    {
        if (strcmp(array[i], word) == 0)
        {
            array[i][j] = '\0';
        }
    }
 
    j = 0;
    
    // calculate words before removing
    printf("The words before removing: ");
    
    // Returns first token 
    char* token = strtok(string, " ");
  
    // always keep token's printing
    while (token != NULL) { 
        count+=1;
        token = strtok(NULL, " "); 
    } 
    // if word has any break it will count +1
    printf("%d\n", count);
    
    count = 0;

     // printing the sentence after removing
    printf ("String or sentence after removing: ", word);
    for (i = 0; i < k + 1; i++)
    {
        if (array[i][j] == '\0')
            continue;
        else
        {
            printf ("%s ", array[i]);
            count++;
        }
    }
    
    printf ("\n");
     //calculating words after removing of letter
    printf("The words after removing: ");
    printf("%d\n", count);
    return 0;
}

Enter the string:
Hello World! My anme is Faith
Enter the word to be removed:
My anme is Faith
The words before removing: 6
String or sentence after removing: pL(◆◆P�·
The words After removing: 6
0
IX
Transcribed Image Text:Enter the string: Hello World! My anme is Faith Enter the word to be removed: My anme is Faith The words before removing: 6 String or sentence after removing: pL(◆◆P�· The words After removing: 6 0 IX
Expert Solution
steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

The code is here for C language. But the words did not get remove as wanted. The output picture shows the output. Please help.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main ()
{
    char string[1000], word[1000], array[100][150]; //showing the maximum size for string, word and array

    int i = 0, j = 0, k = 0, len1 = 0, len2 = 0, count = 0;
 
    printf ("Enter the string:\n");
     //user input a sentence
    gets(string);
 
    printf ("Enter the word to be removed:\n");

    //inputs the words to be removed
    gets (word);

   // (Removing process condition)
  for (i = 0; string[i] != '\0'; i++)
    {
        if (string[i] == ' ')
        {
            array[k][j] = '\0';
            k ++;
            j = 0;
        }
        else
        {
            array[k][j] = string[i];      // Change 1
            j ++;                           
        }
    }
 
    array[k][j] = '\0';
 
    j = 0;
    for (i = 0; i < k + 1; i++)
    {
        if (strcmp(array[i], word) == 0)
        {
            array[i][j] = '\0';
        }
    }
 
    j = 0;
    
    // calculate words before removing
    printf("The words before removing: ");
    
    // Returns first token 
    char* token = strtok(string, " ");
  
    // always keep token's printing
    while (token != NULL) { 
        count+=1;
        token = strtok(NULL, " "); 
    } 
    // if word has any break it will count +1
    printf("%d\n", count);
    
    count = 0;

     // printing the sentence after removing
    printf ("String or sentence after removing: ", word);
    for (i = 0; i < k + 1; i++)
    {
        if (array[i][j] == '\0')
            continue;
        else
        {
            printf ("%s ", array[i]);
            count++;
        }
    }
    
    printf ("\n");
     //calculating words after removing of letter
    printf("The words after removing: ");
    printf("%d\n", count);
    return 0;
}

Enter the string:
Hello World! I am Faith.
Enter the word to be removed:
I am Faith.
The words before removing: 5
String or sentence after removing: Hello World! I am Faith.
The words after removing: 5
Transcribed Image Text:Enter the string: Hello World! I am Faith. Enter the word to be removed: I am Faith. The words before removing: 5 String or sentence after removing: Hello World! I am Faith. The words after removing: 5
Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Operators
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education