Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string manipulation 1

Status
Not open for further replies.

tsp120

Programmer
May 21, 2003
52
0
0
US
I have a string that is always 4 characters long. I want to store the last three characters into a different string. I've looked at string manipulation for C on the internet and can't really find anything to do this for me. A basic example would be:

If str1 = 3L39 I want str2 = L39

Thanks. :)
 
Code:
#include <stdio.h>
#include <string.h>

int main()
{
   char str1[5] = "3L39", str2[4];
   [red]strcpy[/red](str2, [red]&str1[1][/red]);
   printf("str1 = \"%s\", str2 = \"%s\"\n", str1, str2);
   return 0;
}

/* my output
str1 = "3L39", str2 = "L39"
*/
 
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define shortstr(str,pt,len) {pt = str + len;}

int main(void) {
char str1[5] = "3L39";
char *ptr;
             
           shortstr(str1,ptr,1);
           printf("%s\n",ptr);
return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top