In c if I am given the original string like you have, I have to find the starting position of the subset I want to copy to the other string. I need to convert that str[x] to a char * type.
i'm using visual studio 2008 and i can't use it to compile C language because of the setting problem. you detect the string[a] and convert it to char *type, But if the last 4 letter is 1234 or something else. do u know how to get it? take the last 4 letter of a string.
the setting issue of Visual studio 2008? I guess I don't understand what you mean since visual studio has been able to compile c and c++ code for so long. This code is also pretty common to c++ too, but this isn't the way we do it in c++ but it can be done this way.
Generally I have to know the starting position of the segment I have to cut out or copy. In either c or c++ it would look basically the same. The headers may be set up differently. Like:
// for c++
#include <cstdio>
#include <cstring>
// for c
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[]= "123456-78-abcd";
char str2[5];
// set up the scan to find the start position...
for(int index = 0; str1[index] != '\0'; index++)
{
if(str1[index] == 'a') // are we at the start position
{
// copy into str2 from the reference of str1[index] for the length of 4 places. index being where I found the 'a' character.
strncpy_s(str2, &str1[index] ,4);
// null cap the string.
str2[4]='\0';
}
}
// print it out to standard output I believe.
puts (str2);
// but I guess I would use
printf("subString: %s", str2);
return 0;
}
in c++ we would make it look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
string string1= "123456-78-abcd";
string string2;
// same procedure I have to find where abcd is in the string...
size_t nPos = string1.find("abcd");
// now we take the substring of length 4
string2 = string1.substr(nPos, 4);
cout << "Sub string is : " << string2 << endl;
return 0;
}