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; }

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter7: Characters, Strings, And The Stringbuilder
Section: Chapter Questions
Problem 12RQ
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.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT