Skip to main content
Code Review

Return to Question

Question Protected by Community Bot
Tweeted twitter.com/StackCodeReview/status/690872815407513600
deleted 2 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

(Simple) String Inverter Program Simple string inverter program

I am currently studying c programming, but I'm at a very basic level. So for practice I made a program that reads a string and inverts it. I want to know if the code is readable and how can I can improve it.

/* 
* Goal: Given a string, invert it, then print the result.
* Author: Lúcio Cardoso
*/
#include <stdio.h>
#include <string.h>
/*This is the size of the arrays in this program. It's better to use a 
constant, because makes easier to modify the arrays' size, if needed.*/
#define NUM 200
void invert(char *s) //Fucntion responsible for inverting the string.
{
 char aux[NUM];
 int i, j, n; //These variables are all used to control the for loops.
 /*This loop will read the size of the array's string, that's why
 strlen(s) - 1 is used. We don't need to read all contents
 from the array, just the string. As we read the s[] backwards,
 its content is stored in aux[n]. In this case, from zero to the
 size of s[]'s string - 1. Minus 1 is used because we read it from ZERO,
 not i. */
 for (i = strlen(s) - 1, n = 0; n < strlen(s), i >= 0; i--, n++) {
 aux[n] = s[i];
 }
 printf("Included with the super mojo from the string inverter, ");
 printf("this is the result: ");
 //This for loop reads all the content from the aux array, and print it.
 for (j = 0; j < strlen(s); j++) {
 printf("%c", aux[j]);
 }
}
int main(void)
{
 char string[NUM];
 printf("\nWelcome to the super, duper string inverter!\n");
 printf("If you want to see some magic, enter a string:\n>");
 gets(string); //Reads the string.
 //Now, we only need to call the function with string[] as a parameter.
 invert(string);
 return 0;
}

(Simple) String Inverter Program

I am currently studying c programming, but I'm at a very basic level. So for practice I made a program that reads a string and inverts it. I want to know if the code is readable and how can I improve it.

/* 
* Goal: Given a string, invert it, then print the result.
* Author: Lúcio Cardoso
*/
#include <stdio.h>
#include <string.h>
/*This is the size of the arrays in this program. It's better to use a 
constant, because makes easier to modify the arrays' size, if needed.*/
#define NUM 200
void invert(char *s) //Fucntion responsible for inverting the string.
{
 char aux[NUM];
 int i, j, n; //These variables are all used to control the for loops.
 /*This loop will read the size of the array's string, that's why
 strlen(s) - 1 is used. We don't need to read all contents
 from the array, just the string. As we read the s[] backwards,
 its content is stored in aux[n]. In this case, from zero to the
 size of s[]'s string - 1. Minus 1 is used because we read it from ZERO,
 not i. */
 for (i = strlen(s) - 1, n = 0; n < strlen(s), i >= 0; i--, n++) {
 aux[n] = s[i];
 }
 printf("Included with the super mojo from the string inverter, ");
 printf("this is the result: ");
 //This for loop reads all the content from the aux array, and print it.
 for (j = 0; j < strlen(s); j++) {
 printf("%c", aux[j]);
 }
}
int main(void)
{
 char string[NUM];
 printf("\nWelcome to the super, duper string inverter!\n");
 printf("If you want to see some magic, enter a string:\n>");
 gets(string); //Reads the string.
 //Now, we only need to call the function with string[] as a parameter.
 invert(string);
 return 0;
}

Simple string inverter program

I am currently studying c programming, but I'm at a very basic level. So for practice I made a program that reads a string and inverts it. I want to know if the code is readable and how I can improve it.

/* 
* Goal: Given a string, invert it, then print the result.
* Author: Lúcio Cardoso
*/
#include <stdio.h>
#include <string.h>
/*This is the size of the arrays in this program. It's better to use a 
constant, because makes easier to modify the arrays' size, if needed.*/
#define NUM 200
void invert(char *s) //Fucntion responsible for inverting the string.
{
 char aux[NUM];
 int i, j, n; //These variables are all used to control the for loops.
 /*This loop will read the size of the array's string, that's why
 strlen(s) - 1 is used. We don't need to read all contents
 from the array, just the string. As we read the s[] backwards,
 its content is stored in aux[n]. In this case, from zero to the
 size of s[]'s string - 1. Minus 1 is used because we read it from ZERO,
 not i. */
 for (i = strlen(s) - 1, n = 0; n < strlen(s), i >= 0; i--, n++) {
 aux[n] = s[i];
 }
 printf("Included with the super mojo from the string inverter, ");
 printf("this is the result: ");
 //This for loop reads all the content from the aux array, and print it.
 for (j = 0; j < strlen(s); j++) {
 printf("%c", aux[j]);
 }
}
int main(void)
{
 char string[NUM];
 printf("\nWelcome to the super, duper string inverter!\n");
 printf("If you want to see some magic, enter a string:\n>");
 gets(string); //Reads the string.
 //Now, we only need to call the function with string[] as a parameter.
 invert(string);
 return 0;
}
deleted 2 characters in body
Source Link
SuperBiasedMan
  • 13.5k
  • 5
  • 37
  • 62

I'mI am currently studying c programming, but I'm at a very basic level. So for practice I made a program that reads a string and inverts it. I want to know if the code is readable and how can I improve it.

