Certainly. That's what sequence %s is good for.
#include,stdio.h>main(){char string1[20],string2[20]printf("enter the first string");scanf("%s",string1);// reading the string1printf("enter the second string");scanf("%s", string2);// reading the the string2printf( "the first string is %s",string1);// printing the string1printf("the second string is %s",string2);// printing the string2}the problem of using scanf is that it does not take space. so we can use gets for it.ie instead of scanf("%s",string1); we can use gets(string1); .
The use of the reverse string in C program is used to reverse the letters in the string. An example would be reverse me would be reversed to em esrever.
scanf ("%[^\n]s", sentence);
They do different things, read the manual/help for details.
int main (void) { char buf[1024]; scanf ("Enter a string: %s", buf); printf ("The length of the string is %d chars long.\n", strlen (buf)); return 0; }
scanf
#include<stdio.h> #include<conio.h> void main() { char string[20]; int i=0; printf("Enter the string.\n"); while(i<=19) { scanf("%c",&string[i]); i++; } while(i>=0) { printf("%c",string[i]); i--; } getch(); } In this program it is compulsory to enter 20 characters. Now we can write a program in which it is not necessary. #include<stdio.h> #include<conio.h> #include<string.h> void main() { char string[20]; int length; printf("enter the string.\n"); gets(string); length=strlen(string); length--; while(length>0) { printf("%c",string[length]); length--; } getch(); }
#include<stdio.h> void main() { int cnt=0,i; char str[100]; printf("Enter the string "); scanf("%s",str); for(i=0;i<strlen(str)-1;i++) { if(str[i]==' ') cnt++; } printf("\nTotal no. of word in string = %d",cnt); }
scanf is a function (available in C and C++)cin is an istream object (C++ only)Advice: when in doubt, use fgets+sscanf
in c simply add three lines in the begining of your program: int x; printf("enter the size of the array to be entered :"); scanf("%d",&x); after that use x as your maximum limit of array in your program. in c++ just replace above printf & scanf statements by cout<<"enter the size of the array to be entered :"; & cin>>x; respectively and do not use brackets.
Use '%s', eg: char name[64]; scanf ("%s", name);
To read input from standard input.