Create function script(spaces preceeding, character selected, and times printed) - c

I have an attempt, but this site will not let me indent it properly and I keep getting errors
Write the prototype for a function called script that has three input parameters.
The first parameter will be the number of spaces to display at the beginning of a
line. The second parameter will be the character to display after the spaces, and
the third parameter will be the number of times to display the second parameter
on the same line.
script();
int main() {
script();
return 0;
}
double script() {
int spaces, timesCharacter;
char character;
printf("Please enter in order the number of spaces preceeding a character\n");
printf("the specific character to input and \n");
printf("the number of times your want the character to display\n");
printf("in order with a space in between each one: \n");
scanf("%d %c %d", &spaces, &character, &timesCharacter);
printf("%d %c %d", spaces, character, timesCharacter);
}
this line above is the problem line, I cannot figure out how to print spaces preceeding

Write the prototype for a function called script that has three input
parameters.
how to print spaces preceding?
Function with parameter and Prototype, Have look at follow code:
#include <stdio.h>
double script(int spaces,char character, int timesCharacter ); //Function Prototype
int main() {
int spaces, timesCharacter;
char character;
script(spaces, character,timesCharacter); //Function Call
return 0;
}
double script(int spaces,char character, int timesCharacter) { //Function Defination
printf("Please enter in order the number of spaces preceeding a character\n");
printf("the specific character to input and \n");
printf("the number of times your want the character to display\n");
printf("in order with a space in between each one: \n");
scanf("%d %c %d", &spaces, &character, &timesCharacter);
//printf("%d %c %d", spaces, character, timesCharacter);
/*How to add spaces preceding*/
printf("%*c", spaces, character);
//To print the number of times
int i=0;
for(i = 1;i < timesCharacter; i++ ){
printf("%c",character);
}
}
this will not have any error.

Related

Scanning multidimensional string with space in c

