简体   繁体   English

反转没有库 function 的字符串

[英]Reversing the string without library function

The code got compiled successfully.代码编译成功。 but I can't reverse the string.但我无法反转字符串。 since I am a beginner-level programmer, I can't really figure out the mistake I made.因为我是一个初学者级的程序员,我真的无法弄清楚我犯的错误。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {      
    int temp, i, j, length;
    char name[20], name1[20];

    printf(" Enter string \n");
    scanf("%s", name);
    length = strlen(name);
    printf(" %d", length);
    for (i = length - 1; i >= 0; i--) {
        name1[length - i] = name[i];
    }
    printf("%S ", name1);
    
    return 0;
}

Use lower case 's' in print:在打印中使用小写“s”:

printf("%s ",name1);

Similar codes: https://www.programmingsimplified.com/c-program-reverse-string类似代码: https://www.programmingsimplified.com/c-program-reverse-string

The issues I noticed:我注意到的问题:

  • length-i will be off by +1 here: length-i将在此处关闭 +1:
     name1[length-i]=name[i];
  • You forgot to add the null terminator ( \0 ).您忘记添加 null 终止符 ( \0 )。
  • %S is a non-standard printf formatter. %S是一个非标准的printf格式化程序。 You probably want %s .您可能想要%s

Suggested changes:建议更改:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    char name[20], name1[20];

    printf(" Enter string \n");

    if(scanf("%19s", name) != 1) {         /* check that input works */
        fprintf(stderr, "input failure\n");
        return 1;
    }

    int length = strlen(name);

    printf(" %d\n", length);

    for(int i = 0; i < length; ++i) {
        name1[length - i - 1] = name[i];  /* corrected indexing */
    }

    name1[length] = '\0';                 /* add null terminator */

    printf("%s\n", name1);                /* %s instead of %S */
}

You can try this one:-你可以试试这个:-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int i, length;
    char name[20], temp;
    printf(" Enter string \n");
    scanf("%s", name);
    length = strlen(name);
    printf(" %d", length);
    for(i = 0; i < length /2; i++)
    {
        temp = name[i];
        name[i] = name[length-i-1];
        name[length-i-1] = temp;
    }
    printf("\n%s", name);
    return 0;
}

Here are some issues in your code:以下是您的代码中的一些问题:

  • you should limit the number of characters stored into name with %19s .您应该使用%19s限制存储在name中的字符数。

  • name1[length - i] = name[i]; does not use the correct offset.不使用正确的偏移量。 Try the first iteration: i is length-1 you copy the last character to position length - (length - 1) hence position 1 instead of 0 .尝试第一次迭代: i is length-1您将最后一个字符复制到 position length - (length - 1)因此 position 1而不是0 Use this instead:改用这个:

     name1[length - i - 1] = name[i];
  • you must set a null terminator at the end of the reversed string:您必须在反转字符串的末尾设置一个 null 终止符:

     name1[length] = '\0';
  • in the printf() call, %S is incorrect.printf()调用中, %S不正确。 Use %s instead and append a newline.改用%s和 append 换行。

Here is a modified version:这是修改后的版本:

#include <stdio.h>

int main() {      
    char name[20], name1[20];
    int i, length;

    printf(" Enter string:\n");
    if (scanf("%19s", name) == 1) {
        length = strlen(name);
        printf("%d\n", length);
        for (i = 0; i < length; i++) {
            name1[length - i - 1] = name[i];
        }
        name1[length] = '\0';
        printf("%s\n", name1);
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM