urgent: Printing a part of string - prefix

Posted on 2009-04-21
Medium Priority
Last Modified: 2012-05-06
i am not looking for code but for some direction..

i am trying to print a part of a string which is prefix...

i.e like this:

an apple where i am trying to print an app...

is it possible ? could anyone pls help me. tq
Question by:zizi21
  • 3
  • 2
LVL 40

Expert Comment

ID: 24193141
// string::substr
#include <iostream>
#include <string>
using namespace std;
int main ()
  string str="We think in generalities, but we live in details.";
                             // quoting Alfred N. Whitehead
  string str2, str3;
  size_t pos;
  str2 = str.substr (12,12); // "generalities"
  pos = str.find("live");    // position of "live" in str
  str3 = str.substr (pos);   // get from "live" to the end
  cout << str2 << ' ' << str3 << endl;
  return 0;

Open in new window

LVL 40

Expert Comment

ID: 24193148
Oops... sorry ziz21... just realised this was only C zone as I clicked submit. Oh well, at least you know how easy it is in C++ now :)
LVL 40

Assisted Solution

evilrix earned 800 total points
ID: 24193155
You can use strncpy to do this...
/* strncpy example */
#include <stdio.h>
#include <string.h>
int main ()
  char str1[]= "To be or not to be";
  char str2[6];
  strncpy (str2,str1,5);
  puts (str2);
  return 0;
To be 

Open in new window

Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

LVL 53

Accepted Solution

Infinity08 earned 1200 total points
ID: 24193159
Since this is in the C zone, you might want to look at the precision option for the printf formats :


For example :

        char str[] = "some text ...";
        printf("%.*s", 5, str);

will print the first 5 characters of the given string.
LVL 53

Expert Comment

ID: 24193164
>> Oops... sorry ziz21... just realised this was only C zone as I clicked submit.

heh ;)

Author Comment

ID: 24193296
thanks a million..looking at it now...thanks

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question