Edit a fixed-length string in the array

Source: Internet
Author: User

The following code stores, queries, and deletes strings in an array. The maximum length of each stored string is 9 (excluding the string Terminator \ 0 ).
When a new user inputs a string, the array space based on 10 bytes is scanned sequentially. If the flag is 1, the space is valid until 0 is found and then stored.
After a string is deleted, the space for storing the string is released (the flag is set to 0) and can be saved to the new string entered by the user.
When you query a string, if the flag is 1, it is displayed. If the flag is 0, the deleted invalid string is not displayed.
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
 
// Store the total length of the string array
# Define CSTRSPACE (10000)
// Total length of the array storing the flag bit
# Define NFLAGLEN (CSTRSPACE)/(10 ))
 
Void addString (char * pcStr, int * pnFlag );
 
Void delString (char * pcStr, int * pnFlag );
 
Void searchString (char * pcStr, int * pnFlag );
 
Int main ()
{
Char cStr [CSTRSPACE] = {0 };
Int nFlag [NFLAGLEN] = {0 };
Char * pcStr = cStr;
Int * pnFlag = nFlag;
Int nUserChoose = 0;
 
While (1)
{
Printf ("1. Add string \ t \ t2. Delete string \ r \ n"
"3. Search string \ t0.exit \ r \ n ");
Printf ("please input a number: \ r \ n ");
 
NUserChoose = 0;
Scanf ("% d", & nUserChoose );
 
If (nUserChoose! = 0 & nUserChoose! = 1
& NUserChoose! = 2 & nUserChoose! = 3)
{
Printf ("Wrong choose! \ R \ n ");
}
If (0 = nUserChoose)
{
Break;
}
 
Switch (nUserChoose)
{
Case 1: addString (pcStr, pnFlag );
Break;
Case 2: delString (pcStr, pnFlag );
Break;
Case 3: searchString (pcStr, pnFlag );
Break;
}
}
 
System ("pause ");
Return 0;
}
 
// Add a string
Void addString (char * pcStr, int * pnFlag)
{
Int I = 0;
 
Printf ("please input a string: \ r \ n ");
For (I = 0; I <NFLAGLEN; I ++)
{
If (* (pnFlag + I) = 0)
{
Scanf ("% 9 s", (pcStr + I * 10 ));
Fflush (stdin );
Printf ("add succeed! \ R \ n ");
* (PnFlag + I) = 1;
Break;
}
}
If (NFLAGLEN = I)
{
Printf ("No Space! \ R \ n ");
}
}
 
// Delete the string www.2cto.com
Void delString (char * pcStr, int * pnFlag)
{
Int I = 0;
 
SearchString (pcStr, pnFlag );
 
Printf ("please input a number: \ r \ n ");
Scanf ("% d", & I );
 
// The data is not cleared, but the flag position is 0.
* (PnFlag + I) = 0;
Printf ("delete succeed! \ R \ n ");
}
 
// Query all strings
Void searchString (char * pcStr, int * pnFlag)
{
Int I = 0;
 
Printf ("\ r \ n ");
For (I = 0; I <NFLAGLEN; I ++)
{
// The flag is 1.
If (* (pnFlag + I) = 1)
{
Printf ("% d: \ t % s \ r \ n", I, (pcStr + I * 10 ));
}
}
Printf ("\ r \ n ");
}
 
PS: It is relatively simple to store fixed-length strings. If the storage length is uncertain and a new string is added after the string is deleted, you need to check whether the space of the deleted string can be stored to add strings. If not, find the next bucket. In another case, if the continuous space is insufficient to store long strings entered by the user, the storage can be split. However, after the storage is split, the query and Display need to be re-spliced. Or shift the stored strings in the array and squeeze out the deleted string space, so that the continuous space will become longer. This situation is similar to the overall disk fragmentation. This part will be posted after it is written.
First, play with the array in the array, and then change the storage medium, such as the hard disk, should be much easier, just changed several functions.

 


From Elijah Wong

Related Article

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.