Search the Site:

How to Read a String in C

I wish to know how can I read a string in c.

Thanks in advance.

Re: How to Read a String in C

Use fgets to read string from standard input  #include <stdio.h>#include <string.h> int main(void){  char str[80];  int i;   printf("Enter a string: ");  fgets(str, 10, stdin);   /* remove newline, if present */  i = strlen(str)-1;  if( str[ i ] == '\n')       str[i] = '\0';   printf("This is your string: %s", str);   return 0;}

Re: How to Read a String in C

Get a string from stdin: how to use gets  #include <stdio.h> int main(){  char string [256];    printf ("Enter your address: ");    gets (string);    printf ("Your address is: %s\n",string);  return 0;} 

Re: How to Read a String in C

Read formatted data from string: how to use sscanf  #include <stdio.h> int main (){  char sentence[] = "This is a line.";  char str [20];  int i;   sscanf (sentence,"%s %*s %d",str,&i);    printf ("%s -> %d\n",str,i);    return 0;} 

Re: How to Read a String in C

Read string: A simple scanf and printf pair #include <stdio.h> int main(void){  char str[80];   printf("Enter a string: ");  scanf("%s", str);  printf(str);   return 0;} 

Re: How to Read a String in C

Get string from console and assign it to a char pointer #include <stdio.h> int main(void){  char *p, str[80];   printf("Enter a string: ");   p = gets(str);   if(p) /* if not null */     printf("%s %s", p, str);   return 0;} 

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <i> <object> <param> <embed> <blockquote>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

2 Steps to Post a New Problem

2 Steps to Post a New Problem

Register
Login
Post New Problem

DISCLAIMER: The content on the site has been submitted by the users. NibbleGuru.com has no responsibility other than in removing material from the website when we have been notified of a copyright violation. Notify here.