2

I created a program that asks the user to input their name, and then manipulates it in multiple ways. The final way that it manipulates it is by printing the users name backwards. For instance if the user entered John Doe, the program would print Doe John. The only problem I'm having at this point is stopping my program from putting an unnecessary new line between the last and first name.

Example: I want Doe John on one line but I get

Doe
John

For my assignment I need to get rid of this extra line. How do I do this?

#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

最好的方法是将 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

喜欢提供“接受后”的解决方案。

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 在fgets(name,(sizeof(name)-1),stdin).

于 2013-10-04T17:06:15.233 回答
0

做很少的修改和交换这些下面的两行

    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 回答