Home c Row Symbol Output in reverse order

Row Symbol Output in reverse order

Author

Date

Category

It is impossible to write a program that reads the input line, and then prints this line in the reverse order. By condition, you can memorize the input in the array of values ​​of the char values; It is assumed that the SO string is not more than 255 characters. It is necessary to use the scanf () function with the% C specifier to perform the input device. How?

# include & lt; stdio.h & gt;
#Include & lt; locale.h & gt;
#Include & lt; String.h & gt;
INT MAIN (Void)
{
  SETLOCALE (LC_ALL, "RUS");
  SETLOCALE (LC_NUMERIC, "C");
  Char Str [255];
  int i, j;
  PrintF ("Enter a string: \ n");
  for (i = 0; i & lt; 255; i ++)
    Scanf ("% C", & amp; STR);
  for (j = strlen (STR); j & gt; = 0; j--)
    PrintF ("% C", STR [J]);
  Return 0;
}

That’s it:

# include & lt; stdio.h & gt;
#Include & lt; locale.h & gt;
#Include & lt; String.h & gt;
INT MAIN (Void)
{
  SETLOCALE (LC_ALL, "RUS");
  SETLOCALE (LC_NUMERIC, "C");
  Char Str [255];
  int i, j;
  PrintF ("Enter a string: \ n");
  scanf ("% s", str);
  // for (i = 0; i & lt; 255; i ++)
    // scanf ("% c", & amp; str);
  for (j = strlen (STR); j & gt; = 0; j--)
    PrintF ("% C", STR [J]);
  Return 0;
}

And through the% C specifier, in case, as by the condition of the task – no .. ??


Answer 1, Authority 100%

char str [255];

255 characters in a string and more zolik – so you need 256.

char str [256];
for (i = 0; i & lt; 255; i ++)
 Scanf ("% C", & amp; STR);

Taking the address of the array is incorrect. An array is the address of the first symbol.

for (i = 0; i & lt; 255; i ++)
  Scanf ("% C", STR);

The output condition is a row translation, and not the achievement of 255 characters:

for (i = 0; scanf ("% c", str), * str! = '\ n' ; I ++);

Next, the characters must be written in series, and not all in the zero position:

for (i = 0; scanf ("% c", str + i), * str [i]! = '\ n'; i ++);

The string must end with zero byte.
After the I cycle, I indicates the translation of the line, and we do not need it, replace the zolik:

str [i] = 0;
for (j = strlen (str); j & gt; = 0; j--)
 PrintF ("% C", STR [J]);

Length is already in I – you can use it.

while (- i! = -1)
 PrintF ("% C", STR [J]);

or so

for (- i; i--;)
 PrintF ("% C", STR [I]);

Symbol output is better to use Putchar Ohm, and not printf Ohm:

while (- i! = -1)
 Putchar (STR [I]);

In general, I would use pointers, not indexes:

CHAR S [256], p;
For (* (p = s + 255) = 0; p & gt; S & amp; & amp; (scanf ("% c", - p), * p! = '\ n'););
Puts (P);

It seems that the requirements of the task are made?


Answer 2

# include & lt; stdio.h & gt;
#Include & lt; locale.h & gt;
#Include & lt; String.h & gt; // for strlen.
#Define Size 256.
INT MAIN (Void)
{
SETLOCALE (LC_ALL, "RUS");
SETLOCALE (LC_NUMERIC, "C");
Char Line [Size];
int i;
PrintF ("Enter a string: \ n"); 
For (i = 0; Scanf ("% C", & amp; line [i]) == 1 & amp; & amp; line [i]! = '\ n'; i ++)
   ;
Line [i] = '\ 0';
For (i = strlen (Line) - 1; i & gt; = 0; i--)
   PrintF ("% C", Line [i]);
Return 0;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions