简体   繁体   English

字符串反转而无需在c中转数字

[英]String reverse without to turn the numbers in c

int  main()
{
 char str[1000],temp;
 int i,j=0;

 printf("String: ");
 gets(str);

 i=0;
 j=strlen(str)-1;

 while(i<j)
{
    temp=str[i];
    str[i]=str[j];
    str[j]=temp;
    i++;
    j--;
}
 printf("\n");
 printf("%s",str);
 return 0;
}

I want to change the rota 我想改变轮值

- In: 15 15 15 15 15 0 0 -输入:15 ​​15 15 15 15 0 0 0
- Out: 0 0 51 51 51 51 51 but i want : 0 0 15 15 15 15 15 -出:0 0 51 51 51 51 51但我要:0 0 15 15 15 15 15

It's because you basically mirror the string without caring about the contents of the string. 这是因为您基本上是在镜像字符串而不关心字符串的内容。 You need the code to be aware that the string contains space-delimited sub-strings, and reverse based on that. 您需要代码来注意该字符串包含以空格分隔的子字符串,并在此基础上反转。

One way is to tokenize the string (with eg strtok ) and push the sub-string onto a stack, and then recreate the string from the stack. 一种方法是标记字符串(例如使用strtok )并将子字符串推入堆栈,然后从堆栈中重新创建字符串。

You are now reversing the order of the characters in the string, but it seems that you want to reverse the order of the space-separated fields instead. 现在,您正在颠倒字符串中字符的顺序,但是似乎您想颠倒以空格分隔的字段的顺序。 To achieve this, one way would be to iterate through the array backwards and print the tail every time you encounter a space, then replace the space with a '\\0' . 为此,一种方法是向后遍历数组,并在每次遇到空格时打印尾部,然后将其替换为'\\0' Or form an array of strings with the space-separated fields as elements, then reverse the order of that array (same method as now, but dealing with pointers to char instead of char s). 或使用以空格分隔的字段作为元素形成字符串数组,然后反转该数组的顺序(与现在相同的方法,但处理的是char指针,而不是char s)。

Call this function to reverse the string. 调用此函数以反转字符串。 It accepts the string, then pushes each word onto stack until the string is empty. 它接受字符串,然后将每个单词压入堆栈,直到字符串为空。 Then it starts popping out the words from stack thereby reversing the string, but not the words. 然后,它开始从堆栈中弹出单词,从而反转字符串,而不是单词。

void rev(char *str){
    char s[20];
    int i=0;
    for(; str[i]!=' ' && str[i]!='\0'; i++)
        s[i]=str[i];
    s[i]='\0';
    if(str[i]==' ')
        rev(str+i+1);
    printf("%s ",s);
}

Steps to do: 步骤:

  1. Write a string reverse function. 编写一个字符串反向函数。 Say reverseString(char* str) reverseString(char* str)
  2. Pass the whole string to this function. 将整个字符串传递给此函数。 you will get 你会得到

    0 0 51 51 51 51 51 0 0 51 51 51 51 51

  3. Get each element by splitting for spaces in an array. 通过拆分数组中的空格来获取每个元素。 Say... 说...

    arr[0] = 0; arr [0] = 0; arr[1] = 0; arr [1] = 0; arr[2] = 51; arr [2] = 51; arr[3] = 51; arr [3] = 51; ... ...

  4. Pass individual array to string reverse function.Example: 将单个数组传递给字符串反向函数,例如:

    reverseString(arr[0]); reverseString(arr [0]); reverseString(arr[1]); reverseString(arr [1]); reverseString(arr[2]); reverseString(arr [2]); ... ...

    5 Concatenate the array and you will get 5连接数组,您将得到

    0 0 15 15 15 15 15 0 0 15 15 15 15 15

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

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