Change CAPS/SMALL in buffer data

Hi friendz,

Que 1:
I was read buffer.and i was check string using strncmp.
Is any possible to change all data is CAPS/SMALL in buffer data?

Unsigned char *buffer = NULL; //Buffer initial

Ques 2:
Is any one know strncmpi function in C++;
i cannot able to use.
Host :Ubuntu 16

Thanks.

Kuluoz
To question 1:

You need to loop through all characters in your buffer and apply toupper or tolower to each character.
http://www.cplusplus.com/reference/cctype/
To question 2:

It's not in the standard (though a few implementations seem to add it). You would need to write your own.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cctype>
using namespace std;

int strncmpi( char *str1, char *str2, int n )
{
   for ( int i = 0; i < n; i++ )
   {
      char c1 = tolower( str1[i] ), c2 = tolower( str2[i] );
      if ( c1 < c2    ) return -1;      // includes str1 terminating, str2 not
      if ( c1 > c2    ) return  1;      // includes str2 terminating, str1 not
      if ( c1 == '\0' ) return  0;      // given the above, both must terminate here
   }
   return 0;
}



// Some testing ...

int main()
{
   char *s[] = { "abc", "ABC", "defg", "DEF", "dEfa" };
   for ( int i = 0; i < 4; i++ )
   {
      for ( int j = 0; j < 4; j++ ) cout << s[i] << "  " << s[j] << "  " << strncmpi( s[i], s[j], 3 ) << '\n';
   }
}
Thanks for all.
question to :lastchance

it is working (q:2 reply).

but i was fail in below condition.

int i=0,j=0;
char *s[] = {"myGameEnd"}; //len:9
cahr *s1[] = {"gameend"}; //len :7
cout << s[i] << " " << s[j] << " " << strncmpi( s[i], s1[j], 7 )

here gameend is exist on "s" but still return value is -1 .
"myGameEnd" and "gameend"
There is a mismatch on first pair of characters 'm' and 'g', hence they are not equal.

Perhaps you want to search for a substring inside another longer string?
See strstr() - but again this is case sensitive.
http://www.cplusplus.com/reference/cstring/strstr/
Hi Chervil,

it ll occur case sensitive problem . if any other possible ?
Some of the previous answers have suggested workarounds such as looping through the entire array and converting it to all uppercase, or all lowercase, before doing the search or compare.

There are other solutions, in the most recent versions of C++ you might try regular-expressions - but this is not something I have used:
http://www.cplusplus.com/reference/regex/basic_regex/
Last edited on
@kuluoz

Your testing code doesn't really make sense - I put indices on the arrays so that I could do a lot of comparisons quickly: you don't need them
1
2
3
4
nt i=0,j=0;
char *s[] = {"myGameEnd"}; //len:9
char *s1[] = {"gameend"}; //len :7
cout << s[i] << " " << s[j] << " " << strncmpi( s[i], s1[j], 7 )   // why the indices here? 



Try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cctype>
using namespace std;

int strncmpi( char *str1, char *str2, int n )
{
   for ( int i = 0; i < n; i++ )
   {
      char c1 = tolower( str1[i] ), c2 = tolower( str2[i] );
      if ( c1 < c2    ) return -1;      // includes str1 terminating, str2 not
      if ( c1 > c2    ) return  1;      // includes str2 terminating, str1 not
      if ( c1 == '\0' ) return  0;      // given the above, both must terminate here
   }
   return 0;
}


// Test ...

int main()
{
   char s[] = "myGameEnd";
   char s1[] = "gameend";
   int n = 6;  // change to what you want
   cout << s << "  " << s1 << "  " << strncmpi( s, s1, n ) << '\n';
}


myGameEnd  gameend  1


It produces 1 (not -1) because the first character m > g, irrespective of case.


Your original question asked for strncmpi, which is available in some computer languages and as an add-on in some implementations of c++ - but not in the standard. However, your subsequent replies suggest that (as @Chervil pointed out following your answer) you might be looking for one string inside another. That is a different problem. You need to be clear about what you actually require. Maybe you can give us some context?
Last edited on
Topic archived. No new replies allowed.