Here is my code
void display(char ch, int lines, int width);
int main()
{
char c;
int rows, cols;
while (1)
{
scanf("%c %d %d", &c, &rows, &cols);
display(c, rows, cols);
if (c == '\n')
break;
}
}
void display(char ch, int lines, int width)
{
for (int i = 0; i < lines; i++)
{
for (int j = 0; j < width; j++)
printf("%c", ch);
printf('\n');
}
}
I wana like this.
if I input a 2 3
It returns
aaa
aaa
But It didn't work. SO I change like this
void display(char ch, int lines, int width)
{
for (int i = 0; i < lines; i++)
{
for (int j = 0; j < width; j++)
putchar(ch);
putchar('\n');
}
}
It works well. why that code works well??
what is difference between printf and putchar?
printf('\n'); is a mistake, and your compiler should warn you about it. The first argument to printf should be a string, such as "\n", not a character constant, such as '\n'.
The source code "\n" represents a string of two characters, the first being a new-line character and the second being a null character that indicates the end of the string. When used in an expression this way, it is automatically converted to a pointer to its first element, and this pointer is passed to printf.
The source code '\n' represents a character. Its value is the code for that character. When it is passed to printf, that value is passed. That is the wrong thing to pass to printf, which is why your first program did not work.
When you have %c inside the printf, then it will print the character only. But your point is you gave 2 but it prints a, why? see the ASCII table and find the number to char equivalent value from here ASCII Table. on the other hand putchar is just char printer.
Related
[Warning] passing argument 1 of 'strcat' makes pointer from integer without a cast THIS ERROR IS COMING WHEN I AM USING STRCAT WHAT SHOULD I DO , below is my code
#include <stdio.h>
#include <string.h>
void main() {
char a[20], b[256], p = NULL, f;
int i, j, n, k, c[20], t, x, l;
printf("enter the no of possible characters");
scanf("%d", &k);
printf("enter the possible characters in the dictionary");
printf("hi");
for (i = 0; i < k; i++) {
c[i] = (i);
a[i] = getchar();
}
for (i = 0; i < k; i++) {
printf("%d %s\n ", c[i], a[i]);
}
l = k;
printf("enter the string\n");
scanf("%s", &b);
n = strlen(b);
printf("%d", n);
for (i = 0; i < n; i++) {
strcat(p, b[i]);
}
getch();
}
This is because strcat takes null-terminated strings, not individual characters. You can work around this problem by null-terminating b at position n, and calling strcat once:
b[n] = '\0';
strcat(p, b);
This will append the initial n characters of b to p, all in one go. Of course, given that p does not have any characters in it, this is completely pointless: you might as well use strcpy. Of course, you need to declare p to be a character buffer large enough to hold all of b:
char a[20],b[256],p[256]={0},f;
// ^^^^^^^^^
b[n] = '\0';
strcpy(p, b);
Your variable definition is wrong.
char a[20],b[256],p=NULL,f;
does not define p as a pointer. It is a char variable with is not the expected type for the first argument of strcat().
You need to define p as a pointer to a string which has enough memory to hold the concatenated result.
That said,
strcat(p,b[i]);
is completely wrong, because b[i] is not a pointer to a string, either.
So I have written a code that sorts words in the right order. The words are being stored via pointers and I have initialized another char array in the program to store the char* argv.
The last for loop is what prints segment fault and I can't figure out why.
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[]) {
int i, j;
char *key;
char a[argc-1];
for(i=1; i < argc; i++){
a[i-1]= tolower(argv[i]);
}
for (i = 2; i < argc; i++) {
key = argv[i];
j = i-1;
while (j >= 1 && strcmp(argv[j], key) > 0) {
argv[j+1] = argv[j];
j--;
}
argv[j+1] = key;
}
for(i = 1; i < argc; i++){
a[i-1] = *argv[i];
}
for (i = 1; i < argc ; i++){
puts(argv[i]);
}
for(i = 0; i < argc-1; i++){
printf("%s", a[i]);
}
return 0;
}
input
./a.out orange banana apple
output
apple
banana
orange
Segmentation fault
Your compiler should also give you a warnging on:
printf("%s", a[i]);
warning: format specifies type 'char *' but the argument has type
'char' [-Wformat]
If you change that to a %cit works fine.
As the warning says, when you use it %s printf is expecting a string or a char* and will treat it as such. a[i] is an integer type that can reference an invalid location in memory. So the correct way to do it would be to either use %c, and print a character; or use %s and pass a char* as the second argument.
Or perchance you want args in a. Change the delcaration.
char *a[argc-1];
Then change the assignment.
a[i-1] = argv[i];
There are multiple problems in your code
The line a[i - 1] = tolower(argv[i]) is wrong because tolower() takes int as paramater and you are passing char * so it's converting a pointer to int which is legal in c but does not guarantee defined behavior.
You are not setting the '\0' terminator on the a array which is another cause for problems, specifically when you try to use it as a string, a char array is not a string unless, it's a sequence of printable bytes with a terminating '\0' byte.
Allocating argc - 1 is not going to work because
The value of argc is the number of passed parameters to the executable.
If it was the length of the corresponding argv as found by strlen() you would need 1 more byte not less, so it would be argc + 1 in any case.
Why are you using %s
for(i = 0; i < argc-1; i++){
printf("%s", a[i]);
try %c
Expected output of program is to print a border of a specified character around a sentence input by the user.
//program: border.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int argu = argc - 1;
if(argu < 1){
printf("usage: border arguments are the text to border.\n");
}
else{
printf("Enter the character for the border: ");
char in = getchar();//get the character input by user
int size;
for(int i = 1; i <= argu; i++){// gets the count of the characters in the string
size += strlen(argv[i]);
size += 1; //to compensate for spaces
}
printf("%d", size);
printf("\n");
size += 2;
for( int a = 0; a <= size ; a++){
printf("%c", in); // prints the first border line
}
printf("\n");
printf("%c%*c\n", in, size, in);//prints second line of border line.
printf("%c", in);
printf(" ");
for( int i = 1; i <= argu; i++){//prints the sentence that was typed.
printf("%s " , argv[i]);
}
printf("%c", in);
printf("\n");
printf("%c%*c\n", in, size, in);// same as the second line.
printf("%d", size);
for( int b = 0; b <= size ; b++){ //causing the infinite loop
printf("%c", in);
}
printf("\n");
}
return 0;
My first loop works fine:
for( int a = 0; a <= size ; a++){
printf("%c", in); // prints the first border line
}
but when I included the second one, which is identical, it caused my program to continue infinately.
for( int b = 0; b <= size ; b++){
printf("%c", in);
}
As was mentioned in the comments, you never initialize size which means it has some random value. So you end up printing a number of border characters equal to that random value plus the intended length of the border.
If you do this:
int size = 0;
That will solve the problem. Sample input/output:
[dbush#db-centos tmp]$ ./x1 test text
Enter the character for the border: x
10
xxxxxxxxxxxxx
x x
x test text x
x x
12xxxxxxxxxxxxx
So now we see that there's a number stuck at the bottom border. That's because you're printing size before the bottom border, so you should move that printf to after the loop to print that border.
So change this:
printf("%c%*c\n", in, size, in);// same as the second line.
printf("%d", size);
for( int b = 0; b <= size ; b++){ //causing the infinite loop
printf("%c", in);
}
To this:
printf("%c%*c\n", in, size, in);// same as the second line.
for( int b = 0; b <= size ; b++){ //causing the infinite loop
printf("%c", in);
}
printf("%d", size); // this line moved down
I am trying to get the most frequent characters from an array.
Here's my code
#include <stdio.h>
int main(void)
{
int c[1000];
char input[] = "abcdab";
int i;
for(i=0; input[i]; i++)
{
c[input[i]]++;
}
int j = 0;
char str = 0;
for(i=0; i<256; i++)
{
if(c[i] > j)
{
j = c[i];
str = i;
}
}
printf("%c\n", str);
return 0;
}
It returns 'a'
But I want to get 'a' and 'b' since they are the most frequent characters in the array.
Any help would be appreciated, thank you.
You are passing through the entire array looking for a maximum, and remembering the first one. With the solution you have, you need an additional loop:
for(i=0; i<256; i++){ // Look for all maximums
if(c[i] == j) // If it is the maximum
{
printf("%c\n", i); // print the character
}
}
Note that your array c is not initialized to all zeroes, so it is purely by chance (not really) that the code is working. If you want c to be all zeroes, you need to declare it as int c[1000] = {0}; or to call memset on it.
I want to input an array of integers without giving spaces.
For ex:- 032146548 ,each integer should be stored in array distinctly ,
i.e a[0]=0,a[1]=3,a[2]=2 and so on.
How can i do this ?
I think it's clearer to say "each digit", since it's not at all obvious how many "integers" the character sequence 032146548 represents (the common practice is "one") once you know it's supposed to be several.
The simplest way is to just read it in as a string of digits, then convert each digit to its integer counterpart by subtracting '0':
char line[12];
unsigned int a[10];
if(fgets(line, sizeof line, stdin) != NULL)
{
const size_t digits = strlen(line) - 1;
for(size_t i = 0; i < sizeof a; ++i)
{
if(i < digits && isdigit((unsigned int) line[i]))
a[i] = line[i] - '0';
else
a[i] = 0;
}
}
Use this if you are reading from file,
int i=0;
while(scanf("%1d",&a[i])==1)
{
i++;
}
Use this if you know how many inputs are there,
for(int i=0;i<inputLength;i++)
{
scanf("%1d",&a[i]);
}
#include <stdio.h>
int main(){
int a[16];
int i, j, stat;
char ch[2] ={0};
for(i=0;i<16;++i){
if(1!=(stat=scanf("%1d%1[^0-9]", &a[i], ch))){
if(stat==2)
++i;
break;
}
}
for(j=0;j<i;++j)
printf("%d ", a[j]);
printf("\n");
return 0;
}