2

ユーザーに名前を入力するように要求し、それを複数の方法で操作するプログラムを作成しました。それを操作する最後の方法は、ユーザー名を逆に出力することです。たとえば、ユーザーが John Doe と入力した場合、プログラムは Doe John を出力します。この時点で私が抱えている唯一の問題は、私のプログラムが姓と名の間に不要な改行を入れないようにすることです。

例: Doe John を 1 行にしたいのですが、

Doe
John

私の課題では、この余分な行を取り除く必要があります。どうすればいいですか?

#include <stdio.h>
#include <string.h>

void removeNewLine (char * userName, int charLenght)
{
    int i=0;

    do {
        if (userName [i]=='\n')
        {
            userName [i]='\0';
        }
    i++;
    } while (i<charLenght);

}

// This is going to tell me exactly how many real character are in my array
int myCounter (char * userName, int size)
{
    int counter=0;
        do
        {
            if(userName [counter]=='\0')
            {
                return counter; //I always thought that you needed to put your return at the end of the function, this is good to know that you don't need too
            }
            counter++;
        }while (counter<size);
    return -1;
}


int main ()
{
printf("Enter your first and last name\n");

char name [250]={'\0'};
char * space;
char *first=NULL, *last = NULL, *firstspace;
char *userName;
int numOfChars=0;
//Prevents the potential problem of an overflow = (sizeof(name)-1)
fgets(name,(sizeof(name)-1),stdin); 

//This is what is actually doing the dirty work of removing the extra chars

removeNewLine(userName, numOfChars);

//This is going to count the number of characters that were input by the user

numOfChars = strlen(name)-1;

printf("You Entered: %s     \n", name);
printf("There are %zu characters in your name including the space. \n", strlen(name));

char end;
int i;
end = strlen(name) -1;
printf("Your name backwards is");
for (i = end; i >= 0; --i)
{
printf("%c", name [i]);
}

printf("\nLooking for the space in your name \n", name);
firstspace=space=strchr(name, ' ');
*firstspace='\0';
while (space!=NULL)
{
    printf("The space was found at character %d\n", space-name+1);
    last = space+1;
    space=strchr(space+1, ' ');
}

printf("%s%s", last, name);
*firstspace=' ';

//This is just to tell the user how many "real" characters were in there name
printf("\n There are %d actual characters in your name including the space", numOfChars);


 }
4

3 に答える 3

0

「承認後」のソリューションを提供したい。

void *removeNewLineAfter_fgets(char *s) {
  if (s) {
    size_t l = strlen(s);
    if ((l > 0) && (s[l-1] == '\n')) {
      s[l-1] = '\0';
    }
  }
  return s;
}

// Usage:
if (removeNewLineAfter_fgets(fgets(name,sizeof(name),stdin)) == NULL) { handle EOF }

ところで: OP は -1 in を必要としませんfgets(name,(sizeof(name)-1),stdin)

于 2013-10-04T17:06:15.233 に答える
0

最良の方法は、いくつかのヘルパー関数で fgets() を使用することです。

/*Removes remaining characters from keyboard input buffer until next newline*/

/*Returns 0 if OK, a negative value if EOF.*/
int fpurge(FILE *f)
{
    int c;
    while((c=fgetc(f))!=EOF && c!='\n')
    { }
    return (c==EOF ? -1 : 0);
}

/*Find and remove newline from string*/

/* Returns a nonzero value if found, zero if not. */
int truncate_newline(char *str)
{
    int bRet=0;
    if(str!=NULL)
    {
        char *pNewline = strchr(str, '\n');
        if(pNewLine!=NULL)
        {
            bRet = 1;
            *pNewLine = '\0';
        }
    }
    return bRet;
}

/*Remove newline from string or excess characters from input buffer,
where appropriate.*/

/* Returns 0 if buffer is full, a positive value if line is complete,
   a negative value if EOF (implies buffer full). */
int fclean(char *str, FILE *f)
{
    int ret = 1;
    if(!truncate_newline(str))
        ret = fpurge(f);
    return ret;
}

次のように使用されます。

char buf[42];
fgets(buf, sizeof buf, stdin);
fclean(buf);

これで、NULL で終了し、newlinelessがあり、次の呼び出しbufを破損する入力バッファーには何もありません。fgets

于 2013-10-04T15:24:57.957 に答える
0

少し変更を加えて、これらの2行の下を交換してください

    removeNewLine(userName, numOfChars);

    //This is going to count the number of characters that were input by the user

    numOfChars = strlen(name)-1;

このような

numOfChars = strlen(name); // first find the length of input.
removeNewLine(name, numOfChars); // And now remove newline at the end of input  

編集

あなたのコード

#include <stdio.h>
#include <string.h>

void removeNewLine (char * userName, int charLenght)
{
    int i=0;

    do {
        if (userName [i]=='\n')
        {
            userName [i]='\0';
        }
    i++;
    } while (i<charLenght);

}


int main ()
{
printf("Enter your first and last name\n");

char name [250]={'\0'};
char * space;
char *first=NULL, *last = NULL, *firstspace;
int numOfChars=0;
//Prevents the potential problem of an overflow = (sizeof(name)-1)
fgets(name,(sizeof(name)-1),stdin);

//This is what is actually doing the dirty work of removing the extra chars

numOfChars = strlen(name); // first find the length of input.
removeNewLine(name, numOfChars); // And now remove newline at the end of input
printf("You Entered: %s     \n", name);
printf("There are %zu characters in your name including the space. \n", strlen(name));

char end;
int i;
end = strlen(name) -1;
printf("Your name backwards is");
for (i = end; i >= 0; --i)
{
printf("%c", name [i]);
}

printf("\nLooking for the space in your name \n", name);
firstspace=space=strchr(name, ' ');
*firstspace='\0';
while (space!=NULL)
{
    printf("The space was found at character %ld\n", space-name+1);
    last = space+1;
    space=strchr(space+1, ' ');
}

printf("%s %s", last, name);
*firstspace=' ';

//This is just to tell the user how many "real" characters were in there name
printf("\n There are %d actual characters in your name including the space", numOfChars);


 }

出力

Enter your first and last name
John Doe
You Entered: John Doe
There are 8 characters in your name including the space.
Your name backwards iseoD nhoJ
Looking for the space in your name
The space was found at character 5
Doe John
 There are 9 actual characters in your name including the space
于 2013-10-04T15:26:46.627 に答える