i wrote the following code in c to print floyd's triangle.
int main()
{
printf("Enter the number of rows you want to have");
int t;
scanf("%d",&t);
int i;
char a[1000] ="";
for(i=1;i<=t;i++)
{
if (i%2!=0)
{
strcat("1",a);
printf("%c\n",a);}
else
strcat("0",a);
printf("%c\n",a);
}
return 0;
}
The program seems fine to me but it stops working as soon as i execute it. Please help
I want to have the output as follows-
1
01
101
0101
10101
and so on
You can construct the string (the bigger one) first and then print only a part of it in each row:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Enter the number of rows you want to have");
int t;
scanf("%d",&t); // You should check the return value...
puts("");
char pattern[t + 1]; // It's a VLA, so you need a C99 compliant compiler or
// use your big array...
// Initialize the string (it's the last row) starting from the last
// char (the null terminator) and stepping back to first. Every row should
// end with a '1', so in the loop, start with it.
int i = t;
pattern[i] = '\0';
while ( i > 0 )
{
pattern[--i] = '1';
if ( i == 0 )
break;
pattern[--i] = '0';
}
// Print only the part needed in each row
char* tmp = &pattern[t - 1];
for ( int i = 0; i < t; ++i, --tmp )
{
printf("%s\n", tmp);
}
return 0;
}
Compile with warnings enabled and you will quickly see that you need to print a with %s (string format), rather than %c (character format). When I compiled your code, I got:
prog.c: In function 'main':
prog.c:16:22: warning: format '%c' expects argument of type 'int', but argument 2 has type 'char *' [-Wformat=]
printf("%c\n",a);
~^ ~
%s
prog.c:20:22: warning: format '%c' expects argument of type 'int', but argument 2 has type 'char *' [-Wformat=]
printf("%c\n",a);
~^ ~
%s
Moreover, your else statement lacks curly braces, which results in only the strcat() to be assumed as its body.
To get the desired output, you should abandon strcat() and index the position you want to assign the bit, like this:
#include <stdio.h>
#include <string.h>
int main()
{
printf("Enter the number of rows you want to have\n");
int t;
scanf("%d",&t);
int i;
char a[1000] ="";
for(i=1;i<=t;i++)
{
if (i%2!=0)
{
a[999 - i] = '1';
printf("%s\n", &a[999 - i]);
}
else {
a[999 - i] = '0';
printf("%s\n", &a[999 - i]);
}
}
return 0;
}
Output:
Enter the number of rows you want to have
4
1
01
101
0101
Notice that 999 is the size of your array, minus 1.
PS: In your posted code: when concatenating the string, you messed up the order of the arguments.
This should give you the output you're looking for:
#include <iostream>
#include <string.h>
int main()
{
printf("Enter the number of rows you want to have: ");
int t;
scanf("%d", &t);
for (int i = 1; i <= t; i++)
{
for (int j = 1; j <= i; j++)
{
char a[1000] = "";
if ((i+j) % 2 == 0)
strcat(a, "1");
else
strcat(a, "0");
printf("%s", a);
}
printf("\n");
}
return 0;
}
Since every other line begins with a 0, you could simply recreate the string a per line.
Related
There's a bunch of threads about C++, but I'm stuck with regular C here and can't find an answer anywhere.
#include <stdio.h>
#include <conio.h>
#include <string.h>
main() {
char word[26], letter, reverse[26], length;
printf("Enter word: ");
scanf("%s", &word);
length=strlen(word);
for (int i=0; i<=length; i++) {
letter = word[length-i];
strcpy(reverse, letter);
printf("%c\n", reverse);
}
getch();
}
I'm getting errors as the title states.
13 25 ~ [Error] invalid conversion from 'char' to 'const char*'
[-fpermissive]
51 18 C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include\string.h [Note]
initializing argument 2 of 'char* strcpy(char*, const char*)'
What the code should do is reverse the written word and type it out line by line, by each last letter.
Etc. - Type in "food" and the program should output:
d
od
ood
food
I know I messed up somewhere in the 'letter', but I have no clue how to fix it.
Also I'm trying to avoid using std::, personal prefrence.
Try this:
int main( void ) {
char word[26], reverse[26];
int length, i;
printf("Enter word: ");
scanf("%s", &word);
length=strlen(word);
for (i=0; i<length; i++) {
reverse[i] = word[length - i - 1];
}
/* Put \0 to terminate the string */
reverse[length]='\0';
printf("%s\n", reverse);
}
Note that in the for loop you have to use only < and not <= because you'll get out of bounds of the array (negative index) and you have to put the \0 terminator to terminate your reverse string. You don't need letter and btw strcpy works not with single chars, since a single char is not a string (because of the missing string terminator (\0)).
Edit:
int main( void ) {
char word[26], reverse[26];
int length, i;
printf("Enter word: ");
scanf("%s", &word);
length=strlen(word);
for (i=0; i<length; i++) {
reverse[i] = word[length - i - 1];
/* Put \0 to terminate the string */
reverse[i + 1]='\0';
printf("%s\n", reverse);
}
}
Problems:
You pass the address of an array to scanf, rather than passing the array.
You don't copy the null termination correctly.
strcat must have null terminated strings as parameters, it cannot work with single characters. You don't need to use it here anyway.
Fixed program:
#include <stdio.h>
#include <string.h>
int main (void)
{
char word[26];
char reverse[26];
size_t length;
size_t i;
printf("Enter word: ");
scanf("%s", word);
length=strlen(word);
for (i=0; i<length; i++)
{
reverse[i] = word[length-i-1];
}
reverse[i] = '\0';
printf("Reverse: %s\n", reverse);
return 0;
}
Output:
Enter word: stackoverflow
Reverse: wolfrevokcats
There are a bunch of errors in the code. The one that you are asking about is due to the fact that you are passing a char (i.e. the variable letter) rather than the address (i.e. &letter)
This "fix" would cause the compiler error to disappear, however it would cause another one to appear. This is because we'd now be passing a pointer to a char rather than a pointer to a terminated string.
for (i=0 ; i<26 ; i++)
reverse[i] = word[25-i];
Here i have to pass in strcat(char * s1, const char * s2);
Here reverse is the cha* and &letter is const char*.
Check out this one.
this is the code what you want as output.
int main()
{
char word[26], letter, length;
int n, c, d;
char reverse[26];
char temp[26];
printf("Enter word: ");
scanf("%s", word);
length=strlen(word);
for (int i=0; i<=length; i++)
{
letter = word[length-i];
strcat(reverse, &letter);
n = strlen(reverse);
for (c = n - 1, d = 0; c >= 0; c--, d++)
temp[d] = reverse[c];
temp[d] = '\0';
printf("%s\n", temp);
}
return 0;
}
Output :-
I need to code a program that gets input values for a string, then it ignores the characters that are not digits and it uses the digits from the string to create an integer and display it. here are some strings turned into integers as stated in the exercise.
I wanted to go through the string as through a vector, then test if the each position is a digit using isdigit(s[i]), then put these values in another vector which creates a number using the digits. At the end it's supposed to output the number. I can't for the life of it figure what's wrong, please help.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
char *s;
scanf("%s", s);
printf("%s\n", s);
int i, n=0, v[100], nr=0;
for(i=0; i<strlen(s); i++)
{
if (isdigit(s[i]) == 1)
{
v[i] = s[i];
n++;
}
}
for(i=0;i<n;i++)
{
printf("%c\n", v[i]);
}
for(i=0; i<n; i++)
{
nr = nr * 10;
nr = nr + v[i];
}
printf("%d", nr);
return 0;
}
The pointer s is unintialized which is your major problem. But there are other problems too.
isdigit() is documented to return a non-zero return code which is not necessarily 1.
The argument to isdigit() needs to be cast to unsigned char to avoid potential undefined behaviour.
Your array v is also using the same index variable i - which is not right. Use a different variable to index v when you store the digits.
You need to subtract '0' to get the each digits integer equivalent.
scanf()'s format %s can't handle inputs with space (among other problems). So, use fgets().
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void)
{
char s[256];
fgets(s, sizeof s, stdin);
s[strcspn(s, "\n")] = 0; /* remove trailing newline if present */
printf("%s\n", s);
int i, n = 0, v[100], nr = 0;
size_t j = 0;
for(i = 0; i < s[i]; i++)
{
if (isdigit((unsigned char)s[i]))
{
v[j++] = s[i];
n++;
}
}
for(i = 0;i < j; i++)
{
printf("%c\n", v[i]);
}
if (j) { /* No digit was seen */
int multiply = 1;
for(i= j-1 ; i >= 0; i--) {
nr = nr + (v[i] - '0') * multiply;
multiply *= 10;
}
}
printf("%d", nr);
return 0;
}
In addition be aware of integer overflow of nr (and/or multiply) can't hold if your input contains too many digits.
Another potential source of issue is that if you input over 100 digits then it'll overflow the array v, leading to undefined behaviour.
Thanks a lot for your help, i followed someone's advice and replaced
v[i] = s[i] -> v[n] = s[i] and changed char *s with char s[100]
now it works perfectly, i got rid of the variable nr and just output the numbers without separating them through \n . Thanks for the debugger comment too, I didn't know I can use that effectively.
Firstly, you did not allocate any memory, I changed that to a fixed array.
Your use of scanf will stop at the first space (as in the first example input).
Next, you don't use the right array index when writing digits int v[]. However I have removed all that and simply used any digit that occurs.
You did not read the man page for isdigit. It does not return 1 for a digit. It returns a nonzero value so I removed the explicit test and left it as implicit for non-0 result.
I changed the string length and loop types to size_t, moving the multiple strlen calls ouside of the loop.
You have also not seen that digits' character values are not integer values, so I have subtracted '0' to make them so.
Lastly I changed the target type to unsigned since you will ignore any minus sign.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void)
{
char s[100]; // allocate memory
unsigned nr = 0; // you never check a `-` sign
size_t i, len; // correct types
if(fgets(s, sizeof s, stdin) != NULL) { // scanf stops at `space` in you example
len = strlen(s); // outside of the loop
for(i=0; i<len; i++) {
if(isdigit(s[i])) { // do not test specifically == 1
nr = nr * 10;
nr = nr + s[i] - '0'; // character adjustment
}
}
printf("%u\n", nr); // unsigned
}
return 0;
}
Program session:
a2c3 8*5+=
2385
Just use this
#include <stdio.h>
#include <ctype.h>
int main()
{
int c;
while ((c = getchar()) != EOF) {
switch (c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case '\n':
putchar(c); break;
}
}
return 0;
} /* main */
This is a sample execution:
$ pru_$$
aspj pjsd psajf pasdjfpaojfdapsjd 2 4 1
241
1089u 0u 309u1309u98u 093u82 40981u2 982u4 09832u4901u 409u 019u019u 0u3 0ue
10890309130998093824098129824098324901409019019030
Very elegant! :)
I have I problem. I get 2 warnings from console, but I dont know what's wrong with my code. Can you have look?
Program suppose to show lines with at least 11 characters and 4 numbers
#include <stdio.h>
#include <ctype.h>
int main()
{
char line[200];
printf("Enter a string: \n");
while(fgets(line, sizeof(line),stdin))
{
int numberAlpha = 0;
int numberDigit = 0;
if(isalpha(line)) numberAlpha++;
else if(isdigit(line)) numberDigit++;
if(numberAlpha+numberDigit>10 && numberDigit>3) printf("%s \n", line);
}
return 0;
}
Both isalpha() and isdigit() takes an int, not a char *, as argument.
In your code, by passing the array name as the argument, you're essentially passing a char * (array name decays to the pointer to the first element when used as function argument), so, you're getting the warning.
You need to loop over the individual elements of line and pass them to the functions.
That said, just a suggestion, for hosted environment, int main() should be int main(void) to conform to the standard.
isalpha and isdigit are supposed to test if a char taken as int (a char can be safely converted to an int) is the encoding of an alphanumeric or digit character. You pass a char array, not an individual char. You need to test each char of the string you got, so you need a loop as:
for (int i=0; i<strlen(line); i++) {
if (isalpha(line[i])) numberAlpha++;
...
}
It is better to compute the length once:
int length = strlen(line);
for (int i=0; i<length; i++) {
...
}
You may also use a pointer to move along the string:
for (char *ptr = line; *ptr!=`\0`; ptr++) {
if (isalpha(*ptr)) ...
...
}
isalpha() and isdigit() functions take an int. But you are passing a char* i.e. the array line gets converted into a pointer to its first element (see: What is array decaying?). That's what the compiler complains about. You need to loop over line to find the number of digits and alphabets in it.
Also note that fgets() will read in the newline character if line has space. So, you need to trim it out before counting.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void)
{
char line[200];
printf("Enter a string: \n");
while(fgets(line, sizeof(line),stdin))
{
int numberAlpha = 0;
int numberDigit = 0;
line[strcspn(line, "\n")] = 0; // Remove the trailing newline, if any.
for (size_t i = 0; line[i]; i++) {
if(isalpha((unsigned char)line[i])) numberAlpha++;
else if((unsigned char)isdigit(line[i])) numberDigit++;
}
printf("alpha: %d, digits:%d \n", numberAlpha, numberDigit);
}
return 0;
}
Ok, i got something like this:
#include <stdio.h>
#include <ctype.h>
int main()
{
char line[200];
printf("Enter a string: \n");
while(fgets(line, sizeof(line),stdin))
{
int numberAlpha = 0;
int numberDigit = 0;
int i;
for(i=0; i<strlen(line); i++){
if(isalpha(line[i])) numberAlpha++;
else if(isdigit(line[i])) numberDigit++;
}
if(numberAlpha+numberDigit>10 && numberDigit>3) printf("%s \n", line);
}
return 0;
}
Now the question is, if it is passible to make it first accepts data and then display only those line which follows the if statment. Now it shows line just after input it.
I'm trying to write a program that gets a string, and a number, and calculates the length of it and shifting all the elents right.
I have 2 errors:
1.assignment makes pointer from integer without a cast.
2.assignment makes integer from pointer without a cast.
#include <stdio.h>
#include <string.h>
#define N 10
int myStrlen(char*);
void shiftRight(char*, int);
int main() {
char str[N] = {0};
int num = 0;
int len;
/* input of the string */
scanf("%s",str);
scanf("%d",&num);
len=myStrlen(str);
if(num>=0) {
shiftRight(str, num);
printf("%s\n",str);
}
else
{
printf("%s\n", str);
}
return 0;
}
int myStrlen(char*str)
{
int my_len=0;
while (str[my_len] != '\0')
{
my_len++;
}
return my_len;
}
void shiftRight(char* str, int num)
{
int i;
char* j;
int count;
j=(str[N-1]);
for(count=0;count<num;count++)
{
for(i=N-1;i>0;--i)
{
str[i]=str[i-1];
}
str[0]=j;
}
}
Your answers are welcome,anf if you anything wrong with this code,please mention it.
As your compiler will have told you, pointer from integer without a cast is at
j=(str[N-1]);
And integer from pointer is at
str[0]=j;
You should have declared j as char j;
But now when i run it, and typing lets say ball as a string and 1 to
be a number, i get nothing from the program instead of getting "lbal"
You have all the correct elements but that's not enough. Writing a program is telling a story, you need to set the scene, describe what happens along the way and conclude your narrative. A story with elements out of order is nonsense, as is a program.
Specific issues with your code: you're saving of the last character (to restore it to the beginning of the string) is in the wrong place; you're using the allocation of the string when you should be using it's length (and conveniently, you have a function for that!); this is really more of a rotation than a shift; use the most descriptive variable names you can, not the shortest you can get away with; pick one indentation style and stick with it -- it can change between programs you write but shouldn't change within an individual program.
Below is a rework of your code addressing some of the issues above:
#include <stdio.h>
#define STRING_SIZE 10
int myStrlen(char *string)
{
int length = 0;
while (string[length] != '\0')
{
length++;
}
return length;
}
void rotateRight(char *string, int number)
{
int length = myStrlen(string);
for (int count = 0; count < number; count++)
{
char j = string[length - 1];
for (int i = length - 1; i > 0; i--)
{
string[i] = string[i - 1];
}
string[0] = j;
}
}
int main()
{
char string[STRING_SIZE] = {0};
int number = 0;
/* input of the string */
scanf("%s", string);
scanf("%d", &number);
if (number > 0)
{
rotateRight(string, number);
printf("%s\n", string);
}
else
{
printf("%s\n", string);
}
return 0;
}
OUTPUT
% ./a.out
elephant
3
anteleph
%
I have a string, like "101 1 13" and I need to split it to a int aux[3] --> resulting in aux[0] = 101, aux[1] = 1 and aux[2] = 13 (in this case). How can
I do that?
In the example of the code below I get op as a String and want to get the value of the INTs in there. Each int is divided in the string by a white space(" ").
Another detail: I need the code to compile with flag -std=c99, so the answer that was accepted would not work.
#include <stdio.h>
#include <stdlib.h>
//example of str = "101 1 14" (char *)
// example of output = {101, 1, 14}(int *)
int* stoi(char *str) {
// function to split str into 3 ints
}
int main() {
char op[10];
int num[3];
scanf("%s\n", op);
num = stoi(op);
printf("%d %d %d", num[0], num[1], num[2]);
return 0;
}
First you need to tokenize your input (break apart the input into distinct elements). Then you need to parse/integerize the individual tokens by converting them from strings to the desired format.
Sample Code
#include <stdio.h>
#include <string.h>
#define BUF_LEN (64)
int main(void)
{
char buf[BUF_LEN] = { 0 };
char* rest = buf;
char* token;
int i = 0;
int iArr[100] = { 0 };
if ( fgets(buf, BUF_LEN, stdin) != NULL )
{
strtok(buf, "\n"); // Remove newline from input buffer in case we want to call fgets() again.
while ( (token = strtok_r(rest, " ", &rest)) != NULL )
{
iArr[i] = strtol(token, NULL, 10);
printf("Token %d:[%d].\n", i, iArr[i]);
i++;
}
}
return 0;
}
Sample Run
1231 12312 312 1232 1312
Token 0:[1231].
Token 1:[12312].
Token 2:[312].
Token 3:[1232].
Token 4:[1312].
Try to replace your code by following code.
The new code works only if input contains only single space between integers.
Your code:
while(op[cont] != '\0') {
for(i = 0; op[cont] != ' '; i++, cont++) {
num[i] += op[cont];
}
printf("num[i] = %d\n", num[i]);
}
New code:
while(op[cont] != '\0')
{
if(op[cont] != ' ')
num[i] = num[i]*10 + (op[cont]- '0');
else
i++;
cont++;
}
See this example of how to do that:
char string [10] = "101 1 666"
int v [3], n=0, j=0;
int tam = strlen(string);
int current_Len = 0;
for(i=0; i<tam; i++){
//32 = ascii for White space
if(string[i] != 32){
n = n*10 + string[i] - '0';
current_len++;
} else if (current_len > 0){
v[j++] = n;
current_len = 0;
n=0;
}
}
if (current_len > 0){
v[j++] = n;
}
This answer is assuming you know how much integers your string contain at the time of writing your code. It also uses specific clang/gcc extension (typeof) and may not be portable. But it may be helpful to someone (I mainly wrote it because I had nothing good to do).
#include <stdio.h>
#include <string.h>
struct {int _[3];} strToInt3(const char (*pStr)[])
{
int result[3] = {0}, *pr = result;
for(register const char *p = *pStr; *p; ++p)
{
if(*p == ' ') ++pr;
else
*pr *= 10,
*pr += *p - '0';
}
return *(__typeof__(strToInt3(0)) *)result;
}
int main()
{
char op[10];
int num[3];
scanf("%10[^\n]", op),
//memcpy(num, strToInt3(op)._, sizeof(num));
//or
*(__typeof__(strToInt3(0)) *)num = strToInt3(op);
printf("%d %d %d", num[0], num[1], num[2]);
}
I've commented the copying of returned array using memcpy and added a structure assignment. Although both must be valid (not standard I guess but working in most cases) I prefer the second option (and maybe some compiler optimizers will).
Also I assume ASCII character set for chars.
I found an easier approach to the problem. I insert a scanf, that don't catch the space blanket and convert it using atoi. As it is just 3 ints it doesn't become so bad to use this simple, repetitive way of catching the values. And it work with the -std=c99 flag, that I needed to use.
scanf("%s[^ ]\n", op);
num[0] = atoi(op);
scanf("%s[^ ]\n", op);
num[1] = atoi(op);
scanf("%s[^ ]\n", op);
num[2] = atoi(op);
printf("%d\n", num[0]);
printf("%d\n", num[1]);
printf("%d\n", num[2]);