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

  • SEARCH
  • Home
  • 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 9020675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:04:50+00:00 2026-06-16T05:04:50+00:00

So my brother was making a program to turn all words in a string

  • 0

So my brother was making a program to turn all words in a string to hashtag, but for some reason it always gives a “segmentation fault” error at the end of execution. I tried to find what may cause it, but haven’t found. Here’s the code:

#include <stdio.h>
#include <string.h>

char* setHashtag(char text[10000])
{
    int i, j;

    printf("Initial text = %s\n", text);

    for (i = 9998; i >= 0; i--)
    {
        text[i+1] = text[i];
    }
    text[0] = ' ';

    for (i = 0; text[i+1] != '\0'; i++)
    {
        if(text[i] == ' ' && text[i+1] != ' ')
        {
            for (j = 9998; j > i; j--)
            {
                text[j+1] = text[j];
            }
            text[i+1] = '#';

            printf("Partial text = %s\n", text);
        }
    }

    return text;
}

void execute() {
    char text[5000], textFinal[10000];

    gets(text);

    strcpy(textFinal, setHashtag(text));
    printf("%s\n", textFinal);
}

int main()
{
    execute();
    printf("Back to main\n");
    return 0;
}
  • 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-16T05:04:51+00:00Added an answer on June 16, 2026 at 5:04 am

    You pass an array of size 5000 into your function, yet you access 10000 elements inside. Of course, it will crash.

    This size of the array specified in function declaration does not matter. It is ignored by the compiler. This

    char* setHashtag(char text[10000])
    

    is equivalent to this

    char* setHashtag(char *text)
    

    i.e. the function receives a pointer to the beginning of your original argument array, not a new local copy of the argument array (naked arrays in C are not copyable).

    This means that when you call your function as

    char text[5000];
    ...
    setHashtag(text)
    

    the text array does not magically become a char [10000] array. It remains a char [5000] array, as it was originally declared. Attempting to access text[9998] and such inside the function leads to undefined behavior.

    Since your setHashtag function expects a fixed size array of size 10000, it might be a better idea to declare your function as

    char* setHashtag(char (*text)[10000])
    

    and pass in the array arguments as setHashing(&text). This will make sure you will not be able to pass in an array of wrong size. Inside the function you’ll have to access the array as (*text)[i].

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

Sidebar

Related Questions

I'm a beginner and my brother has showed me some prototype. All the weeks
I'm currently making a small program to teach me some new Java, and I'm
My brother wrote this code for me to show an example of polymorhism. But
I was reading a question on making a generic property , but I'm a
My little brother would like to start making websites, and I've suggested Frontpage or
I am making a Service Program where it will change the start up type
Greetings all. I need to get some opinions from those outside of my work
I'm making a python URL grabber program. For my purposes, I want it to
My brother would like to learn some programming to find out if he'd enjoy
the code my brother wrote worked before but i think i changed him by

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.