Big trouble~~~need assistance~~~~

Anyone know how too get the last 4 letter (abcd) by using this statement? now it only can take the first 4 letter (1234)...
1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[]= "123456-78-abcd";
  char str2[5];
  strncpy_s(str2,str1,4);
  str2[4]='\0';
  puts (str2);
  return 0;
}
Last edited on
if I were doing this, if I remember correctly my c.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[]= "123456-78-abcd";
  char *pChar = &str1;
  char str2[5];
  
  for(; pChar != '\0'; pChar++)
  {
         if(pChar == 'a')
         {
               strncpy_s(str2,pChar,4);
               str2[4]='\0';
         }
  }
  puts (str2);
  return 0;
}
and it will only print last 4letter abcd as the output?
did you try it?

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.

alternately my code would look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[]= "123456-78-abcd";
  char str2[5];
  
  for(int index = 0; str1[index] != '\0'; index++)
  {
         if(str1[index] == 'a')
         {
               strncpy_s(str2, &str1[index] ,4);
               str2[4]='\0';
         }
  }
  puts (str2);
  return 0;
}
Last edited on
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 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>
using namespace 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;
}


I don't know how I can make it any clearer.
Last edited on
thank you very much!
Topic archived. No new replies allowed.