I'm trying to make a simple voucher. So, I used a multidimensional string. But facing trouble including space in those strings. Instead, I took words as input. But is there any way to include space? My code is given below-
#include<stdio.h>
#include<string.h>
int main(){
int sum =0, n, i;
puts("Please input how many transactions you want to enlist: ");
scanf("%d", &n);
char list[301][51];
int amount[301];
puts("Please enter the name of your transaction and the amount: (Press space or enter to toggle between name and amount . And avoid using spaces in the name; use underscore instead.)");
for(i=0; i<n; i++){
scanf("%s %d", &list[i], &amount[i]);
sum += amount[i];
}
list[0][n+1] = '\0';
amount[n+1] = '\0';
puts("");
printf("\t\t\t\t Voucher\n\n");
puts(" Ser.|\t Name \t\t\t\t\t\t\t|Amount");
puts("------------------------------------------------------------------------------------------------------------");
for(i=0; i<n; i++ ){
printf(" %03d |\t %-50s\t|%6d\n", i+1, list[i], amount[i]);
}
puts("------------------------------------------------------------------------------------------------------------");
printf(" | Total\t\t\t\t\t\t\t|%6d", sum);
puts("");
return 0;
}
For this you could use the %[ scanf specifier, to read all characters until you hit a digit, and write it into list[i].
This will leave you with a trailing space in list[i], but that can be trimmed if you don't want it.
The scanf call could then look something like
scanf(" %50[^0-9]%d", list[i], &amount[i]);
Note the leading space in the format string, to tell scanf to skip white-space (like the newline from the previous line), and the width-specifier to not read more than can fit in line[i].
Of course, that prevents you from having numbers inside the string you read. To solve this problem you need to go a more complicated route.
For example by reading the whole line into a buffer, and then find the last space in the string. You can then copy the contents before the last space to list[i], and convert the contents after to an int value for amount[i].

2D char array (Storing text with the spacebar in an one char array element?)

Acc. to this post, the most used method to store text in a 2D array is by using %s approach. But, this approach has a downfall, i.e. whenever you press spacebar, the text which is typed after goes into the second element of array. for e.g. you typed
Input:-
1st element of char array = Hi everyone
Output:-
1st element of char array = Hi
2nd element of char array = everyone
Expected output:-
1st element of char array = Hi everyone
So, i want to ask why the below written approach cannot be used to enter text into a 2D array?
#include <stdio.h>
int main()
{
char ch[20];
printf("Enter name:");
scanf("%19[^\n]", ch);
printf("Your name is: %s", ch);
return 0;
}
If the above approach can be used, then please let me know how?
Please do not introduce pointer concepts/code in answer to this post. This is a question to understand why the above written approach fails.
Consider this as the code which fails:-
#include <stdio.h>
int main()
{
char name[4][20];
int i;
printf("Enter names:-\n");
for(i=0; i<4; i++)
{
printf("Enter name %d: ", i);
scanf("%19[^\n]", name[i]);
printf("\n");
}
for(i=0; i<4; i++)
{
printf("Entered name %d: %s", i, name[i]);
printf("\n");
}
return 0;
}
The above program compiles without any error or warning, but fails during runtime.
The problem in the example you provided is that the newline is left in the buffer. You can discard this leading whitespace by changing this:
scanf("%19[^\n]", &name[i]);
To this
scanf(" %19[^\n]", &name[i]);
Note the space before %19. With this, your program prints:
Enter names:-
Enter name 0: foo
Enter name 1: bar
Enter name 2: baz
Enter name 3: qux
Entered name 0: foo
Entered name 1: bar
Entered name 2: baz
Entered name 3: qux
This is because the %[^\n] specifier tells it to take everything but the newline. So the newline will be left in the buffer, and when scanf is called again, it is the first thing in the buffer, so those additional calls can't take any input.
That leading space in the scanf fixes the problem, because it tells scanf to discard any trailing whitespace, which includes that newline left in the buffer.
This is what a reference says about it:
Whitespace character: the function will read and ignore any whitespace characters encountered before the next non-whitespace
character (whitespace characters include spaces, newline and tab
characters -- see isspace). A single whitespace in the format string
validates any quantity of whitespace characters extracted from the
stream (including none).
Figured out another approach to get it done.
#include <stdio.h>
int main()
{
char name[4][20];
int i;
printf("Enter names:-\n");
for(i=0; i<4; i++)
{
printf("Enter name %d: ", i);
scanf("%19[^\n]", name[i]);
fflush(stdin);
printf("\n");
}
for(i=0; i<4; i++)
{
printf("Entered name %d: %s", i, name[i]);
printf("\n");
}
return 0;
}

A user types a word and a number on a single line. Read them into the provided variables. Then print

It takes in a word and a number, I can't seem to understand why the number variable won't receive the input, help please.
#include <stdio.h>
int main(void) {
char userWord[20];
int userNum;
scanf("%s", userWord);
printf("%s_", userWord);
scanf("%s", userNum);
printf("%d\n", userNum);
return 0;
}
Should be:
Input: Stop 7
Output: Stop_7
What I get:
Input: Stop 7
Output: Stop_0
Change
scanf("%s", userNum);
to
scanf("%d", &userNum);
You used format %s for reading in an integral value; it should have been %d.
Once having fixed this (i.e. by writing scanf("%d", &userNum);, note that your code will read in a string and a number even if the string and the number were not in the same line (cf., for example, cppreferene/scanf concerning format %s and treatment of white spaces). Further, you will run into undefined behaviour if a user enters a string with more than 19 characters (without any white space in between), because you then exceed your userWord-array.
To overcome both, you could read in a line with fgets, then use sscanf to parse the line. Note that you can parse the line in one command; the result of scanf is then the number of successfully read items. Further, note the %19s, which limits the input to 19 characters (+ the final string termination character '\0'):
int main() {
char line[100];
if (fgets(line,100,stdin)) {
char userWord[20];
int userNum;
if (sscanf(line, "%19s %d", userWord, &userNum) != 2) {
printf("invalid input.\n");
} else {
printf("word:'%s'; number: %d", userWord, userNum);
}
}
}

how to display entered letter in c?

I am new to c programming. I have created a program for entered letters and finally displayed the entered letters.. but it displayed only final letters always.. please help .. i know its simple question but am beginner so please help guys..
#include<stdio.h>
int main()
{
char z;
int a;
printf("enter the no.");
scanf("%d",&a);
printf("the entered no. is:%d\n",a);
int i;
for(i=0;i<a;i++)
{
printf("enter the letters:");
scanf("%s",&z);
}
printf("the entered letters are:");
for(i=0;i<a;i++)
{
printf("%s\n",&z);
}
return 0;
}
Problems:
You should use %c (for character) instead of %s (for string).
Use a character array for storing multiple characters. Read about arrays here.
Remove & from printf() in the second for loop.
Try this:
int main()
{
char z[10]; //can hold 10 characters like z[0],z[1],z[2],..
int a;
printf("enter the no.");
scanf("%d",&a);
printf("the entered no. is:%d\n",a);
int i;
for(i=0;i<a;i++)
{
printf("enter the letters:");
scanf("%c",&z[i]);
}
printf("the entered letters are:");
for(i=0;i<a;i++)
{
printf("%c\n",z[i]);
}
return 0;
}
Please look into this for more details on scanf. You have given scanf("%s",&z); %s is for reading strings(array of chars except newline char and ended with null char). So if you put this inside loop you wont get desired result. And if you want read only a char at a time use %c here c for Character.
for(i=0;i<a;i++)
{
printf("enter the letters:");
scanf("%c",z+i);
}
char z is a place holder for one character only. And you are over writing what you set z to in the for loop. To take in more characters, use a char array as others have mentioned.
Or print the characters in the same you loop you are scanning them:
#include<stdio.h>
int main()
{
char z;
int a;
printf("enter the no.");
scanf("%d",&a);
printf("the entered no. is:%d\n",a);
int i;
for(i=0;i<a;i++)
{
printf("enter the letters:");
scanf("%s",&z);
printf("letter scanned:%c\n", z);
}
return 0;
}
Letters are scanned using %c. And to scan multiple letters you can use char array: char z[10];
What you are trying to do can be done this way:
char z[10]; // Take some max size array
...
for(i=0;i<a;i++)
{
printf("enter the letters:");
scanf("%c",&z[i]); // Scan the letters on each array position.
}
printf("the entered letters are:");
for(i=0;i<a;i++)
{
printf("%c\n",z[i]); //'printf' doesn't require address of arg as argument hence no `&` required
}
%s argument is used to scan a string of chars.
Note the difference between string of chars and array of chars. The string of chars in C needs to be terminated with ASCII Character 0 represented as \0 in char format, while the array of char is just a collection of letters which need not be terminated with \0.
The difference becomes more important when you try to perform some operation on strings such as printf, strcpy, strlen, etc.. These functions work on null character termination property of string.
For Example: strlen counts the characters in the string till it finds \0, to find out the length of string. Similarly, printf prints the string character by character until it finds the \0 character.
UPDATE:
Forgot to mention that scanf is not a good option to input char format. Use fgetc instead, with stdin as input FILE stream.
#include <stdio.h>
int main()
{
char *z;
int a;
printf("enter the no.");
scanf("%d",&a);
z = (char *) malloc(a);
printf("the entered no. is:%d\n",a);
int i;
for(i=0;i<a;i++)
{
printf("enter the letters:");
scanf("%c",z+i);
}
printf("the entered letters are:");
for(i=0;i<a;i++)
{
printf("%c\n",z);
}
return 0;
}
first error in your code is you have used "%s" instead of "%c".Second is it is impossible to store multiple values in one variable so instead of using variable use arrays.third is that you have told the user to enter the number of character that he/she wants to entered which you don't know.They can enter 1 also and 100000 also so the number of members in array is not defined.Better is to use specific number of characters in array.
finally i got the answer thank you for the help stackoverflow guys simply rocks ...
#include<stdio.h>
#include<malloc.h>
int main()
{
int a;
char *z=(char *)malloc(sizeof(a));
printf("enter the no.");
scanf("%d",&a);
printf("the entered no. is:%d\n",a);
int i;
for(i=0;i<a;i++)
{
printf("enter the letters:");
scanf("%s",&z[i]);
}
printf("the entered letters are:\n");
for(i=0;i<a;i++)
{
printf("%c\n",z[i]);
}
return 0;
}

Inputting array in C

Basically I have a C program where the user inputs a number (eg. 4). What that is defining is the number of integers that will go into an array (maximum of 10). However I want the user to be able to input them as "1 5 2 6" (for example). I.e. as a white space delimited list.
So far:
#include<stdio.h>;
int main()
{
int no, *noArray[10];
printf("Enter no. of variables for array");
scanf("%d", &no);
printf("Enter the %d values of the array", no);
//this is where I want the scanf to be generated automatically. eg:
scanf("%d %d %d %d", noArray[0], noArray[1], noArray[2], noArray[3]);
return 0;
}
Not sure how I might do this?
Thanks
scanf automatically consumes any whitespace that comes before the format specifier/percentage sign (except in the case of %c, which consumes one character at a time, including whitespace). This means that a line like:
scanf("%d", &no);
actually reads and ignores all the whitespace before the integer you want to read. So you can easily read an arbitrary number of integers separated by whitespace using a for loop:
for(int i = 0; i < no; i++) {
scanf("%d", &noArray[i]);
}
Note that noArray should be an array of ints and you need to pass the address of each element to scanf, as mentioned above. Also you shouldn't have a semicolon after your #include statement. The compiler should give you a warning if not an error for that.
#include <stdio.h>
int main(int argc,char *argv[])
{
int no,noArray[10];
int i = 0;
scanf("%d",&no);
while(no > 10)
{
printf("The no must be smaller than 10,please input again\n");
scanf("%d",&no);
}
for(i = 0;i < no;i++)
{
scanf("%d",&noArray[i]);
}
return 0;
}
You can try it like this.

Resources