Link to home
Start Free TrialLog in
Avatar of ronan_40060
ronan_40060Flag for United States of America

asked on

Use of substr( ) function to get the last two characters

Dear Experts

I have a pointer variable pacct which hold a character string ACCOUNT CHK-M
what I need a substr function example that will only get the "-M' from ACCOUNT CHK-M  
Thank you very much
Ronan
Avatar of dtvjho
dtvjho
Flag of United States of America image

ronan,

It appears strstr() will fill your need. From the man page:

char *strstr(const char *s1, const char *s2);

strstr()
     The strstr() function locates the first occurrence of the
     string s2 (excluding the terminating null character) in
     string s1 and returns a pointer to the located string, or a
     null pointer if the string is not found. If s2 points to a
     string with zero length (that is, the string ""), the func-
     tion returns s1.

So an example would be:

void foo(void)
{
char *acct = "ACCOUNT CHK-M";
char *m = strstr(acct, "-M");
}

The pointer 'm' will point to -M if -M exists within the source string 'acct'.
Avatar of ronan_40060

ASKER

Hello dtvjho
Thanks for your reply
Please correct me if I am wrong  
I have one question , instead of "-M" from the string ACCOUNT CHK-M i need to get the last two bytes only then it will be like

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

int main (void)

{

char *acct = "ACCOUNT CHK-M";
char *m = strstr(acct, "  " );    
printf("%s\n", &m );
}

Your last request "i need to get the last two bytes only" is confusing. However, I do see one bug in the program. The line:

printf("%s\n", &m );

should be:

printf("%s\n", m );

'm' is already a pointer type and can be passed directly to printf since %s expects a pointer.

If you meant to get the tail end of your string, i.e. the "CHK-M" piece, then your use of strstr is very close.
The statement char *m = strstr(acct, "  " ); will give back to you a pointer to the blank (0x20) character. Use m++; to move m past the blank so m points to the 'C'. Hope this helps.

Jim
Jim im bit confused need your help again
I need to have a last two characters of string i.e "-M" only and then in that case what will be the change
Thanks for your time
Regards
Ronan
no matter what I select as a string , i need to have the last two characters of that string into a pointer variable m
Thanks for your time
best Regards
Ronan
Avatar of grg99
grg99

Does "-M"   mean it will always be the character dash followed by the letter "M, or can the M be something else, like a number?

SOLUTION
Avatar of rettiseert
rettiseert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hello grg99
I have two strings "ACCOUNT CHK-M" and ACCOUNT CHK-B"
so depending upon the either of the two string selection , it will always be the character dash followed by the letter "M" or the letter "B"

Avatar of Kent Olsen

Hi Ronan,

There are two simple ways to solve this.  If the LAST character of the string is 'B' or 'M'

char *LastChar;
char *String = "xxxxxxxxxx CHK-M";

  LastChar = String + strlen (String) - 1;

If there MAY be other text after the 'B' or 'M' and the 'B' or 'M' always follows the first '-', then

char *LastChar;
char *String = "xxxxxxxxxx CHK-B";

  LastChar = strchr (String, '-') + 1;


It sounds like either should work for you.


Good Luck,
Kent
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dear experts
thanks for all your replies


This is my final code which returns the last two characters of string "ACCOUNT CHK-B"

#include<stdio.h>
#include<string.h>
int main (void)
{
char *LastChar;
char *String = "ACCOUNT CHK-B";
 LastChar = String + strlen (String) - 2;
 printf("%s\n",LastChar  );
 return 0;
}

----------------------------------------------------------------------------------------------------------------------


Now I will use the above logic in the my code of the program  as follows

in the following program prodp->pnam contains the product description which is either "ACCOUNT CHK-B" or "ACCOUNT CHK-M"
 

      fprintf(pdp,
   1478             "CTRL_ACCT|%s|%s|%s|%s\
   1479 |%s|%s|%s|%s|\n",
   1480 pa->acctp->cact_num,                        
   1481  
   1482  
   1483  pa->acctp->cleg_rel,/*relationship title*/
   1484  
   1485  
   1486  ( prodp ? prodp->pnam : "ERROR" ),  
   1487  
   1488  pa->acctp->cdup_sta,/*duplicate statmt*/
   1489  
   1490  pa->acctp->cnum_sgn,                    
   1491  
   1492  (prodp ? prodp->pnam : " "  )  
   1493  getEscracctype(pa->acctp,"CTRL") );


So how would i modify the logic so that ( prodp ? prodp->pnam : " " ),
contains the last two characters of the string either "-B" or "-M"

first i thought i wud use like
 
LastChar = prodp + strlen (prodp) -2 ;
( prodp ? prodp->pnam : "LastChar" )

but im not sure if its a good idea

please suggest
Thanks
Ronan
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Kent Thanks for the reply

prodp is always a string which always holds the values of "ACCOUNT CHK-B" or "ACCOUNT CHK-M" or only "ACCOUNT CHK"
in the above code it will just store the last two charactresi.e ( -M or -B or HK) of prodp into LastChar thats it and which will be passed to a macro then.

but here we need to have a LastChar defined correctly so that it will always stores the last two characters of prodp

please correct me if I am wrong
> LastChar = prodp + strlen (prodp) -2 ;
> ( prodp ? prodp->pnam : "LastChar" )

should work well here ??

thanks for all your time
Regards
Ronan

>> ( prodp ? prodp->pnam : "LastChar" )


If prodp is a string (defined as char *prodp) then the line above can't work.  You're attempting to dereference the structure variable prodp->pnam.


Kent
Kent
its not a structure
the idea here is passing of variable to a macro in another file.
right prodp is a string (defined as char *prodp)
Since we need only last two characters to be stored in  a variable so what i think is as below

if (prodp)  
  {
    if (strlen (prodp) > 2)  
    {
      LastChar = prodp + strlen (prodp) - 2;

    }

      else
      // handle null pointer error


Is this above OK ?
thanks for your time
Regards
Ronan

Sure.  That should work just fine.

Though if you're unsure about the quality/contents of the data, you might want to make sure that you handle all of the errors -- null pointer, short data, and bad trailing characters.


Kent
Kent
Is it OK if I
write like
(prodp? prodp->pnam + (strlen(prodp->pnam) - 2): "LastChar");

Nope.

(prodp? prodp->pnam + (strlen(prodp->pnam) - 2): "LastChar");
            ^^^^^^^^^              ^^^^^^^^^

Since prodp is a string (char *) you cannot dereference an element.  There is no element to dereference.


Kent
Many many Thanks to Kent for brilliant answer.
many thanks to grg99, rettiseert and dtvjho for their valuable input
Thanks all
Have a great weekend
Ronan