Jinshan (function Exercise 2) _ character sorting (letters, numbers, and other characters) parsestring

Source: Internet
Author: User
function Exercise 2

write a function to implement a given string (the string contains letters, number, symbol) processing. The content of the processed string is stored in the order of letters, numbers, and symbols. Function declaration:
void parsestring (char * pstr);
requirement:
. function declaration cannot be modified.
B. the original sequence of occurrence of letters, numbers, and so on in the string is not changed.
C. direct use of pstr value refers to the buffer zone, and separate buffer zones are not allowed.
for example, the given string is a, 2.d? 3! E4r87we79...
the output result is: aderwe2348779 ,.?!...

I thought about this question myself. the idea at the beginning is to first judge the letters in the string, if it is a letter, it will not matter, otherwise it will be taken out, and all the characters after this character are moved forward. in this way, the extracted characters are placed in the Last space of the string.
But the idea is that when writingCodeBut I don't know how to get started. So I found it on the Internet and found it in a blog. I'm so happy! Try again immediately. However, the result is that only letters, numbers and other characters can be distinguished, and the original sequence of appearance is messy.
So, please do it yourself. If you want to find someone else, you might as well do it yourself. Do you commonly say you want to get yourself done !!!
Finally, I came up with a better method than I did for the first time.Algorithm, As follows:
A, 2 D? 3!
1) First, judge whether it is a letter. If it is, extract the letter first, and move the first character to the last character.
2) the corresponding number is to change the judgment function. The specific code is at the bottom:

Programming summary:
In this programming question, I found that my string array is not very familiar. A char * sptr function parameter is not a char array in main ().
And the call function of parsestring (char * sptr) is parsestring (s), and S is Char []. In the function, you can directly use indexes to call a value in the array.
The biggest achievement is to find that my code writing skills are poor. The reason should be that I did not practice much and had a solid foundation. The solution I want to solve is to stick to the exercises and review,
Make a summary.

# Include <stdio. h>
# Include <string. h>

Void parsestring (char * pstr)
{
Char tempch;
Int I = 0, j = 0, K = 0;
// Counter I indicates the number of characters. j indicates the number of letters (or numbers). k indicates the counter used to move characters.
While (pstr [I]! = '\ 0 ')
{
If (isalpha (pstr [I]) // if it is a letter, extract it first, and then move the first to the last letter to the next, last placed after the last letter
{
Tempch = pstr [I];
For (k = I; k> = J; k --)
{
Pstr [k] = pstr [k-1];
}
Pstr [J] = tempch;

J ++;
}
I ++;
}

I = J; // all the letters are put. If there are a few letters, we can count them from the first few characters, because only numbers and other characters are left unsorted

while (pstr [I]! = '\ 0')
{< br> If (isdigit (pstr [I])
{< br> tempch = pstr [I];
for (k = I; k> = J; k --)
{< br> pstr [k] = pstr [k-1];
}< br> pstr [J] = tempch;

J ++;
}

I ++;
}
}

Int main ()
{
Char s [100];

Scanf ("% s", & S );

Parsestring (s );

Printf ("% s", S );

// Scanf ("% s", & S );

Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.