-
String trouble
I'm new to C programming, but not at all new to programming in general. My professor wants me to use scanf() function to read a string of characters into a variable. I missed a day of class and did not learn how to actually go about this. From what I could get from his lecture and the students around me, I've come up with the following:
Code:
char *name;
char *bufp;
char buf[BUFL];
printf("Please enter your name: ");
name = fgets(buf, BUFL, stdin);
buflen = strlen(name);
buf[buflen-1] = "\0";
What I am trying to do is use scanf to put the input into *name. I can do that fine, but it also reads the \n and places it into *name. That is where I know I am going wrong. What I have above is one of many things I tried and failed at.
How can I read a string using scanf() into a variable, and get rid of the \n (replace it with a NUL byte).
Thanks.
-
Well, if you want to use scanf(), perhaps you should actually use scanf()...
Now these two lines:
Code:
char *name;
char *bufp;
I hope you have pointed these to some real space that you own, and not just try to assign some value to it.
There are many ways to get rid of the newline...
-
more over it is not a good idea of using a scanf to read a string. is dosn't read after the white space. u will be more clear with the following programe
Code:
#include<stdio.h>
#include<stdlib.h>
void clear_buffer(void);
int main()
{
char *str1;
char *str2;
str1=malloc(25);
str2=malloc(25);
printf("Enter a string 1\n?");
scanf("%s",str1);
clear_buffer();
printf("Enter a string 2\n?");
scanf("%s",str2);
printf("\nAfter entering the string\n");
printf("---------------------------\n");
printf("string 1 -> %s\n",str1);
printf("string 2 -> %s\n",str2);
clear_buffer();
getchar();
}
void clear_buffer(void)
{
int ch;
while((ch=getchar())!='\n' && ch!=EOF);
}
/*my output
Enter a string 1
?hello there
Enter a string 2
?hellothere
After entering the string
---------------------------
string 1 -> hello
string 2 -> hellothere
*/
ssharish2005
-
Code:
#include<stdio.h>
#include<conio.h>
#include <string.h>
int main() {
char string[10];
char name[10];
clrscr();
printf("Enter String] ");
scanf("%s", &string);
strcpy(name,string);
printf("Your String is %s",name);
getch();
return 0;
}
-
A classic example of what not to do:
Code:
scanf("%s", &string);
Certainly doing a board search out to turn up several reasons why. Maybe even visit the FAQ?
-
or you could even use a function to fix gets :) similar to the one my C programming instructor wrote...
Code:
/* This routine fixes fgets incorporation of line feeds and other
* ASCII values less than 32 into an input string. Written by H. Brown.
*/
int fixgets(char input_array[], int length, FILE *file_name)
{
register int position=0;
if (fgets(input_array,length,file_name) != NULL)
{
while(input_array[position] != 0)
{ if (input_array[position] < 32) input_array[position]=0;
else ++position;
}
}
/* The length of the string is returned. */
fflush(stdin);
return position;
}
then you'd call it exaclty like fgets
-