2013-10-04 71 views
2

我創建了一個程序,要求用戶輸入他們的名字,然後以多種方式操縱它。它操縱它的最後一種方法是向後打印用戶名。例如,如果用戶輸入了John Doe,該程序將打印Doe John。我現在遇到的唯一問題是阻止我的程序在最後名字和名字之間插入不必要的新行。我該如何擺脫這條新線?

例子: 我想在一行Doe的約翰,但我得到

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); 


} 
+0

注意:fgets()應該接受'sizeof(name)',不帶'-1'。 – Medinoc

回答

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 
+0

我仍然得到相同的新行 – user2172993

+0

添加了修改後的代碼和輸出。看到是你想要的嗎? – Gangadhar

+1

沒關係,它的工作我沒有刪除-1。謝謝一個男人。 – user2172993

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結尾,無換行buf,並且輸入緩衝區中沒有任何內容破壞您的下一個fgets電話。

+0

即使將其複製到我的程序後,我仍然遇到同樣的問題。有什麼我需要先刪除? – user2172993

+0

正常情況下,您只需用'fclean'調用替換'removeNewLine'調用... – Medinoc

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 } 

BTW:OP不需要-1 fgets(name,(sizeof(name)-1),stdin)