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 8163279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:58:59+00:00 2026-06-06T18:58:59+00:00

I am working on a code which requires a function. This function gets a

  • 0

I am working on a code which requires a function. This function gets a string as input and returns a string.

What I have planned so far is to get a str[], remove all $‘s and spaces, and store this in another string which is returned later:

char *getstring(char str[])
{
    int i=0;
    char rtn[255];
    while (i<strlen(str))
    {
       if (str[i] != " " || str[i] != "$" )
             rtn[i] = str[i];
       else
             rtn[i] = '';
    } 
    return str;
}

I dont feel like this will work. Any ideas?? :-S

  • 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-06T18:59:00+00:00Added an answer on June 6, 2026 at 6:59 pm

    Problem #1:
    Your function returns a pointer to a (temporary) string which is allocated on the stack. This means that once your function ends, the memory for rtn is released, and this invalidates the char * pointer that the function returns.

    What you would rather do is:

    void getstring(char str[], char rtn[])
    {
        /* Some processing on rtn... */
    }
    

    The caller to getstring should handle the allocation and deallocation of the string passed as rtn.

    Problem #2:
    Your while loop will run indefinitely, because i never increases. Put i++ somewhere inside the loop.

    Problem #3:
    The condition in your if-statement is faulty. You compare str[i], which is a char, to " " or "$", which are string literals (having '\0' in the end). This is wrong.
    You need to compare them to chars, which are indicated by apostrophes (not quotation marks).
    Also, note that the test condition is wrong as well. You need a logical AND operator instead of OR.
    Change the if-statement to:

    if (str[i] != ' ' && str[i] != '$')
    

    Problem #4:
    What does rtn[i] = ''; mean? '' is an empty character constant, which is illegal in C.
    Did you want to just skip a character in str?

    Problem #5:
    You have indexation problems. Because str and rtn may obviously be of different length, you need to manage two running indices, one for each string.

    Problem #6:
    rtn is not necessarily null-terminated when the function returns. Assign '\0' to the end of rtn before the function returns (i.e. rtn[i] = '\0'; after the while-loop ends).


    Here’s your code with all the problems mentioned above fixed:

    void getstring(char str[], char rtn[])
    {
        int i = 0, j = 0;
        while (i < strlen(str))
        {
           if (str[i] != ' ' && str[i] != '$')
                 rtn[j++] = str[i];
           i++;
        }
        rtn[j] = '\0';
    }
    

    And here’s a more efficient version that uses pointers instead of indices, and doesn’t use strlen:

    void getstring(char *str, char *rtn)
    {
        while (*str)
        {
           if (*str != ' ' && *str != '$')
                 *rtn++ = *str;
           *str++;
        }
        *rtn = '\0';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which is working, I was wondering if this can
I have this set of code which was working fine about an hour or
I am currently working on this project which requires me to make a function
I'm working on some code which fills a GridView full of images that have
I have a working JavaScript code below which dynamically creates JSON object using JSON.parse
I have code which as been working against an older Active Directory server and
I have jquery validation code which is working fine in ff but the same
I have the follwing code (which is not working): private void Window_PreviewKeyDown(object sender, KeyEventArgs
I am working on this website which requires a jquery/ajax contact form. Everything works
I often have code based on a specific well defined algorithm. This gets well

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.