Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8815127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:17:53+00:00 2026-06-14T04:17:53+00:00

I am a beginner learning C; so, please go easy on me. :) I

  • 0

I am a beginner learning C; so, please go easy on me. 🙂

I am trying to write a very simple program that takes each word of a string into a “Hi (input)!” sentence (it assumes you type in names). Also, I am using arrays because I need to practice them.

My problem is that, some garbage gets putten into the arrays somewhere, and it messes up the program. I tried to figure out the problem but to no avail; so, it is time to ask for expert help. Where have I made mistakes?

p.s.: It also has an infinite loop somewhere, but it is probably the result of the garbage that is put into the array.

#include <stdio.h>
#define MAX 500 //Maximum Array size.

int main(int argc, const char * argv[])
{
    int stringArray [MAX];
    int wordArray [MAX];
    int counter = 0;
    int wordCounter = 0;

    printf("Please type in a list of names then hit ENTER:\n");  
    // Fill up the stringArray with user input.
    stringArray[counter] = getchar();
    while (stringArray[counter] != '\n') {
        stringArray[++counter] = getchar();
    }

    // Main function.
    counter = 0;
    while (stringArray[wordCounter] != '\n') {     
        // Puts first word into temporary wordArray.
        while ((stringArray[wordCounter] != ' ') && (stringArray[wordCounter] != '\n')) {
            wordArray[counter++] = stringArray[wordCounter++];
        }
        wordArray[counter] = '\0';

        //Prints out the content of wordArray.
        counter = 0;
        printf("Hi ");
        while (wordArray[counter] != '\0') {
            putchar(wordArray[counter]);
            counter++;
        }
        printf("!\n");

        //Clears temporary wordArray for new use.
        for (counter = 0; counter == MAX; counter++) {
            wordArray[counter] = '\0';
        } 
        wordCounter++;
        counter = 0; 
    }
    return 0;
}

Solved it! I needed to add to following if sentence to the end when I incremented the wordCounter. 🙂

    if (stringArray[wordCounter] != '\n') {
            wordCounter++;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:17:54+00:00Added an answer on June 14, 2026 at 4:17 am
        for (counter = 0; counter == MAX; counter++) {
            wordArray[counter] = '\0';
        } 
    

    You never enter into this loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a beginner in programming (learning Java). I am trying to write a
I am a beginner in Haskell so please bear with me. (Just started learning
I'm a beginner to C programming. I'm trying to learning how to code a
I am a beginner learning NodeJS. I am sure that the scripting language has
I am a beginner, just learning C; so, please be patient with me. I
I'm a beginner learning Windows Forms Applications in C# and I want to write
I'm a beginner learning WPF, and need to create an Application Bar that launches
I'm beginner with C and I am learning on my own. I am trying
I am learning oracle and i am beginner. Please solve my following error. create
query level: beginner As part of a learning exercise I have written code that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.