%s is a format specifier for accepting the string in c language by scanf. But the problem of using this is that it will terminate when we enter a space character as input.So whenever we need to take input string with space character we can not just use %s.To resolve this problem we can use gets() and we can also use %[^\n]s. The expression scanf("%[^\n]s", string) will take input from the keyboard until and unless we will not press Enter button because as we can see that we are using \n in the above scanf() which is used for new line. if you want that your scanf terminates on entering any character or integer then use that at the place of \n. Example- scanf("%[^&]s", string) -using this will terminates the scanf on entering & character. scanf("%[^#]s", string)-this will stop taking input when we press # character. and so on...... In this way you can use this in your own way.
Posts
This is a program to count the number of words in a string entered by the user.I am performing this task by using ASCII value of lower case letters (as the string , user will enter must be in lower case only) . T o count the number of words we are not considering any other character oth er then lowercase letters. To enter the text we are using %[^\n]s as %s terminates on entering space (' ') to resolve this problem we can also use gets but i am using %[^\n]s her e. #include<stdio.h> #include<string.h> int main() { char str[100],c=0,i=0; scanf("%[^\n]s",str); while(str[i++]!='\0') { if(str[i]<97 || str[i]>123) { if(str[i+1]>=97 && str[i+1]<=123) c++; } } printf("no of worlds=%d",c); return 0; }
We know that factorial of number greater than 50 is too large that we can not store it's value in any standard data type available.So, if we need to find the factorial of large numbers we will use an array to store the factorial value. The concept is based on that we store single digit of factorial in reverse order to the array and multiply these numbers with the next number of the factorial sequence and each time when we have a carry on multiplying the last digit of array we will increase the size of array and store that carry value there.At last we will print the array in reverse order. Example- We are storing 1 at a[0] and then multiply it with 2 and then 3 and so on..... till that number whose factorial we need.if in any case there is a carry the increase the size of array by 1. Note that we are storing the result in reverse order like for the factorial of 5 and 6 result will be stored like 5!= 0 2 1 6!= 0 ...