当我将一个字符串 strcpy 到另一个字符串时,有一个 space
There's a space when I strcpy a string into another string
我的 class 中有一种逻辑分配。所以我的问题是,当我尝试将一个字符串 strcpy() 转换为另一个字符串时,我的新字符串中有一个(如 space)。我不知道如何删除它,也许是我的错误。请帮帮我,谢谢。
这个程序让你在键盘上输入任何字母或符号,然后尝试捕捉它并计算符号的数量。然后,return它。
这是我的 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();
}
这是我的控制台结果:
所以程序将显示 space 符号并计算它。
如果我可以再问一次,如果在另一个具有相同符号的索引中再次出现一个符号,如何只显示一个字符。谢谢,如果我的英语不流利,请原谅我。
出现在打印输出末尾的 space(s) 是因为您包含的测试条件列表:
if(temp[i]!=' ' && temp[i]!='-' && temp)
可能缺少一些需要排除的附加条件:
1) 添加附加测试:test[i] != 0
2) 将 temp[i] != ' '
更改为 !isspace(temp[i])
,这将针对全白 space 进行测试。
添加这些后:
if(!isspace(temp[i]) && temp[i]!='-' && temp && (temp[i] != 0))
输入的文本只打印到最后一个非白色space字符。
代码修改:
我对以下代码添加了一些其他小修改,以允许在我的环境中编译代码。因为我的修改使用了作为 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
}
解决您的问题:如果在另一个具有相同符号的索引中再次出现符号,如何只显示一个字符。
显示使用过的字符列表,以及使用过的次数最好在单独的函数中处理。通过插入以下行,可以调整下面的函数以在您的原始 main 函数中调用:
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;
}
例如,如果字符串 "this is the string" 作为参数传递给该函数,将输出以下内容:
我的 class 中有一种逻辑分配。所以我的问题是,当我尝试将一个字符串 strcpy() 转换为另一个字符串时,我的新字符串中有一个(如 space)。我不知道如何删除它,也许是我的错误。请帮帮我,谢谢。
这个程序让你在键盘上输入任何字母或符号,然后尝试捕捉它并计算符号的数量。然后,return它。
这是我的 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();
}
这是我的控制台结果:
所以程序将显示 space 符号并计算它。
如果我可以再问一次,如果在另一个具有相同符号的索引中再次出现一个符号,如何只显示一个字符。谢谢,如果我的英语不流利,请原谅我。
出现在打印输出末尾的 space(s) 是因为您包含的测试条件列表:
if(temp[i]!=' ' && temp[i]!='-' && temp)
可能缺少一些需要排除的附加条件:
1) 添加附加测试:test[i] != 0
2) 将 temp[i] != ' '
更改为 !isspace(temp[i])
,这将针对全白 space 进行测试。
添加这些后:
if(!isspace(temp[i]) && temp[i]!='-' && temp && (temp[i] != 0))
输入的文本只打印到最后一个非白色space字符。
代码修改:
我对以下代码添加了一些其他小修改,以允许在我的环境中编译代码。因为我的修改使用了作为 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
}
解决您的问题:如果在另一个具有相同符号的索引中再次出现符号,如何只显示一个字符。
显示使用过的字符列表,以及使用过的次数最好在单独的函数中处理。通过插入以下行,可以调整下面的函数以在您的原始 main 函数中调用:
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;
}
例如,如果字符串 "this is the string" 作为参数传递给该函数,将输出以下内容: