Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

This is not a valid declaration of main() even in C even in C

This is not a valid declaration of main() even in C

This is not a valid declaration of main() even in C

added 2 characters in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341

But there are already functions that do that.
You can use the scanfscanf series of commands. Your code drops any non alphabetic characters (but you could read a line then post processes to remove and invalid characters).

char data[50]; /* You wanted a max of 20 letters.
 But if the user types a space between every word
 it can get longer and we post processes to remove
 space so. So we will cut off at 50.
 */
fscanf(stdin, "%50[^\n]", data); /* read upto 50 characters
 that are not '\n'
 into data
 */
fscanf(stdin, "%*[^\n]"); /* Read the rest of the line if
 the user types more than 50 and
 throw it away
 */
fscanf(stdin, "\n"); /* Read the '\n' off the input */
/* Now remove any non letter characters and lowercase at the same time */
int removed = 0;
for(int loop = 0;data[loop];++loop0; data[loop]; ++loop)
{
 if (!isalpahisalpha(data[loop]))
 {
 removed++;
 continue; // starts next iteration.
 }
 data[loop-removed] = tolower(data[loop]);
}
data[loop-removed] = '0円'; /* Add string terminator */

But there are already functions that do that.
You can use the scanf series of commands. Your code drops any non alphabetic characters (but you could read a line then post processes to remove and invalid characters).

char data[50]; /* You wanted a max of 20 letters.
 But if the user types a space between every word
 it can get longer and we post processes to remove
 space so. So we will cut off at 50.
 */
fscanf(stdin, "%50[^\n]", data); /* read upto 50 characters
 that are not '\n'
 into data
 */
fscanf(stdin, "%*[^\n]"); /* Read the rest of the line if
 the user types more than 50 and
 throw it away
 */
fscanf(stdin, "\n"); /* Read the '\n' off the input */
/* Now remove any non letter characters and lowercase at the same time */
int removed = 0;
for(int loop = 0;data[loop];++loop)
{
 if (!isalpah(data[loop]))
 {
 removed++;
 continue; // starts next iteration.
 }
 data[loop-removed] = tolower(data[loop]);
}
data[loop-removed] = '0円'; /* Add string terminator */

But there are already functions that do that.
You can use the scanf series of commands. Your code drops any non alphabetic characters (but you could read a line then post processes to remove and invalid characters).

char data[50]; /* You wanted a max of 20 letters.
 But if the user types a space between every word
 it can get longer and we post processes to remove
 space so. So we will cut off at 50.
 */
fscanf(stdin, "%50[^\n]", data); /* read upto 50 characters
 that are not '\n'
 into data
 */
fscanf(stdin, "%*[^\n]"); /* Read the rest of the line if
 the user types more than 50 and
 throw it away
 */
fscanf(stdin, "\n"); /* Read the '\n' off the input */
/* Now remove any non letter characters and lowercase at the same time */
int removed = 0;
for(int loop = 0; data[loop]; ++loop)
{
 if (!isalpha(data[loop]))
 {
 removed++;
 continue; // starts next iteration.
 }
 data[loop-removed] = tolower(data[loop]);
}
data[loop-removed] = '0円'; /* Add string terminator */
added 426 characters in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341

One MAJOR problems here.

char sent[20] = "";

Don't use global variables. Globally mutable state makes your code hard to debug/test and generally behave correctly. You not only need to know what a function does but also what external state the function depends on. Pass all state into functions via the parameters (that way you know exactly what it depends on).

Note: Global constants are fine.

This is not a valid declaration of main() even in C

This is not a valid declaration of main() even in C

One MAJOR problems here.

char sent[20] = "";

Don't use global variables. Globally mutable state makes your code hard to debug/test and generally behave correctly. You not only need to know what a function does but also what external state the function depends on. Pass all state into functions via the parameters (that way you know exactly what it depends on).

Note: Global constants are fine.

This is not a valid declaration of main() even in C

Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341
Loading
lang-c

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