commit d5f64b53a0189299208f7107650b762f34b19163
parent 6324e62dbf15a73fe5380bb7ba0ca7c77818c803
Author: klewer-martin <martin.cachari@gmail.com>
Date: Mon, 31 May 2021 15:30:18 -0300
Update: corrected conditions like comparisons with 0, and comments made with '//'
Diffstat:
10 files changed, 92 insertions(+), 101 deletions(-)
diff --git a/guia03/ex15.c b/guia03/ex15.c
@@ -9,13 +9,14 @@ int main(void) {
fgets(str, MAX_LEN, stdin);
- j = 0;
- while(str[j] == ' ')
- j++;
+ /* Count consecutive starting blank spaces */
+ for(i = 0; str[i] == 32; i++)
+ ;
- for(i = 0; str[i] != '\0'; i++) {
- str[i] = str[i + j];
- }
- puts(str);
+ /* Shift the string i times to the left */
+ for(j = 0; str[j]; j++)
+ str[j] = str[j + i];
+
+ printf("%s", str);
return 0;
}
diff --git a/guia03/ex16.c b/guia03/ex16.c
@@ -1,28 +1,30 @@
-// Reads a string from stdin and if it ends in blank
-// spaces it erase them and moves the '\n', '\0' chars
-// it performs a "right trim"
-
+/* Reads a string from stdin and if it ends in blank
+ * spaces it erase them and move the '\0' char
+ * it performs a "right trim"
+*/
#include <stdio.h>
#define MAX_LEN 100
-
-int main ( void ) {
-
+int
+main (void)
+{
int i, j, k;
char str[MAX_LEN];
- if(fgets(str, MAX_LEN, stdin) == NULL)
+ if(!fgets(str, MAX_LEN, stdin))
return 1;
- for(i = 0; str[i] != '\0'; i++)
+ /* Goes to end of string, then starts counting trailing spaces */
+ for(i = 0; str[i]; i++)
if(str[i + 1] == '\n')
- for(j = 0; str[i - j] == ' '; j++)
+ for(j = 0; str[i - j] == 32; j++)
;
-
- str[(i - j) - 1] = str[i - 1];
- str[(i - j)] = '\0';
+
+ /* Places the '\0' character j times 'til end of string */
+ str[i - j] = '\0';
+ printf("%s", str);
return 0;
}
diff --git a/guia03/ex17.c b/guia03/ex17.c
@@ -1,33 +1,28 @@
+/* Makes a case sensitive search of the s2 in s1 */
+
#include <stdio.h>
#include <string.h>
#define MAX_LEN 100
-
-int main ( void ) {
-
- int i, j;
-
+int
+main (void)
+{
char s1[MAX_LEN];
char s2[MAX_LEN];
-
- if(fgets(s1, MAX_LEN, stdin) == NULL)
- return 1;
- if(fgets(s2, MAX_LEN, stdin) == NULL)
- return 1;
+ /* Get two strings from stdin */
+ if(!fgets(s1, MAX_LEN, stdin)) return 1;
+ if(!fgets(s2, MAX_LEN, stdin)) return 1;
- if(strlen(s2) > strlen(s1))
- return 1;
+ if(strlen(s2) > strlen(s1)) return 1;
- for(i = 0; s1[i] != '\0'; i++)
+ for(size_t i = 0; s1[i]; i++)
if(s1[i] == s2[0])
- for(j = 0; (s2[j] != '\0'); j++)
- if(s1[i] == s2[j]) {
- putchar(s1[i]);
- i++;
- }
- putchar('\n');
+ for(size_t j = 0; s2[j]; j++)
+ if(s1[i] == s2[j])
+ putchar(s1[i++]);
+
return 0;
}
diff --git a/guia03/ex18.c b/guia03/ex18.c
@@ -1,57 +1,51 @@
-// Reads a string of chars from stdin and check if it's
-// a palindrome.
+/* Reads a string of chars from stdin and check if it's
+ a palindrome. */
#include <stdio.h>
#include <ctype.h>
#define MAX_LEN 100
-
-int main ( void ) {
-
+int
+main (void)
+{
char str[MAX_LEN];
- int aux, len, i, j;
+ int len, i, j;
- if(fgets(str, MAX_LEN, stdin) == NULL)
+ if(!fgets(str, MAX_LEN, stdin))
return 1;
-// Converts all the string to lowercase;
+ /* Converts all the string to lowercase; */
for(i = 0; str[i] != '\0'; i++)
if(isupper(str[i]))
str[i] = tolower(str[i]);
-// If the readed string it's a sentence, erases all
-// the blank spaces between the words;
- for(i = 0; str[i] != '\0'; i++) {
- if(str[i] == ' ') {
- aux = i;
- while(str[aux] != '\0') {
- str[aux] = str[aux + 1];
- aux++;
- }
- }
- }
-
-// Counts the length of the string;
+ /* Erases all the blank spaces between words */
+ for(i = 0; str[i]; i++)
+ if(str[i] == ' ')
+ for(size_t j = i; str[j]; j++)
+ str[j] = str[j + 1];
+
+ /* Counts the length of the string; */
for(i = 0; str[i] != '\n'; i++)
;
-// Stored the lengh of the string for later,
-// and subtract one from i because of the last char;
+ /* Stored the lengh of the string for later, */
+ /* and subtract one from i because of the last char; */
len = i;
i = (i - 1);
-// This part compares one by one the chars from the
-// string, first with last, second with before last,
-// and so on, only comparing the next one if the previous
-// one was equal;
- for(j = 0; (str[j] == str[i]) && (str[j] != '\0'); j++, i--)
+ /* This part compares one by one the chars from the */
+ /* string, first with last, second with before last, */
+ /* and so on, only comparing the next one if the previous */
+ /* one was equal; */
+ for(j = 0; (str[j] == str[i]) && str[j]; j++, i--)
;
-// If the string or sentence its palindrome, then in the previous
-// block j would end with the value of the lengh, stored previously;
+ /* If the string or sentence its palindrome, then in the previous */
+ /* block j would end with the value of the lengh, stored previously; */
if(j == len)
- printf("La cadena es capicua.\n");
+ printf("%s\n", "La cadena es capicua.");
return 0;
}
diff --git a/guia03/ex20.c b/guia03/ex20.c
@@ -1,22 +1,23 @@
-// Reads a string of chars from stdin and converts
-// it to a number, int or float;
+/* Reads a string of chars from stdin and converts
+ it to a number, int or float; */
#include <stdio.h>
#include <stdlib.h>
-#define MAX_LEN 100
-
-int main ( void ) {
+#define MAX_LEN 26
+int
+main (void)
+{
char buffer[MAX_LEN];
int num1;
float num2;
- fgets(buffer, MAX_LEN, stdin);
+ if(!fgets(buffer, MAX_LEN, stdin)) return 1;
num1 = atoi(buffer);
printf("n1(int) = %d\n", num1);
- fgets(buffer, MAX_LEN, stdin);
+ if(!fgets(buffer, MAX_LEN, stdin)) return 1;
num2 = atof(buffer);
printf("n2(float) = %.2f\n", num2);
diff --git a/guia03/ex21.c b/guia03/ex21.c
@@ -9,8 +9,7 @@ int main ( void ) {
float num;
char num2[MAX_LEN];
- if(fgets(buffer, MAX_LEN, stdin) == NULL)
- return 1;
+ if(!fgets(buffer, MAX_LEN, stdin)) return 1;
num = atof(buffer);
sprintf(num2, "%.2f\n", num);
diff --git a/guia03/ex23.c b/guia03/ex23.c
@@ -14,10 +14,10 @@ int main ( void ) {
num = atoi(buffer);
-// Converts num to octal and stores it on num2 str;
+ /* Converts num to octal and stores it on num2 str; */
sprintf(num2, "%o\n", num);
-// prints the string with the octal number;
+ /* prints the string with the octal number; */
printf("%s", num2);
return 0;
}
diff --git a/guia03/ex23_imp.c b/guia03/ex23_imp.c
@@ -1,5 +1,5 @@
-// Reads an integer from stdin and prints it
-// on stdout in octal base;
+/* Reads an integer from stdin and prints it
+ on stdout in octal base; */
#include <stdio.h>
#include <stdlib.h>
@@ -7,22 +7,21 @@
#define MAX_LEN 100
#define ERR_MSG_NEG "Err: the number is negative"
-int main ( void ) {
-
+int main (void)
+{
int num;
char buffer[MAX_LEN];
- if(fgets(buffer, MAX_LEN, stdin) == NULL)
- return 1;
+ if(!fgets(buffer, MAX_LEN, stdin)) return 1;
-// Converts the input str to int, if the number
-// is negative, puts an error message on stderr;
+/* Converts the input str to int, if the number
+ is negative, puts an error message on stderr; */
if((num = atoi(buffer)) < 0 ) {
- fprintf(stderr, ERR_MSG_NEG"\n");
+ fprintf(stderr, "%s\n", ERR_MSG_NEG);
return 1;
}
-// prints the integer in octal base;
+/* prints the integer in octal base; */
printf("%o\n", num);
return 0;
}
diff --git a/guia03/ex24.c b/guia03/ex24.c
@@ -1,5 +1,5 @@
-// Reads a number from stdin and prints it
-// in hexadecimal base on stout;
+/* Reads a number from stdin and prints it
+ in hexadecimal base on stout; */
#include <stdio.h>
#include <stdlib.h>
@@ -17,11 +17,11 @@ int main ( void ) {
num = atoi(buffer);
-// Converts num to hexadecimal and stores it
-// on num2 str;
+ /* Converts num to hexadecimal and stores it
+ on num2 str; */
sprintf(num2, "%x\n", num);
-// prints the string with the octal number;
+ /* prints the string with the octal number; */
printf("%s", num2);
return 0;
}
diff --git a/guia03/ex24_imp.c b/guia03/ex24_imp.c
@@ -1,5 +1,5 @@
-// Reads an non-negative integer from stdin and prints it
-// on stdout in hexadecimal base;
+/* Reads an non-negative integer from stdin and prints it
+ on stdout in hexadecimal base; */
#include <stdio.h>
#include <stdlib.h>
@@ -15,14 +15,14 @@ int main ( void ) {
if(fgets(buffer, MAX_LEN, stdin) == NULL)
return 1;
-// Converts the input str to int, if the number
-// is negative, puts an error message on stderr;
+ /* Converts the input str to int, if the number
+ is negative, puts an error message on stderr; */
if((num = atoi(buffer)) < 0 ) {
fprintf(stderr, ERR_MSG_NEG"\n");
return 1;
}
-// prints the integer in hexadecimal base;
+ /* prints the integer in hexadecimal base; */
printf("%X\n", num);
return 0;
}