Skip to content
This repository has been archived by the owner on Mar 12, 2023. It is now read-only.

Commit

Permalink
expanded readme
Browse files Browse the repository at this point in the history
  • Loading branch information
MattMoony committed Mar 21, 2019
1 parent 879c9f8 commit ca047bc
Show file tree
Hide file tree
Showing 16 changed files with 219 additions and 15 deletions.
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2019 Matthias M.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
202 changes: 201 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
![GitHub release](https://img.shields.io/github/release/MattMoony/propaganda.c.svg) ![GitHub top language](https://img.shields.io/github/languages/top/MattMoony/propaganda.c.svg) ![GitHub last commit](https://img.shields.io/github/last-commit/MattMoony/propaganda.c.svg) ![GitHub All Releases](https://img.shields.io/github/downloads/MattMoony/propaganda.c/total.svg) ![GitHub repo size in bytes](https://img.shields.io/github/repo-size/MattMoony/propaganda.c.svg) ![GitHub](https://img.shields.io/github/license/MattMoony/propaganda.c.svg)

# propaganda.c
_A C-library for string manipulations_

Expand Down Expand Up @@ -511,7 +513,205 @@ printf("Trimmed: %s\n", trimmed);
![exa23_output](media/23.JPG)
**-- to be continued --**
#### Integer to character
In order to convert an integer to its character representation, you can use the `itoc((int) n)`.
```C
// ...
int number = 8;
char n_chr = itoc(8);
printf("As character: %c\n", n_chr);
// ...
```

![exa24_output](media/24.JPG)

#### Character to integer

You can also do it the other way around by using the `ctoi((char) c)` function.

```C
// ...

char n_chr = '2';
int number = ctoi(n_chr);

printf("As integer: %d\n", number);

// ...
```
![exa25_output](media/25.JPG)
#### Convert to string
The following are all functions to convert several datatypes to a _string_.
* ##### Integer to string
To convert an integer to a _string_, use the `itos((int) n)` function.
```C
int my_int = 802;
char* my_str = itos(my_int);
printf("String: %s\n", my_str);
```

![exa26_output](media/26.JPG)

* ##### Boolean value to string
To convert a boolean value to a _string_, you should use the `btos((int) b)` function.

```C

int my_boo = 0;
char* my_str = btos(my_boo);

printf("String: %s\n", my_str);

```

![exa27_output](media/27.JPG)

* ##### Long to string
To convert a long to a _string_, you can use the `ltos((long) n)` function.

```C
// ...

long my_lon = 8000000l;
char* my_str = ltos(my_lon);

printf("String: %s\n", my_str);

// ...
```

![exa28_output](media/28.JPG)

* ##### Float to string
To convert a float to a _string_, use the `ftos((float) n, (int) r)` function.

**Note:** The `r` parameter specifies how many decimals to round to.

```C
// ...

float my_flo = 3.141f;
char* my_str = ftos(my_flo, 3);

printf("String: %s\n", my_str);

// ...
```

![exa29_output](media/29.JPG)

* ##### Double to string
To convert a double to a _string_, you should use the `dtos((double) n, (int) r)` function.

**Note:** The `r` parameter specifies to how many decimals the resulting "string" should be rounded.

```C
// ...

double my_dou = 3.14159265359;
char* my_str = dtos(my_dou, 10);

printf("String: %s\n", my_str);

// ...
```

![exa30_output](media/30.JPG)

#### Convert from string

The following are all functions to convert a _string_ to several, different datatypes.
* ##### String to integer
To convert a _string_ to an integer, use the `stoi((char*) str)` function.

```C
// ...

char* my_str = "1024";
int my_int = stoi(my_str);

printf("Integer: %d\n", my_int);

// ...
```

![exa31_output](media/31.JPG)

* ##### String to boolean value
To convert a _string_ to a boolean value, you should use the `stob((char*) str)` function.

```C
// ...

char* my_str = "true";
int my_boo = stob(my_str);

printf("Boolean: %d\n", my_boo);

// ...
```

![exa32_output](media/32.JPG)

* ##### String to long
To convert a _string_ to a long, you can use the `stol((char*) str)` function.

```C
// ...

char* my_str = "8000000";
long my_lon = stol(my_str);

printf("Long: %ld\n", my_lon);

// ...
```

![exa33_output](media/33.JPG)

* ##### String to float
To convert a _string_ to a float, use the `stof((char*) str)` function.

```C
// ...

char* my_str = "3.141";
float my_flo = stof(my_str);

printf("Float: %f\n", my_flo);

// ...
```

![exa34_output](media/34.JPG)

* ##### String to double
To convert a _string_ to a double, you can use the `stod((char*) str)` function.

```C
// ...

char* my_str = "3.14159265359";
double my_dou = stod(my_str);

printf("Double: %.15lf\n", my_dou);

// ...
```

![exa35_output](media/35.JPG)

## Conclusion

Expand Down
9 changes: 2 additions & 7 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

int main()
{
/*char* msg = newstr("Hello World!");
char* msg = newstr("Hello World!");
char* cpy = strcpy(msg);

cpy[0] = 'A';
Expand Down Expand Up @@ -100,12 +100,7 @@ int main()
printf("->stof:\t%f\n", stof("8.5096"));
printf("->stod:\t%.10lf\n", stod("8.0123456789"));

printf("\n");*/

char* msg = newstr(" \n\rHello World!\n ");
char* trimmed = trim(msg);

printf("Trimmed: %s\n", trimmed);
printf("\n");

char c;
scanf("%c", &c);
Expand Down
Binary file added media/24.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/25.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/26.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/27.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/28.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/29.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/30.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/31.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/32.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/33.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/34.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/35.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 7 additions & 7 deletions propaganda.c
Original file line number Diff line number Diff line change
Expand Up @@ -612,8 +612,6 @@ char* ftos(float n, int l) {
for (int i = 0; i < l-1; i++) {
n *= 10;
res_s = strcat(res_s, itos((int) n));
printf("%f\n", n);
printf("%s\n", res_s);
n -= (int) n;
}

Expand Down Expand Up @@ -721,8 +719,9 @@ float stof(char* str) {
float res = (float) stoi(substring(str, 0, indexOf(str, '.')));

char* after_c = substr(str, indexOf(str, '.')+1);
float af_c = (float) stoi(after_c);
res += af_c * (float) pow(10, -strlen(after_c));
for (int i = 0; i < strlen(after_c); i++) {
res += ctoi(after_c[i]) * (float) pow(10, -i-1);
}

return res;
}
Expand All @@ -736,13 +735,14 @@ float stof(char* str) {

double stod(char* str) {
if (indexOf(str, '.') < 0)
return 0.0f;
return 0.0;

double res = (double) stoi(substring(str, 0, indexOf(str, '.')));

char* after_c = substr(str, indexOf(str, '.')+1);
double af_c = (double) stoi(after_c);
res += af_c * pow(10, -strlen(after_c));
for (int i = 0; i < strlen(after_c); i++) {
res += ctoi(after_c[i]) * pow(10, -i-1);
}

return res;
}

0 comments on commit ca047bc

Please sign in to comment.