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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:07:12+00:00 2026-05-13T09:07:12+00:00

I have the following code in C: char *str = Hello World; char *s

  • 0

I have the following code in C:

    char *str = "Hello World";
    char *s = malloc(strlen(str));
    int i =0;
    for(;i<strlen(str)-5;i++)
    {
        s += *(str+i);
    }
    printf(s);

It shows nothing. What I want is to get the substring of str stored in s.

In Java I would do the following:

    String str = "Hello World";
    String s="";

    for(int i=0;i<str.length()-5; i++)
        s+=str[i];

    System.out.println(s);

Or instead use the substring method. As String s = str.substring(1,2); for example.

How can I achieve it?

  • 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-05-13T09:07:12+00:00Added an answer on May 13, 2026 at 9:07 am

    Use the strcpy function.

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[] ) 
    {
      char *str = "Hello World";
      size_t length = strlen(str);
    
      char *s = (char*)malloc(sizeof(char) * (length + 1));
    
      strcpy(s, str);
    
      s[length] = '\0'; // makes sure it's NUL terminated
    
      printf("%s", s);
    
      free(s);
    
      return 0;
    }
    

    When allocating the destination buffer, pay attention to the fact that strings are terminated by the NUL character.

    To only copy a substring, use strncpy:

    strncpy(s, str + 6, strlen(str) - 6);
    

    will just copy “World” into s.

    In any case, make sure your C strings are NUL terminated before using functions like printf.

    See also strcat and strncat. And well, familiarize yourself with C arrays and pointers.

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

Sidebar

Related Questions

Hi i have the following code: char msg[10000]; string mystr = hello; I want
I have the following code, #include<stdio.h> #include<stdlib.h> int main() { //freopen(file1,w,stdout); char str[]=command line
i have the following code below: const char* timeformat = %Y-%m-%d %H:%M:%S; const int
I have the following code int ParseData(unsigned char *packet, int len) { struct ethhdr
I have the following code: int main(int argc, char** argv) { onelog a; std::cout
I have the following code: #include <string.h> int main(void) { char *buffer = NULL,
I have the following code: struct VRfile{ char sessionID[10]; char file[20]; int first; };
I have the following code: char stats[109]; /* !LINE UNDER QUESTION! */ sprintf(stats, OBJECTS:\n%u/256\n
gcc 4.4.2 I have the following code: char channels[] = NumberOfChannel = [2]; sscanf(channels,
On a Solaris 5.8 machine, I have the following code: [non-working code] char *buf;

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.