• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

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
0
ronan_40060
Asked:
ronan_40060
  • 9
  • 5
  • 3
  • +2
4 Solutions
 
dtvjhoCommented:
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'.
0
 
ronan_40060Author Commented:
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 );
}

0
 
dtvjhoCommented:
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ronan_40060Author Commented:
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
0
 
ronan_40060Author Commented:
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
0
 
grg99Commented:
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?

0
 
rettiseertCommented:
/* Use this function to get the last n characters of a string: */

char *getLastChars(char *fullString, int charsToGet){
   int lenFullString = strlen(fullString);
   if (lenFullString >= charsToGet){
      return fullString + ( lenFullString - charsToGet );
   }else{
      return NULL;
   }
}

/* Example, this will print the last 2 characters of "ACCOUNT CHK-M": */

int main(void){
   char *fullString = "ACCOUNT CHK-M";
   printf("%s\n", getLastChars(fullString, 2) );
   return 0;
}

0
 
dtvjhoCommented:
to always obtain a pointer to the last two characters in a string, regardless of the contents of the string, use:

m = &acct[strlen(acct)-2];
0
 
ronan_40060Author Commented:
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"

0
 
Kent OlsenData Warehouse Architect / DBACommented:

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
0
 
grg99Commented:
How about:

int Len;

Len = strlen( S );

if( Len < 2 ) return( -1 ); // string too short to have suffix
else
{
   if( S[ Len -2 ] == '-' ) // correct leading dash present
         return( S[ Len - 1 ] );   // return the character
   else
         return( -2 );   // no dash found
}

0
 
ronan_40060Author Commented:
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
0
 
Kent OlsenData Warehouse Architect / DBACommented:

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

There are a couple of caveats here.

The first line assumes that prodp contains a string of at least two characters.  If this is always true, then the first hurdle is a non-issue.  :)

The second is that if prodp always contains a string of at least two characters, then the second line will always dereference the pnam field of the prodp structure.  But of course, we're already treating prodp as a string, not a structure.  :(

Assuming that prodp IS a string, and that you really was to defererence pa->pnam, try this:

  if (prodp)  // prodp points to something
  {
    if (strlen (prodp) > 2)  // it's long enough to have meaning.
    {
      LastChar = prodp + strlen (prodp) - 2;
      if (strcmp (prodp, "-M") == 0)  // It ends with -M
      {
       // do something
      }
      else if (strcmp (prodp, "-B") == 0)  // It ends with -B
      {
        // so something
      }
      else
        // We have a data error
    }
    else
      // handle string to short error
  }
  else
    // handle null pointer error


At each of the error handlers, you can simply do a LastChar = "Some Error Message".

Does that meet your needs?
Kent
0
 
ronan_40060Author Commented:
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
0
 
Kent OlsenData Warehouse Architect / DBACommented:

>> ( 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
0
 
ronan_40060Author Commented:
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
0
 
Kent OlsenData Warehouse Architect / DBACommented:

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
0
 
ronan_40060Author Commented:
Kent
Is it OK if I
write like
(prodp? prodp->pnam + (strlen(prodp->pnam) - 2): "LastChar");
0
 
Kent OlsenData Warehouse Architect / DBACommented:

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
0
 
ronan_40060Author Commented:
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
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 9
  • 5
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now