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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:48:42+00:00 2026-06-13T08:48:42+00:00

Im trying to copy substrings from one char* to another, when I printf(%c) it

  • 0

Im trying to copy substrings from one char* to another, when I printf(%c) it shows the chars printing, but at end of add method, i use printf(%s) to print entire string and nothing is printing out. Any help is appreciated – also perhaps a better method for substrings in C than char by char?

Code below:

int aliasCount;

typedef struct Alias {
  char* alias;
  char* actual;
  struct Alias* next;
}Alias;

Alias* aliasHead;

void addAlias(char* new);

addAlias method

void addAlias(char* new)
{
  strip(new);
  Alias* newAlias = (Alias*)malloc(sizeof(Alias));

  //Get start-end position for alias
  int start=0; int end=0; int countSpace=0;
  for(int i = 0; i < strlen(new); i++)
  {
    //Check for space
    if(new[i]==' ')
    {
        printf("found space %d\n",i);
        countSpace++;
        if(countSpace==1)
        {
          start=i;
        }
        else if(countSpace==2)
        {
          end=i;
          break;
        }
    }
  }

  //malloc memory
  newAlias->next=NULL;
  newAlias->alias=(char*)malloc(sizeof(char)*(end-start));
  newAlias->actual=(char*)malloc(sizeof(char)*(strlen(new)-end+1));

  //Get substring, 
  //Copy char by char from alias to node
  for(int i = 0; i < strlen(new); i++)
  {
    if(i>start && i<end) //Alias
    {
        newAlias->alias[i] = new[i];printf("'%c'",newAlias->alias[i]);
    }
    else if(i>end) //Actual command
    {
        newAlias->actual[i] = new[i];printf("'%c'",new[i]);
    }
  } 
  printf("%s\n%s\n",newAlias->alias,newAlias->actual);

  if(aliasCount==0)
  {
    aliasHead = newAlias;
  }
  else
  {
      Alias* curr = aliasHead;
      for(int i = 0; i < aliasCount; i++)
      {
        curr=curr->next;
      }
      curr->next = newAlias;
  }
  aliasCount++;
  printf("%s\n%s\n",aliasHead->alias,aliasHead->actual);
}
  • 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-13T08:48:43+00:00Added an answer on June 13, 2026 at 8:48 am

    Your indices are wrong; you copy the range from new[start+1] to new[end-1] into the range from alias[start+1] to alias[end-1], when you actually need to copy it into the range from alias[0] to alias[end-start-2]:

            newAlias->alias[i-start-1] = new[i];printf("'%c'",newAlias->alias[i]);
    

    and similarly (mutatis mutandis) for actual.

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

Sidebar

Related Questions

I'm trying to copy the <options> HTML Codes from one select list to another.
Trying to copy some user input from one HTML input to another and my
Im trying to copy contents from one table to another, both having identical table
Im trying to copy over data from one table to another in the same
I am trying to copy the contents of one graphics object to another, but
We are trying to copy a BO Query Service from one Universe to another.
im trying to copy 300 lines from one file to another, in source file
I am trying to copy a view from one database to another and get
I'm currently trying to copy data from table into another by using a SELECT
I'm trying to copy to my server an image from another site and i

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.