I would like to read a newline and store it in a variable.
I tried reading a \n from a standard input. but instead of reading as a newline it read as "\" and also "n". How can i combine the two things into one character ??
I would like to read a newline and store it in a variable.
I tried reading a \n from a standard input. but instead of reading as a newline it read as "\" and also "n". How can i combine the two things into one character ??
read a newline... Use fgets, that picks up the \n.
Help populate a c/c++ help irc channel
server: irc://irc.efnet.net
channel: #c
Use fgets()
This is what I typed
After a call to fgets() with a suitable buffer, it will look like this
char buff[] = "This is what I typed\n";
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I would like to replace all the a's in the text file with \n and all the b's in the text file with \t. How can i archieve this ??
coz my current code it only replace all the a's with ' and all the b's with \. every each of ' , \ , n , \ , t and ' are being read in as character instead of just \n and also \t. I will appreciate if some one is able to modify the code so that it is able to read in \n as a character and \t as a character so that both the character are able to replace the a's and b's in the text file.
testing '\n\t' < textfile.TXT
File name: testing.c
Code:main(int argc, char *argv[]) { char sentence[330]; char c; int i=0; while(( c = getchar()) != EOF){ sentence[i++] = c; } sentence[i] = '\0'; printf("%s",replaceChar(sentence, argv[1])); } char *replaceChar(char *sN, char *rC) { int new_len,i; new_len = strlen(sN); for(i=0; i< new_len; i++) { if(*sN == 'a') *sN = *rC; else if(*sN == 'b') { rC++; *sN = *rC; rC--; } sN++; } for(i=0; i< new_len; i++) { sN--; } return sN; }
Last edited by winsonlee; 03-19-2004 at 09:07 AM.
Format your code properly. Don't use TABs:Originally posted by winsonlee
I would like to replace all the a's in the text file with \n and all the b's in the text file with \t. How can i archieve this ??
coz my current code it only replace all the a's with ' and all the b's with \. every each of ' , \ , n , \ , t and ' are being read in as character instead of just \n and also \t. I will appreciate if some one is able to modify the code so that it is able to read in \n as a character and \t as a character so that both the character are able to replace the a's and b's in the text file.
testing '\n\t' < textfile.TXT
The problem is from the commande line the values inCode:int main(int argc, char *argv[]) { char sentence[330]; char c; int i=0; while(( c = getchar()) != EOF) { sentence[i++] = c; } sentence[i] = '\0'; printf("%s",replaceChar(sentence, argv[1])); return 0; } char *replaceChar(char *sN, char *rC) { int new_len,i; new_len = strlen(sN); for(i=0; i< new_len; i++) { if(*sN == 'a') *sN = *rC; else if(*sN == 'b') { rC++; *sN = *rC; rC--; } sN++; } for(i=0; i< new_len; i++) { sN--; } return sN; }
testing '\n\t' < textfile.TXT
are characters ', \, n, and t. From the command line you cannot pass in a newline and tab. In your main() you'd have to parse your argv[1] and replace the characters \ & n with the single character '\n'
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers