EmbDev.net

Forum: ARM programming with GCC/GNU tools Equivalent functions for LeftStr, MidStr, RightStr, InStr, UpperCase, LowerCase


von listrik (Guest)


Rate this post
0 useful
not useful
Here is some code making handy functions known from Basic or Delphi, 
available in GCC too.
1
#include <stdint.h>
2
#include <stdio.h>
3
#include <string.h>
4
#include <stdbool.h>
5
6
//              0123456789AB
7
char S1[17] = {"Hello World"};  //max 16 char availabel!
8
char S2[17] = {""};        //Midstr
9
char S3[17] = {""};        //Upper-/Lowercase
10
char S4[17] = {"Wo"};      //Instring
11
char Tempstr[17];  
12
  
13
uint8_t x;
14
uint8_t position;
15
16
/* LeftStr returns the leftmost characters of a string
17
  Input:
18
    char *dst ==> pointer to the destination string
19
    char *src ==> pointer to the source string
20
    uint8_t dstsize ==> buffer size of the destination string
21
    uint8_t length ==> number of characters to extract. (1...n)
22
  Output:
23
    Pointer to the destination string
24
*/
25
char * LeftStr(char *dst, char *src, uint8_t dstsize, uint8_t length) {  
26
  if (length > (dstsize - 1)) { length = dstsize - 1; }
27
      
28
  strncpy(dst, src, length);
29
  dst[length] = '\0'; // zero terminate 
30
31
  return dst;
32
}
33
34
/* MidStr extracts a number of characters from a string
35
  Input:
36
    char *dst ==> pointer to the destination string
37
    char *src ==> pointer to the source string
38
    uint8_t dstlen ==> buffer size of the destination string
39
    uint8_t start ==> starting position within the source string. (First position is 1)
40
    uint8_t length ==> number of characters to extract. (1...n)
41
  Output:
42
    Pointer to the destination string
43
*/
44
char * MidStr(char *dst, char *src, uint8_t dstsize, uint8_t start, uint8_t length)  {  
45
  if (length > (dstsize - 1)) { length = dstsize - 1; }
46
  
47
  if (start > 0) { start--; } //call starts at 1..., internal start is 0...
48
  
49
    if (start > (strlen(src) - 1)) {
50
    dst[0] = '\0';   // out of range
51
  }
52
   else {
53
     strncpy(dst, src + start, length);
54
     dst[length] = '\0'; // zero terminate 
55
     }
56
   return dst;
57
}
58
59
/* RightStr returns the rightmost characters of a string
60
  Input:
61
    char *dst ==> pointer to the destination string
62
    char *src ==> pointer to the source string
63
    uint8_t dstlen ==> string length of the destination string
64
    uint8_t length ==> number of characters to extract. (1...n)
65
  Output:
66
    Pointer to the destination string
67
*/
68
char * RightStr(char *dst, char *src, uint8_t dstsize, uint8_t length) {  
69
  if (length > (dstsize - 1)) { length = dstsize - 1; }
70
    
71
  strncpy(dst, src + strlen(src) - length, length);
72
  dst[length] = '\0'; // zero terminate 
73
    
74
  return dst;
75
}
76
77
/* UpperCase converts characters a..z to A..Z
78
  Input:
79
    char *dst ==> pointer to the destination string
80
    char *src ==> pointer to the source string
81
    uint8_t dstlen ==> string length of the destination string
82
  Output:
83
    Pointer to the destination string
84
*/
85
char * UpperCase(char * dst, char * src, uint8_t dst_len) {
86
  uint8_t srclen;
87
   
88
  srclen= strlen(src);
89
  if ( (dst_len + 1) < srclen) srclen = dst_len; //ensure available space
90
  strncpy(dst, src, srclen); //copy max l_puffer bytes from src to dst
91
  dst[srclen + 1] = '\0';
92
  strupr(dst);
93
  return dst;
94
}  
95
        
96
/* LowerCase converts characters A..Z to a..z
97
  Input:
98
    char *dst ==> pointer to the destination string
99
    char *src ==> pointer to the source string
100
    uint8_t dstlen ==> string length of the destination string
101
  Output:
102
    Pointer to the destination string
103
*/
104
char * LowerCase(char * dst, char * src, uint8_t dst_len) {
105
  uint8_t srclen;
106
   
107
  srclen= strlen(src);
108
  if ( (dst_len + 1) < srclen) srclen = dst_len; //ensure available space
109
  strncpy(dst, src, srclen); //copy max l_puffer bytes from src to dst
110
  dst[srclen + 1] = '\0';
111
  strlwr(dst);
112
  return dst;
113
}  
114
115
/* InString looks for the occurrence of a substr in the source string
116
  Input:
117
    char *dst ==> pointer to the destination string
118
    char *src ==> pointer to the source string
119
    uint8_t sourcesize ==> buffer size of the source string
120
    uint8_t start ==> starting position within the source string. (First position is 1)
121
    bool CaseSensitive ==> true or false
122
  Output:
123
    Position of the first occurrence (1...)
124
    0 = not found
125
*/
126
uint8_t InStr(char *source,  char *substr, uint8_t sourcesize, uint8_t start, bool CaseSensitive) {  
127
  uint8_t position;
128
  char* subs;
129
  char* src;
130
  char* found;
131
132
  if (start > 0) { start--; } //call starts at 1..., internal start is 0...
133
  if (start > sourcesize - 2) { //string length - \0 - 1 
134
    position = 0;  //illegal starting position
135
  }
136
  else {
137
    if (CaseSensitive) {
138
       found = strstr(source, substr);
139
       if (found != 0) {
140
         position = found - source;   //strstr returns NULL if item not found
141
       }
142
       else {position = 0; }
143
    }
144
    else {
145
      src = malloc(sourcesize);
146
      memcpy(src, source + start, sourcesize - start);
147
    
148
      subs = malloc(strlen(substr) + 1);
149
      memcpy(subs, substr, strlen(substr) + 1);
150
    
151
      strupr(src);
152
      strupr(subs);    
153
    
154
      found = strstr(src, subs);     
155
      if (found != 0) { 
156
        position = found + start - src;   // strstr returns NULL if item not found 
157
      }
158
        else {position = 0; }    
159
    
160
      free(src);
161
      free(subs);
162
    } 
163
  }
164
  return position + 1;  //
165
}
166
167
/* PadString pads the destination string with a character
168
  Input:
169
    char *dst ==> pointer to the destination string
170
    char *src ==> pointer to the source string
171
    uint8_t destsize ==> buffer size of the destination string
172
    uint8_t start ==> starting position within the source string. (First position is 1)
173
    bool CaseSensitive ==> true or false
174
  Output:
175
    Position of the first occurrence
176
*/
177
char * PadStr(char *dst,  char *src, uint8_t dstsize, char padchar, bool padleft) {   //left/Right pad with a character
178
  uint8_t srclen;
179
  
180
  srclen = strlen(src);
181
  
182
  if (srclen > (dstsize - 1)) { srclen = dstsize - 1; }
183
184
  if (padleft) { 
185
    memset (dst, padchar, dstsize - srclen - 1); //insert padchar
186
    strncpy(dst + dstsize - srclen -1, src, srclen); 
187
  }
188
  else { 
189
    strncpy(dst, src, srclen);
190
    memset (dst + srclen, padchar, dstsize - srclen - 1); //insert padchar
191
  }
192
  
193
  dst[dstsize -1] = '\0';  
194
  return dst;
195
}
196
  
197
int main(void) {
198
   LeftStr(S2, S1, sizeof(S2), 3);            //==> "ell"
199
   MidStr(S2, S1, sizeof(S2), 2, 3);          //==> "ll"
200
   RightStr(S2, S1, sizeof(S2), 3);            //==> "rld"
201
   UpperCase(S3, S1, sizeof(S3));            //==> "HELLO WORLD"
202
   LowerCase(S3, S1, sizeof(S3));            //==> "hello world"
203
   PadStr(Tempstr,  S1, sizeof(Tempstr), '>', true);  //==> ">>>>>HELLO WORLD"
204
   PadStr(Tempstr,  S1, sizeof(Tempstr), '>', false);  //==> "HELLO WORLD>>>>>"
205
   x = InStr(S1, S4, sizeof(S1), 4, false);        //==> 7      
206
}

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.