Home > Back-end >  C series, calculate the occurrences of the characters
C series, calculate the occurrences of the characters

Time:09-25

Bosses, help me look at my code, different results on different compilers, my editor is vc + +, the code runs successfully into other compilers will error

 
#include
#include
#include
# define MaxSize 11
//define string
Typedef struct
{
Char data [MaxSize];
int length;//length of string
} SqString;

//store the output
Typedef struct
{
Char ch;//single letter
int count;//counter
} the Cch.

SqString s;
Char CSTR [MaxSize];
//create a string of
Void CreateStr (SqString & amp; STR, char CSTR [])
{
int i;
for(i=0; CSTR [I]!='\ 0'; I++)
{
STR. Data [I]=CSTR [I];
}
STR. Length=I;
//printf (" % d \ n ", I);
}
/*
//output string
Void OutputStr SqString (s)
{
int i;
If (s.l ength> 0)
{
for (i=0; i{
Printf (" % c ", s.d. ata [I]);
}
printf("\n");
}
}
*/

//stored in the Cch
Void SaveCch ()
{
The Cch c [MaxSize];
//OutputStr (s);
CreateStr (s, CSTR);
If (s.d. ata> 0)
{
for(int i=0; i{
C [I]. Ch=s.d. ata [I];
C [I]. Count=0;
for(int j=0; j{
If (c [I]. Ch==s.d. ata [j])
{
C [I] count++;
}
}
Printf (" % c: % d \ n ", c [I] ch, c [I]. Count);
}

}
}

Int main ()
{

Printf (" build string s: ");
Gets (CSTR);
CreateStr (s, CSTR);
//OutputStr (s);
SaveCch ();
return 0;
}

CodePudding user response:

Although why this is not very good, but it seems do not have what problem, in addition to void CreateStr (SqString & amp; STR, char CSTR []) with your participation

CodePudding user response:

 # include & lt; String. H> 
#include
#include
#include

# define MaxSize 32
//define string
Typedef struct
{
Char data [MaxSize];
int length;//length of string
} SqString;

//store the output
Typedef struct
{
Char ch;//single letter
int count;//counter
} the Cch.

SqString s;
Char CSTR [MaxSize];

//create a string of
Void CreateStr (SqString & amp; STR, char CSTR [])
{
int i;
for(i=0; CSTR [I]!='\ 0'; I++)
{
STR. Data [I]=CSTR [I];
}
STR. Length=I;
//printf (" % d \ n ", I);
}
/*
//output string
Void OutputStr SqString (s)
{
int i;
If (s.l ength> 0)
{
for (i=0; i{
Printf (" % c ", s.d. ata [I]);
}
printf("\n");
}
}
*/

//stored in the Cch
Void SaveCch ()
{
//the Cch c [MaxSize];
The Cch c [52];
Int independence idx;
Memset (c, 0, sizeof (c));
//OutputStr (s);
CreateStr (s, CSTR);
//if (s.d. ata> 0)
If (s.l ength & gt; 0)//should use length
{
for(int i=0; i{
If (isupper (s.d. ata [I]) {
Independence idx=s.d. ata [I] - 'A';
C) [r]. Independence idx count++;
}
If (islower (s.d. ata [I]) {
Independence idx=s.d. ata [I] - 'a';
C [26] independence idx + count++;
}
# if 0
C [I]. Ch=s.d. ata [I];
C [I]. Count=0;
for(int j=0; j{
If (c [I]. Ch==s.d. ata [j])
{
C [I] count++;
}
}
Printf (" % c: % d \ n ", c [I] ch, c [I]. Count);
# endif
}
for (int i=0; i <52. I++) {
If (I & lt; 26 & amp; & C [I]. Count & gt; 0)
Printf (" % c: % d \ n ", 'A' + I, c [I]. Count);
If (I & gt;=26 & amp; & C [I]. Count & gt; 0)
Printf (" % c: % d \ n ", 'a' + I - 26, c [I]. Count);

}

}
}

Int main ()
{

Printf (" build string s: ");
The fgets (CSTR, MaxSize stdin);
//CreateStr (s, CSTR);
//OutputStr (s);
SaveCch ();
return 0;
}

For your reference ~
C + + support parameters of the belt is the main problem of the building Lord & amp; , and C does not support parameters & amp;

CodePudding user response:

reference 2 building self-confidence boy reply:
 # include & lt; String. H> 
#include
#include
#include

# define MaxSize 32
//define string
Typedef struct
{
Char data [MaxSize];
int length;//length of string
} SqString;

//store the output
Typedef struct
{
Char ch;//single letter
int count;//counter
} the Cch.

SqString s;
Char CSTR [MaxSize];

//create a string of
Void CreateStr (SqString & amp; STR, char CSTR [])
{
int i;
for(i=0; CSTR [I]!='\ 0'; I++)
{
STR. Data [I]=CSTR [I];
}
STR. Length=I;
//printf (" % d \ n ", I);
}
/*
//output string
Void OutputStr SqString (s)
{
int i;
If (s.l ength> 0)
{
for (i=0; i{
Printf (" % c ", s.d. ata [I]);
}
printf("\n");
}
}
*/

//stored in the Cch
Void SaveCch ()
{
//the Cch c [MaxSize];
The Cch c [52];
Int independence idx;
Memset (c, 0, sizeof (c));
//OutputStr (s);
CreateStr (s, CSTR);
//if (s.d. ata> 0)
If (s.l ength & gt; 0)//should use length
{
for(int i=0; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related