Concatenate a single char to a string?

Hi - I'm a newbie C programmer. Why doesn't this work and how do I fix it? It give an error on strcpy(sName, sTemp[j]);:

1
2
3
4
5
6
7
8
9
10
11
12
13
char sName[128] = {0};
char sTemp[128] = {0};
long j;

strcpy(sTemp, "name1\tname2\t");

for (j=0;j<=128;j++)
 {
 if (sTemp[j] != '\t')
  {
  strcpy(sName, sTemp[j]);
  }
 }
Last edited on
this will strip out the TAB characters from sTemp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char sName[128] = {0};
char sTemp[128] = {0};
long j;
int p=0;                                     // add this

strcpy(sTemp, "name1\tname2\t");

for (j=0;j<=128;j++)
 {
 if (sTemp[j] != '\t')
  {
    sName[p]=sTemp[j];   // change & add this
    p++;
  }
 }
Thanks, but I'm actually trying to take a string with embedded tabs and put each subelement in an array, such that:

char sName1[128] = {0};
char sName2[128] = {0};

so:

the array of chars in sName1 contains the string "name1"
the array of chars in sName2 contains the string "name2"
Try this code:
1
2
3
4
5
6
7
8
9
10
int pos;
for (int i=0;i<128;i++) {
  if (sTemp[i]!='\t') sName1[i]=sTemp[i];
  pos=i;
  if (sTemp[i]=='\t') break;
}
for (int i=pos+1;i<128;i++) {
  if (sTemp[i]!='\t') sName2[i]=sTemp[i];
  else break;
}


Topic archived. No new replies allowed.