Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,52 @@
/*
* RosettaCode: Substring, C89
*
* In this task display a substring: starting from n characters in and of m
* length; starting from n characters in, up to the end of the string; whole
* string minus last character; starting from a known character within the
* string and of m length; starting from a known substring within the string
* and of m length.
*
* This example program DOES NOT make substrings. The program simply displays
* certain parts of the input string.
*
*/
#define _CRT_SECURE_NO_WARNINGS /* MSVS compilers need this */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Put no more than m characters from string to standard output.
*
* It is worth noting that printf("%*s",width,string) does not limit the number
* of characters to be printed.
*
* @param string null terminated string
* @param m number of characters to display
*/
void putm(char* string, size_t m)
{
while(*string && m--)
putchar(*string++);
}
int main(void)
{
char string[] =
"Programs for other encodings (such as 8-bit ASCII, or EUC-JP)."
int n = 3;
int m = 4;
char knownCharacter = '(';
char knownSubstring[] = "encodings";
putm(string+n-1, m ); putchar('\n');
puts(string+n+1); putchar('\n');
putm(string, strlen(string)-1); putchar('\n');
putm(strchr(string, knownCharacter), m ); putchar('\n');
putm(strstr(string, knownSubstring), m ); putchar('\n');
return EXIT_SUCCESS;
}

View file

@ -0,0 +1,56 @@
/*
* RosettaCode: Substring, C89, Unicode
*
* In this task display a substring: starting from n characters in and of m
* length; starting from n characters in, up to the end of the string; whole
* string minus last character; starting from a known character within the
* string and of m length; starting from a known substring within the string
* and of m length.
*
* This example program DOES NOT make substrings. The program simply displays
* certain parts of the input string.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Put all characters from string to standard output AND write newline.
* BTW, _putws may not be avaliable.
*/
void put(wchar_t* string)
{
while(*string)
putwchar(*string++);
putwchar(L'\n');
}
/*
* Put no more than m characters from string to standard output AND newline.
*/
void putm(wchar_t* string, size_t m)
{
while(*string && m--)
putwchar(*string++);
putwchar(L'\n');
}
int main(void)
{
wchar_t string[] =
L"Programs for other encodings (such as 8-bit ASCII).";
int n = 3;
int m = 4;
wchar_t knownCharacter = L'(';
wchar_t knownSubstring[] = L"encodings";
putm(string+n-1,m);
put (string+n+1);
putm(string, wcslen(string)-1);
putm(wcschr(string, knownCharacter), m );
putm(wcsstr(string, knownSubstring), m );
return EXIT_SUCCESS;
}

View file

@ -0,0 +1,74 @@
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *substring(const char *s, size_t n, ptrdiff_t m)
{
char *result;
/* check for null s */
if (NULL == s)
return NULL;
/* negative m to mean 'up to the mth char from right' */
if (m < 0)
m = strlen(s) + m - n + 1;
/* n < 0 or m < 0 is invalid */
if (n < 0 || m < 0)
return NULL;
/* make sure string does not end before n
* and advance the "s" pointer to beginning of substring */
for ( ; n > 0; s++, n--)
if (*s == '\0')
/* string ends before n: invalid */
return NULL;
result = malloc(m+1);
if (NULL == result)
/* memory allocation failed */
return NULL;
result[0]=0;
strncat(result, s, m); /* strncat() will automatically add null terminator
* if string ends early or after reading m characters */
return result;
}
char *str_wholeless1(const char *s)
{
return substring(s, 0, strlen(s) - 1);
}
char *str_fromch(const char *s, int ch, ptrdiff_t m)
{
return substring(s, strchr(s, ch) - s, m);
}
char *str_fromstr(const char *s, char *in, ptrdiff_t m)
{
return substring(s, strstr(s, in) - s , m);
}
#define TEST(A) do { \
char *r = (A); \
if (NULL == r) \
puts("--error--"); \
else { \
puts(r); \
free(r); \
} \
} while(0)
int main()
{
const char *s = "hello world shortest program";
TEST( substring(s, 12, 5) ); // get "short"
TEST( substring(s, 6, -1) ); // get "world shortest program"
TEST( str_wholeless1(s) ); // "... progra"
TEST( str_fromch(s, 'w', 5) ); // "world"
TEST( str_fromstr(s, "ro", 3) ); // "rog"
return 0;
}