/* 
* Goal: Given a string, invert it, then print the result.
* Author: Lúcio Cardoso
*/
#include <stdio.h>
#include <string.h>
/*This is the size of the arrays in this program. It's better to use a 
constant, because makes easier to modify the arrays' size, if needed.*/
#define NUM 200
void invert(char *s) //Fucntion responsible for inverting the string.
{
 char aux[NUM];
 int i, j, n; //These variables are all used to control the for loops.
 /*This loop will read the size of the array's string, that's why
 strlen(s) - 1 is used. We don't need to read all contents
 from the array, just the string. As we read the s[] backwards,
 its content is stored in aux[n]. In this case, from zero to the
 size of s[]'s string - 1. Minus 1 is used because we read it from ZERO,
 not i. */
 for (i = strlen(s) - 1, n = 0; n < strlen(s), i >= 0; i--, n++) {
 aux[n] = s[i];
 }
 printf("Included with the super mojo from the string inverter, ");
 printf("this is the result: ");
 //This for loop reads all the content from the aux array, and print it.
 for (j = 0; j < strlen(s); j++) {
 printf("%c", aux[j]);
 }
}
int main(void)
{
 char string[NUM];
 printf("\nWelcome to the super, duper string inverter!\n");
 printf("If you want to see some magic, enter a string:\n>");
 gets(string); //Reads the string.
 //Now, we only need to call the function with string[] as a parameter.
 invert(string);
 return 0;
}

I'm am currently studying c programming, but I'm at a very basic level. So for practice I made a program that reads a string and inverts it. I want to know if the code is readable and how can I improve it.

/* 
* Goal: Given a string, invert it, then print the result.
* Author: Lúcio Cardoso
*/
#include <stdio.h>
#include <string.h>
/*This is the size of the arrays in this program. It's better to use a 
constant, because makes easier to modify the arrays' size, if needed.*/
#define NUM 200
void invert(char *s) //Fucntion responsible for inverting the string.
{
 char aux[NUM];
 int i, j, n; //These variables are all used to control the for loops.
 /*This loop will read the size of the array's string, that's why
 strlen(s) - 1 is used. We don't need to read all contents
 from the array, just the string. As we read the s[] backwards,
 its content is stored in aux[n]. In this case, from zero to the
 size of s[]'s string - 1. Minus 1 is used because we read it from ZERO,
 not i. */
 for (i = strlen(s) - 1, n = 0; n < strlen(s), i >= 0; i--, n++) {
 aux[n] = s[i];
 }
 printf("Included with the super mojo from the string inverter, ");
 printf("this is the result: ");
 //This for loop reads all the content from the aux array, and print it.
 for (j = 0; j < strlen(s); j++) {
 printf("%c", aux[j]);
 }
}
int main(void)
{
 char string[NUM];
 printf("\nWelcome to the super, duper string inverter!\n");
 printf("If you want to see some magic, enter a string:\n>");
 gets(string); //Reads the string.
 //Now, we only need to call the function with string[] as a parameter.
 invert(string);
 return 0;
}

I am currently studying c programming, but I'm at a very basic level. So for practice I made a program that reads a string and inverts it. I want to know if the code is readable and how can I improve it.

/* 
* Goal: Given a string, invert it, then print the result.
* Author: Lúcio Cardoso
*/
#include <stdio.h>
#include <string.h>
/*This is the size of the arrays in this program. It's better to use a 
constant, because makes easier to modify the arrays' size, if needed.*/
#define NUM 200
void invert(char *s) //Fucntion responsible for inverting the string.
{
 char aux[NUM];
 int i, j, n; //These variables are all used to control the for loops.
 /*This loop will read the size of the array's string, that's why
 strlen(s) - 1 is used. We don't need to read all contents
 from the array, just the string. As we read the s[] backwards,
 its content is stored in aux[n]. In this case, from zero to the
 size of s[]'s string - 1. Minus 1 is used because we read it from ZERO,
 not i. */
 for (i = strlen(s) - 1, n = 0; n < strlen(s), i >= 0; i--, n++) {
 aux[n] = s[i];
 }
 printf("Included with the super mojo from the string inverter, ");
 printf("this is the result: ");
 //This for loop reads all the content from the aux array, and print it.
 for (j = 0; j < strlen(s); j++) {
 printf("%c", aux[j]);
 }
}
int main(void)
{
 char string[NUM];
 printf("\nWelcome to the super, duper string inverter!\n");
 printf("If you want to see some magic, enter a string:\n>");
 gets(string); //Reads the string.
 //Now, we only need to call the function with string[] as a parameter.
 invert(string);
 return 0;
}
deleted 66 characters in body
Source Link
SuperBiasedMan
  • 13.5k
  • 5
  • 37
  • 62

I'm am currently studying c programming, but i'm still inI'm at a very basic level. So, just for practice, I made a program that reads a string the invertand inverts it. I want want to know if the code is readable, and how can I improve it... Please, remember, i'm very newbie in programming.

I'm am currently studying c programming, but i'm still in a very basic level. So, just for practice, I made a program that reads a string the invert it. I want want to know if the code is readable, how can I improve it... Please, remember, i'm very newbie in programming.

I'm am currently studying c programming, but I'm at a very basic level. So for practice I made a program that reads a string and inverts it. I want to know if the code is readable and how can I improve it.

Source Link
Loading
lang-c

AltStyle によって変換されたページ (->オリジナル) /