Home > Back-end >  A great god, and seek help. Call get string function to the English sentence read in 2 d character a
A great god, and seek help. Call get string function to the English sentence read in 2 d character a

Time:12-31

Comprehensive practice knowledge, function, array, the file

Create a user library strpro. H and openfile. H, including strpro. H contains get string, sort and read function, the openfile. H contains openw function,
Prompt the user for a word in the main function, requirements:

1. Call get string function to the English sentence read in 2 d character array, and then call sort () function, by choice or bubbling algorithm for the ordered by the word in the sentence above (in alphabetical order),
2. Will be sorted sentences written to disk file data. TXT, request to open the data. TXT file openw function called operation must be completed, when opened, please check the file with the same exists, and prompt the user whether to cover the original file,
3. Call function will read the data. The contents of the TXT read back display on the screen
Problem: the bubble has a problem, flash back,
The code is as follows:
# pragma once
# define LSIZE 81
Char string [] [LSIZE]={0};
Int get string ();
Void the sort ();
Void the read ();
Int I=0, j=0;
Int get string ()
{
char c;
While ((c=getchar ())! )
='\ n'{
If (c==' ')
{
i++;
J=0;
}
The else
{
String [I] [j]=c;
j++;
}
}
J=0;
Printf (" % d \ n ", I);
Return (I);
}

Void the sort ()
{
Char temString [LSIZE]={0};
Int a, b, c;
for (a=0; a{
B=a;
For (c=a + 1; C{
If (STRCMP (string [b], string [c]) & gt; 0)
B=c.
If (b!
=a){
Strcpy_s (temString, LSIZE, string [a]);
Strcpy_s (string [a], LSIZE, string [b]);
Strcpy_s (string [b], LSIZE temString);
}
}
}
}

Void the read ()
{
The FILE * fp.
Char STR [LSIZE]={} '\ 0';
Fp=fopen (" data. TXT ", "r");
While (fscanf (fp, "% s", STR)!=(EOF)
Puts (STR);
fclose(fp);
}

CodePudding user response:

You this function has no parameters, also don't have a parameter of it? And access to the string you this two-dimensional array is not very understanding, is to require 2 d? One dimension is not ok? (see you put characters) emmmm too difficult, leave a building looking screen to learn bosses

CodePudding user response:

Cut out each word behind need to add '\ 0', the change is, for reference,

 
#include
#include
# define the maximum length of each word LEN 20//
# define MAX 50//the maximum number of words in a sentence

//returns the number of words
Int get string (char STR [] [LEN]) {
Int I=0, j=0;
char c;
While ((c=getchar ())!='\ n' & amp; & iIf (c==' ') {
STR [I] [j]='\ 0';//put a '\ 0' at the end of each word
i++;
J=0;
} the else
STR [I] [j++]=c;
}
Return + + I;
}
//bubble sort method
Int sort (char STR [] [LEN], int LEN) {
Char TMP (LEN);
int i,j;
for (i=0; iFor (j=0; JIf (STRCMP (STR [j], STR [j + 1]) & gt; 0 {
Strcpy (TMP, STR [j]);
Strcpy (STR [j], STR [j + 1));
Strcpy (STR [j + 1), TMP);
}
}
}
return 0;
}

Void the read () {
The FILE * fp.
Char STR [LEN]={} '\ 0';
Fp=fopen (" data. TXT ", "r");
While (fscanf (fp, "% s", STR)!=(EOF)
Printf (" % s \ n ", STR);
fclose(fp);
}

Int openw (char STR [] [LEN], int LEN) {
The FILE * fp.
char ch;
//test file exists
Fp=fopen (" data. TXT ", "r");
If (fp) {
Printf (" file already exists, overwrite? (Y/N) : ");
The scanf (" % c ", & amp; Ch);
If (ch! ) {
='Y'Printf (" not override file, \ n ");
return 1;
}
}
fclose(fp);
//output
Fp=fopen (" data. TXT ", "w");
int i=0;
While (iFprintf (fp, "% s", STR [I]);
i++;
}
fclose(fp);
return 0;
}

Int main () {
Char STR [MAX] [LEN];
int i;
Printf (" please enter the word (English) : \ n ");
I=get string (STR);
Sort (STR, I);
Openw (STR, I);
Read ();
}

CodePudding user response:

 char STR [MAX] [LEN]; 

Instead of
 char STR [MAX] [LEN]={0}; 

VS2015 c + + environment debugging the following

CodePudding user response:

How to create a user database operations
  • Related