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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mid, left, right, trim functions 1

Status
Not open for further replies.

LinuXelite

Programmer
Jun 21, 2002
150
CA
Hi

Im looking for a way to do this in C, using a library like string.h or... I dont know. Im a beginner:

in delphi e.g:

Use strutils;

if (AnsiLeftStr(str, 3) = "123") then begin
codes...
end;

if (AnsiRightStr(str, 3) = "456") then begin
codes...
end;

if (AnsiMidStr(str, 3,1) = "3") then begin
codes...
end;

if (AnsiLowerCase(str) = "fdsjfsdjl") then begin

I use GCC and Linux.

Thank you

Frank,
 
Yes you can use the string library. For instance to do a LeftStr() type operation:
Code:
char myLeft[4];
char* instr = "hello world";
memset(myLeft, 0, 4);
strncpy(myLeft, instr, 3);
cout << myLeft << endl;

To compare strings look at strcmp and strncmp

-pete

 
This function(it's a hack), will do that for you.
You don't need separate functions for left and right.
A generic substr() function will do all this.



Code:
char *ansiSubString(char *str,int *args) {
int a , p = 0;
char *ptr = str, *buf;
 
     args[0] = args[0] <= strlen(str) ? args[0] : 0;
     if (!args[1])  {
          /* return remainder of string from first indice*/
          while (p < args[0]) {
                *ptr++;
                 p++;
          }
          return ptr;
     } else {
          args[1] = args[1] > args[0] ? args[1] : (args[0] + 1);
          args[1] = args[1] <= strlen(str) ? args[1] : strlen(str);
          buf = malloc((args[1] - args[0]) * sizeof(char));
          if (buf) {
                   while (p < args[0]) {
                         *ptr++;
                         p++;
                   }
                   
                   strncpy(buf,ptr,(args[1] - args[0]) + 1);
                   buf[(args[1] - args[0]) + 1] = '\0';
                   return buf;
          } else {
           printf(&quot;Error: malloc()\n&quot;);
           return NULL;
          }
      }
}



int main(void) {
int p = 0, margs[2];
char string[] = &quot;Test string&quot;;

             printf(&quot;Array size = %d\n&quot;, asize(margs));
             margs[0] = 15;
             printf(&quot;1 %s\n&quot;, ansiSubString(string,margs));
             margs[0] = 5; margs[1] = strlen(string);
             printf(&quot;1 %s\n&quot;, ansiSubString(string,margs));
             margs[0] = 3; margs[1] = strlen(string);
             printf(&quot;2 %s\n&quot;, ansiSubString(string,margs));
             margs[0] = 5; margs[1] = 22;
             printf(&quot;1 %s\n&quot;, ansiSubString(string,margs));
             
return 0;
}

OP:
ss2
Array size = 2
1 Test string
1 string
2 t string
1 string
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top