Common function operations on strings in C/C++

wcsncpy_s

wcsncpy_s : Copies the characters of one wide string to another wide string. defined in the header <wchar.h>file

errno_t wcsncpy_s(
   wchar_t *strDest,
   size_t numberOfElements,
   const wchar_t *strSource,
   size_t count
);
parameter illustrate
strDest target string
numberOfElements The size of the target string in characters
strSource resource string
count number of characters to copy

example

#include <stdio.h>
#include <wchar.h>
#include <locale.h>

int main(void)
{
    
    
    const wchar_t src[] = L"南京";
    wchar_t dest[6] = {
    
     L'北', L'京', L'上', L'海', L'深' };

    wcsncpy_s(dest, 3, src, 3);

    puts("The contents of dest are: ");
    setlocale(LC_ALL, "en_US.utf8");

    const long dest_size = sizeof dest / sizeof * dest; // dest_size = 6

    for (wchar_t* p = dest; p - dest != dest_size; ++p) {
    
    
        if (*p)
            printf("%lc ", *p);
        else
            printf("\\0 ");
    }
}

result

The contents of dest are:
南 京 \0 海 深 \0

swprintf_s

swprintf_s : Writes formatted data to a string . defined in the header <stdio.h> 或 <wchar.h>file

int swprintf_s(
   wchar_t *buffer,
   size_t sizeOfBuffer,
   const wchar_t *format,
   ...
);
parameter illustrate
buffer output storage location
sizeOfBuffer The maximum number of characters that can be stored
format format control string
Optional arguments to format

example

#include <stdio.h>

int main(void)
{
    
    
	char  buffer[200], s[] = "computer", c = 'l';
	int   i = 35, j;
	float fp = 1.7320534f;

	// Format and print various data:
	j = sprintf_s(buffer, 200, "String:%s\n", s);
	j += sprintf_s(buffer + j, 200 - j, "Character:%c\n", c);
	j += sprintf_s(buffer + j, 200 - j, "Integer:%d\n", i);
	j += sprintf_s(buffer + j, 200 - j, "Real:%f\n", fp);

	printf_s("Output:\n%s\ncharacter count = %d\n", buffer, j);
}

output

Output:
String:computer
Character:l
Integer:35
Real:1.732053

character count = 53

memset

memsetptr : Sets the first numbytes of the memory block pointed to by to the specified value (interpreted as an unsigned char). defined in the header string.hfile

void * memset ( void * ptr, int value, size_t num );
parameter illustrate
ptr pointer to the block of memory to fill
value The value to set. The value is passed as an int, but the function fills the memory block with an unsigned char conversion of the value.
num The number of bytes to set as value. size_t is an unsigned integer type.

example

/* memset example */
#include <stdio.h>
#include <string.h>

int main()
{
    
    
	char str[] = "almost every programmer should know memset!";
	memset(str, '-', 5);
	puts(str);
	return 0;
}

output

-----t every programmer should know memset!

memcmp

memcmpptr1 : Compares the first numbytes of the memory block pointed to by ptr2with numthe first bytes pointed to by and returns zero if they both match, otherwise returns a value different from zero indicating which is greater if they do not match. defined in string.h.

Note that, strcmpunlike , this function does not stop comparing after finding a null character.

int memcmp ( const void * ptr1, const void * ptr2, size_t num );
parameter illustrate
ptr1 pointer to memory block
ptr2 pointer to memory block
num the number of bytes compared
return value illustrate
<0 The value of the first byte of the mismatch in the two memory blocks in ptr1 is less than the value in ptr2 (if evaluated to an unsigned char value)
>0 The value of the first byte of the mismatch in the two memory blocks in ptr1 is greater than the value in ptr2 (if evaluated to an unsigned char value)
=0 The contents of the two memory blocks are equal
/* memcmp example */
#include <stdio.h>
#include <string.h>

int main()
{
    
    
	char buffer1[] = "DWgaOtP12df0";
	char buffer2[] = "DWGAOTP12DF0";

	int n;

	n = memcmp(buffer1, buffer2, sizeof(buffer1));

	if (n > 0) printf("'%s' is greater than '%s'.\n", buffer1, buffer2);
	else if (n < 0) printf("'%s' is less than '%s'.\n", buffer1, buffer2);
	else printf("'%s' is the same as '%s'.\n", buffer1, buffer2);

	return 0;
}

output

'DWgaOtP12df0' is greater than 'DWGAOTP12DF0'.

memcpy

memcpynum : sourceCopies the value of bytes directly from the location pointed to by to the memory block destinationpointed to by .

void * memcpy ( void * destination, const void * source, size_t num );

example

/* memcpy example */
#include <stdio.h>
#include <string.h>

struct {
    
    
  char name[40];
  int age;
} person, person_copy;

int main ()
{
    
    
  char myname[] = "Pierre de Fermat";

  /* using memcpy to copy string: */
  memcpy ( person.name, myname, strlen(myname)+1 );
  person.age = 46;

  /* using memcpy to copy structure: */
  memcpy ( &person_copy, &person, sizeof(person) );

  printf ("person_copy: %s, %d \n", person_copy.name, person_copy.age );

  return 0;
}
 Edit & Run

output

person_copy: Pierre de Fermat, 46

Guess you like

Origin blog.csdn.net/Star_ID/article/details/125566128