当前位置:网站首页>[C language classic]: inverted string

[C language classic]: inverted string

2022-06-11 02:37:00 Young master KC

【C Language classics 】: Inverted string


link : https://www.nowcoder.com/questionTerminal/b0ae3d72444a40e2bb14af7d4cb856ba
source : Cattle from

Invert the words of a sentence , Punctuation is not inverted . such as I like beijing. After passing through the function, it becomes :beijing. like I

Input description :
 Each test input contains 1 Test cases : I like beijing.  The length of the input case shall not exceed 100
Output description :
 Output the inverted string in turn , Split by space 

Example 1

Input

I like beijing.

Output

beijing. like I

Thought analysis

We know by observation that , Reverse the whole string first , This problem can be solved by reversing each word in the string .

  1. Reverse the entire string
  2. Reverse the order of each word in the string

As shown in the figure

 Insert picture description here

matters needing attention

Out of commission scanf, because scanf Abort as soon as a space is encountered , have access to gets(); perhaps gets_s();

gets_s() Use

First of all, the string is inversely independent into a function of inversely ordered characters

*str Is the first address of the string ,right Is the subscript of the last letter in the string ,

void reseve(char* str, int left, int right)
{
    
    while (left < right)
    {
    
        char ch = str[left];
        str[left] = str[right];// Exchange string 
        str[right] = ch;
        left++;
        right--;
    }
}

Second, reverse the order of each word in the string

     int temp = 0;// The subscript of the first element of the array 
    for (int i = 0; i < len + 1; i++).// Traversal string 
    {
    
        if (arr[i] == ' ' || arr[i] == 0)// Encountered a space or \0 You need words in reverse order 
        {
    
            reseve(arr, temp, i - 1);// Call the array in reverse order , i Is a space or \0 The subscript  i-1 Subscript the last letter of a word 
            temp = i + 1;// The subscript of the first letter of the next word 
        }
    }

All the code

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

void reseve(char* str, int left, int right)
{
     
   while (left < right)
   {
     
       char ch = str[left];
       str[left] = str[right];
       str[right] = ch;
       left++;
       right--;
   }
}

int main()
{
     
   char arr[100] = {
      0 };
   gets_s(arr,100);
   int len = strlen(arr);
   reseve(arr, 0, len - 1);

   
   int temp = 0;
   for (int i = 0; i < len + 1; i++)
   {
     
       if (arr[i] == ' ' || arr[i] == 0)
       {
     
           reseve(arr, temp, i - 1);
           temp = i + 1;
       }
   }

   printf("%s", arr);
   return 0;
}
Conclusion

​ Upper couplet : The world is so big , I want to see it ; Lower bound : The wallet is so small , No one can leave ; Streamer : Go to work well .

原网站

版权声明
本文为[Young master KC]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020606584787.html