私のクラスには、ある種の論理的な割り当てがあります。したがって、私の質問は、文字列を別の文字列にstrcpy()しようとすると、新しい文字列に(スペースのように)あります。私はそれを削除する方法がわかりません、おそらく私の間違いです。助けてください、ありがとう。
このプログラムでは、キーボードで文字や記号を入力し、それをキャプチャして記号を数えることができます。その後、返却してください。
これがCの私のコードです
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
#define N 25
typedef char string[N];
int main(int argc, char *argv[])
{
int i,j;
int jumlah[10];
string inputan;
string temp;
int counter;
//Init
for(i=0;i<10;i++) {
jumlah[i]=0;
}
for(i=0;i<10;i++) {
temp[i]='-';
}
for(i=0;i<10;i++) {
inputan[i]='-';
}
do {
system("cls");
printf("\nMasukan kalimat: ");fflush(stdin);gets(inputan);
if(strcmpi(inputan,"0")!=0) {
strcpy(temp,inputan);
}
getch();
}while(strcmpi(inputan,"0")!=0);
printf("Hasil Analisa:\n\n");
for(i=0;i<10;i++) {
if(temp[i]!='-') {
char c = temp[i];
for(j=0;j<10;j++) {
if(temp[j]!='-') {
if(c == temp[j])
counter+=1;
}
}
jumlah[i] = counter;
counter = 0;
}
}
for(i=0;i<10;i++) {
if(temp[i]!=' ' && temp[i]!='-' && temp) {
printf("\t%c terdapat %d\n",temp[i],jumlah[i]);
}
}
getch();
}
そして、これが私のコンソールの結果です:
これで、プログラムはスペース記号を表示してカウントします。
また、もう一度質問できる場合は、同じ記号を持つ別のインデックスに記号が再びある場合に、1つの文字だけを表示する方法を説明します。Thx、私の英語が流暢でないなら許してください。
印刷物の最後に表示されるスペースは、含めるテスト条件のリストが原因です。
if(temp[i]!=' ' && temp[i]!='-' && temp)
除外する必要のあるいくつかの追加条件が欠落している可能性があります:
1)追加のテストを追加:test[i] != 0
2)に変更さtemp[i] != ' '
れ!isspace(temp[i])
、すべての空白に対してテストされます。
これらが追加されたら:
if(!isspace(temp[i]) && temp[i]!='-' && temp && (temp[i] != 0))
入力されたテキストは、最後の空白以外の文字までしか印刷されません。
コードの変更:
次のコードに他のマイナーな変更をいくつか追加して、コードを自分の環境でコンパイルできるようにしました。私の変更はC標準ライブラリの一部である関数を使用しているので、これもコンパイルする必要があります。変更には、for(...)
作成した配列サイズに対応するためのループの拡張も含まれ、N-1
10文字だけではなく最大文字までの入力が可能になります。私が行ったことのほとんどには、コメント付きの説明が含まれています。
int main(int argc, char *argv[])
{
int i,j;
//int jumlah[10];
int jumlah[N]; // probably meant to use N here?
string inputan = {0};
string temp = {0};
int counter = 0;// initialize
for(i=0;i<N;i++) {
jumlah[i]=0;
}
for(i=0;i<N-1;i++) {
temp[i]='-';
}
for(i=0;i<N-1;i++) {
inputan[i]='-';
}
do {
//system("cls"); This is fine, just does not work in my environment, so commented.
//printf("\nMasukan kalimat: ");fflush(stdin);gets(inputan);
printf("\nPut Sentence (or \"0\" to process): ");fflush(stdin);gets(inputan);// clarified instructions.
if(stricmp(inputan,"0")!=0) { //strcmpi
strcpy(temp,inputan);
}
//getch(); this (or getchar()) is really not necessary here to support
// the flow of your application.
}while(stricmp(inputan,"0")!=0);
printf("Hasil Analisa:\n\n");
for(i=0;i<N;i++) { //replace 10 with N
if(temp[i]!='-') {
char c = temp[i];
for(j=0;j<N;j++) { //replace 10 with N
if(temp[j]!='-') {
if(c == temp[j])
//counter+=1;
counter++; // var++ equivalent var += 1
}
}
jumlah[i] = counter;
counter = 0;
}
}
for(i=0;i<N;i++) {
//if(temp[i]!=' ' && temp[i]!='-' && temp) { // only spaces ?
if(!isspace(temp[i]) && temp[i]!='-' && temp && (temp[i] != 0)) { // temp[i] != 0, and exclude all white space
printf("\t%c terdapat %d\n",temp[i],jumlah[i]);
}
}
getchar(); //orig getch() not standard
}
質問への対処:同じ記号を持つ別のインデックスに記号が再びある場合に、1つの文字のみを表示する方法。
使用された文字のリストと使用された回数を表示することは、別の関数でより適切に処理される可能性があります。以下の行を挿入することにより、元のメイン関数で呼び出すように適合させることができます。
char *res = letterCounter("this is the string");
printf(res);
free(res);
既存の行のすぐ下:(printf("Hasil Analisa:\n\n");
つまり、その行の下のすべてのコードをgetch();
関数まで置き換えます。
char * letterCounter(const char *string)
{
int i, j;
int len = strlen(string);
char *dup = StrDup(string);
if(!dup) return NULL;
int viewableAscii = '~' - '!'; /// range of ASCII from ! to ~ (33 - 126)
char buf[20];
char * results = calloc(100*strlen(string), 1);//ensure enough room
if(!results) return NULL;
/// caps 'A' == 65, 'Z' == 90
/// lowr 'a' == 97, 'z' == 122
/// all visable printables: 33 - 126
unsigned char characterUsageCounter[viewableAscii];
memset(characterUsageCounter, 0,viewableAscii);
for(i=0;i<len;i++)
{
for(j=0;j<viewableAscii;j++)
{
if(dup[i] == 33 + j)
{
characterUsageCounter[j]++;
}
}
}
for(i=0;i<viewableAscii;i++)
{
if(characterUsageCounter[i] > 0)
{
if(characterUsageCounter[i] == 1) sprintf(buf, "%c occurs %d time\n", i+33, characterUsageCounter[i]);
else sprintf(buf, "%c occurs %d times\n", i+33, characterUsageCounter[i]);
strcat(results, buf);
}
}
return results;
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加