Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
In line no 20,27,32,36 even though the value is true the count value is not increasing at the end it is showing 0 count value. i think i have made an mistake while comparing array. i dont know how to compare an char element 2d array
the input is
1
xxx
ooo
the answer must be 3
the code is as below
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; n++)
{
int count = 0;
char t[2][2];
for (int i = 1; i <= 3; i++)
{
scanf("%c %c %c", &t[i][0], &t[i][1], &t[i][2]);
}
if (t[0][0] == '_' || t[0][1] == '_' || t[0][2] == '_' || t[1][0] == '_' || t[1][1] == '_' || t[1][2] == '_' || t[2][0] == '_' || t[2][1] == '_' || t[2][2] == '_')
{
for (int j = 0; j < 3; j++)
{
if (t[j][0] == t[j][1] == t[j][2])
{
20) count++;
}
}
for (int q = 0; q < 3; q++)
{
if (t[0][q] == t[1][q] == t[2][q])
{
27) count++;
}
}
if (t[0][0] == t[1][1] == t[2][2])
{
32) count++;
}
else if (t[0][2] == t[1][1] == t[2][0])
{
36) count++;
}
if (count == 0)
{
printf("%d", 2);
}
else if (count == 1)
{
printf("%d", 1);
}
else if (count > 1)
{
printf("%d", 3);
}
}
}
}
Code below is probably what you're looking for:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, count=0; char s[20];
printf("Enter total number of iterations (n): ");
scanf("%s", s); // Don't use %d. If you use %d and input a character, \
buffer issues arise.
printf("\n");
n = atoi(s);
for (int i = 0; i < n; i++)
{
int count = 0;
char t[3][3];
for (int x = 0; x < 3; x++)
{
printf("Enter 3 chars with spaces between them: ");
scanf(" %c %c %c", &t[x][0], &t[x][1], &t[x][2]); // Space before\
the first %c makes scanf \
skip leading whitespace.
printf("\n");
}
printf("n is %d\n\
t[0][0] = %c, t[0][1] = %c, t[0][2] = %c\n\
t[1][0] = %c, t[1][1] = %c, t[1][2] = %c\n\
t[2][0] = %c, t[2][1] = %c, t[2][2] = %c\n", n, \
\
t[0][0], t[0][1], t[0][2], \
t[1][0], t[1][1], t[1][2], \
t[2][0], t[2][1], t[2][2]);
if (t[0][0] == '_' || t[0][1] == '_' || t[0][2] == '_' || \
t[1][0] == '_' || t[1][1] == '_' || t[1][2] == '_' || \
t[2][0] == '_' || t[2][1] == '_' || t[2][2] == '_')
{
for (int j = 0; j < 3; j++)
{
if ((t[j][0] == t[j][1]) && (t[j][1] == t[j][2]))
{
count++;
}
}
for (int q = 0; q < 3; q++)
{
if ((t[0][q] == t[1][q]) && (t[1][q] == t[2][q]))
{
count++;
}
}
if ((t[0][0] == t[1][1]) && (t[1][1] == t[2][2]))
{
count++;
}
else if ((t[0][2] == t[1][1]) && (t[1][1] == t[2][0]))
{
count++;
}
if (count == 0)
{
printf("%d", 2);
}
else if (count == 1)
{
printf("%d", 1);
}
else if (count > 1)
{
printf("%d", 3);
}
}
}
return(0);
}
Output is as follows (count seems to be > 1, so 3 is printed):
Enter total number of iterations (n): 1
Enter 3 chars with spaces between them: x x x
Enter 3 chars with spaces between them: o o o
Enter 3 chars with spaces between them: _ _ _
n is 1
t[0][0] = x, t[0][1] = x, t[0][2] = x
t[1][0] = o, t[1][1] = o, t[1][2] = o
t[2][0] = _, t[2][1] = _, t[2][2] = _
3
Related
I was trying this pattern matching method in C but whenever I give all the input, the vscode terminal waits for a while and just stops the program without any warnings/message. Can anyone point to what is wrong here?
#include <stdio.h>
#include <string.h>
int main()
{
char STR[100], PAT[100], REP[100], ANS[100];
int i, m, j, k, flag, slP, slR, len;
i = m = k = j = flag = len = 0;
printf("\nMain String: ");
gets(STR);
printf("\nPattern String: ");
gets(PAT);
slP = strlen(PAT);
printf("\nReplace String: ");
gets(REP);
slR = strlen(REP);
while (STR[i] != '\0')
{
if (STR[i] = PAT[j])
{
len = 0;
for (k = 0; k < slP; k++)
{
if (STR[k] = PAT[k])
len++;
}
if (len == slP)
{
flag = 1;
for (k = 0; k < slR; k++, m++)
ANS[m] = REP[k];
}
}
else
{
ANS[m] = STR[i];
m++;
i++;
}
}
if (flag == 0)
{
printf("\nPattern not found!");
}
else
{
ANS[m] = '\0';
printf("\nResultant String: %s\n", ANS);
}
return 0;
}
There are multiple problems in the code:
using gets() is risky, this function was removed from the C Standard because it cannot be used safely.
if (STR[i] = PAT[j]) copied the pattern to the string. You should use:
if (STR[i] == PAT[j])
similarly, if (STR[k] = PAT[k]) is incorrect. You should compare PAT[k] and STR[i + k]:
if (STR[i + k] == PAT[k])
you should test for buffer overflow for the output string as replacing a short string by a larger one may produce a string that will not fit in ANS
you do not increment i properly.
Here is a modified version:
#include <stdio.h>
int getstr(const char *prompt, char *dest, int size) {
int c, len = 0;
printf("%s", prompt);
while ((c = getchar()) != EOF && c != '\n') {
if (len + 1 < size)
dest[len++] = c;
}
if (size > 0)
dest[len] = '\0';
printf("\n");
if (c == EOF && len == 0)
return -1;
else
return len;
}
int main() {
char STR[100], PAT[100], REP[100], ANS[100];
int i, m, k, flag;
if (getstr("Main String: ", STR, sizeof STR) < 0)
return 1;
if (getstr("Pattern String: ", PAT, sizeof PAT) < 0)
return 1;
if (getstr("Replace String: ", REP, sizeof REP) < 0)
return 1;
i = m = flag = 0;
while (STR[i] != '\0') {
if (STR[i] == PAT[0]) { // initial match
// compare the rest of the pattern
for (k = 1; PAT[k] != '\0' && PAT[k] == STR[i + k]; k++)
continue;
if (PAT[k] == '\0') { // complete match
flag = 1;
// copy the replacement string
for (k = 0; REP[k] != '\0'; k++) {
if (m + 1 < sizeof ANS)
ANS[m++] = REP[k];
}
i += k; // skip the matching characters
continue;
}
}
// otherwise copy a single character
if (m + 1 < sizeof ANS)
ANS[m++] = STR[i];
i++;
}
ANS[m] = '\0';
if (flag == 0) {
printf("Pattern not found!\n");
} else {
printf("Resultant String: %s\n", ANS);
}
return 0;
}
the push to vaccinate children has taken on fresh urgency amid concerns that the new omicron variant of the virus first identified in southern africa and hong kong in late november will spread quickly in the united states causing a surge in infections already back on the rise from the easily transmitted delta variant given the pervasiveness of delta and prospects of new variants spreading in the united states having as much immunity in the population as possible is critical said dr amesh adalja senior scholar at the johns hopkins center for health security
This is my assignment:
replace multiple spaces to one space between words and delete unnecessary spaces at the beginning and the end.
count the words
print edited string
dont use a new string, just edit.
I can't find problem. It should count the words but it can not do. Help me, please.
//Counting words program C
#include <stdio.h>
#define N 5000
int main(void) {
FILE *fp;
char text[N];
int k, d, leng, spacecount = 0;
int m, j, z, i, p, n;
if ((fp = fopen("soru.txt", "r")) == NULL) {
printf("Dosya acma hatasi!");
return 1;
}
fgets(text, N - 1, fp);
while (k < N && text[k] != '\0') {
leng++;
k++;
}
z = leng;
for (i = 0; i < leng; i++) {
if (i = 0 && text[i] == ' ') {
z--;
for (m = 0; m < leng; m++) {
text[m] = text[m + 1];
}
i--;
text[z] == '\0';
} else
if (text[i] ==' ' && text[i + 1] == ' ') {
z--;
for (j = i; j < leng; j++) {
text[j + 1] = text[j + 2];
}
i--;
text[z] == '\0';
} else
if (text[i] == ' ' && text[i + 1] == '\0') {
z--;
for (j = i; j < leng; j++) {
text[j] = text[j + 1];
}
i--;
text[z] == '\0';
} else
if (text[i] == '\0') {
break;
}
}
while (text[d] != '\0') {
if (text[d] == ' ')
spacecount++;
d++;
}
printf("kelime sayisi: %d" , spacecount + 1);
printf("\n cikti:%s ", text);
fclose(fp);
return 0;
}
I can't find problem. It should count the word but it can not do. Help me, please
for(i=0; i < leng; i++) {
if(i=0 && text[i]== ' '){
z--;
for(m=0; m< leng; m++ ){
text[m] = text [m+1];}
i--;
}
else if(1<i<z && text[i] ==' ' && text[i+1] == ' ' ){
z--;
for(j=i; j<leng ; j++) {
text[j+1] = text [j+2];}
i--;
}
else if(i=z && text[i] ==' ' && text[i+1] == '\0' ){
z--;
for(j=i; j<leng ; j++) {
text[j] = text [j+1]; }
i--;
}
},// I think problem in here. Endless loop
Your code is too complicated. You can solve the problem with 2 index variables: one to read the characters from the input line, one to write the relevant characters into the same buffer.
You would keep track of the previous character, starting with space, and detect the beginning of words as the current character is not a space following a space. You would thus count the words and only output a space before each word except the first on a line.
Here is a modified version:
//Counting words program C
#include <stdio.h>
#define N 5000
int main(void) {
FILE *fp;
char text[N];
int total_words = 0;
if ((fp = fopen("soru.txt", "r")) == NULL) {
printf("Dosya açma hatası!\n");
return 1;
}
while (fgets(text, N, fp) != NULL) {
int len = strlen(text);
int word_count = 0;
char c, lastc = ' ';
int i, j;
// strip the trailing newline
if (len > 0 && text[len - 1] == '\n') {
text[--len] == '\0';
}
for (i = j = 0; i < len; i++) {
c = text[i];
if (c != ' ') {
if (lastc == ' ') {
if (word_count > 0) {
// output a space between words
text[j++] = ' ';
}
word_count++;
}
text[j++] = c; // copy the non space character
}
lastc = c;
}
text[j] = '\0'; // set the null terminator
printf("kelime sayısı: %d\n", word_count);
printf("çıktı: %s\n", text);
total_words += word_count;
}
fclose(fp);
printf("\ntoplam kelime sayısı: %d\n", total_words);
return 0;
}
Note a silly bug in your code: if (i = 0 && text[i] == ' ') is parsed as if ((i = (0 && (text[i] == ' '))) != 0) which is always false and sets i to the value 0. C expression syntax is very powerful but somewhat error prone and confusing. I advise you to use -Wall or -Weverything as a compiler option to let the compiler warn about potential mistakes.
Similarly, you should not write if (1<i<z && ...: 1<i<z is parsed as 1<(i<z) which is always false. You must write 1 < i && i < z or more idiomatically i > 1 && i < z
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
The goal of this program is to ask user for input and print the 3 most frequent characters from user's string. After few days I managed to make this kind of work. I mean kind of because if input will be "aaaaaaabbbbbccxz" program will work just fine, but if input will be "abc" program prints wrong values. Same with "aabbc", empty string, etc. I've been trying to fix that but with no luck. I have no idea what to do.
There's my code:
#include <stdio.h>
#include <stdlib.h>
#define N 100
/*
ask user to type letter string and load it to the array.
Count apperance of ASCII characters in the string
Print 3 most frequent characters from the string and how often they appeared.
*/
int main(int argc, char *argv[])
{
int ascii[256] = {0};
char str[N];
int z, i, j, k, top, top2, top3, index, index2, index3;
printf("Type your string: \n");
scanf("%s", &str);
for(i = 0; str[i] != 0; i++)
{
++ascii[str[i]];
}
top = ascii[0];
index = 0;
for(z = 0; str[z] != 0; z++)
{
if( ascii[str[z]] > top)
{
top = ascii[str[z]];
index = z;
}
}
printf("The most frequent is %c - was %d times.\n", str[index], top);
top2 = ascii[0];
index2 = 0;
for(j = 0; str[j] != 0; j++)
{
if( ascii[str[j]] > top2 && ascii[str[j]] < top)
{
top2 = ascii[str[j]];
index2 = j;
}
}
printf("second most frequent %c %d times.\n", str[index2], top2);
top3 = ascii[0];
index3 = 0;
for(k = 0; str[k] != 0; k++)
{
if( ascii[str[k]] > top3 && ascii[str[k]] < top2 && ascii[str[k]] < top)
{
top3 = ascii[str[k]];
index3 = k;
}
}
printf("3rd most frequent %c %d times.\n", str[index3], top3);
return 0;
}
EDIT:
it works.
#include <stdio.h>
#include <stdlib.h>
#define N 100
int main(int argc, char *argv[])
{
int ascii[256] = {0};
char str[N];
int x, y, z, i, j, k, top, top2, top3, index, index2, index3, len;
do
{
printf("String input here: \n");
fgets(str, N, stdin);
//scanf("%s", &str);
}
while (str[0] == '\n');
for(i = 0; str[i] != 0; i++)
{
++ascii[str[i]];
}
top = ascii[0];
len = strlen(str);
index = -1;
for(z = 0; str[z] != 0; z++)
{
if( ascii[str[z]] > top)
{
top = ascii[str[z]];
index = z;
}
}
if (index == -1) return printf("This string is empty\n");
else if (top == 1 && len > 1) return printf("There is no repeated character in that string\n");
else if (top == len) return printf("This string contains a single character '%c' - repeated %d times\n", str[index], top);
else {
// Checks the special case where several characters are repeated n times
char characters[len+1];
int count = 0;
for(i = 0; str[i] != 0; i++)
{
if (ascii[str[i]] == top && str[i] != characters[count-1])
characters[count++] = str[i];
}
characters[count] = 0;
if (count > 1) return printf("The most frequent characters are '%s' - repeated %d times.\n", characters, top);
else printf("The most frequent character is '%c' - repeated %d times.\n", str[index], top);
}
top2 = ascii[0];
index2 = -1;
for(j = 0; str[j] != 0; j++)
{
if( ascii[str[j]] > top2 && ascii[str[j]] < top)
{
top2 = ascii[str[j]];
index2 = j;
}
}
if (index2 == -1)
{
}
else if (top2 == 1 && len > 1)
{
}
else if (top2 == len)
{
}
else {
char characters[len+1];
int count = 0;
for(y = 0; str[y] != 0; y++)
{
if (ascii[str[y]] == top2 && ascii[str[y]] < top && str[y] != characters[count-1])
characters[count++] = str[y];
}
characters[count] = 0;
if (count > 1 )
{
}
else printf("The 2nd most frequent character is '%c' - repeated %d times.\n", str[index2], top2);
}
top3 = ascii[0];
index3 = -1;
for(k = 0; str[k] != 0; k++)
{
if( ascii[str[k]] > top3 && ascii[str[k]] < top2 && ascii[str[k]] < top)
{
top3 = ascii[str[k]];
index3 = k;
}
}
if (index3 == -1)
{
}
else if (top3 == 1 && len > 1)
{
}
else if (top3 == len)
{
}
else {
char characters[len+1];
int count = 0;
for(x = 0; str[x] != 0; x++)
{
if (ascii[str[x]] == top3 && ascii[str[x]] < top && ascii[str[x]] < top2 && str[y] != characters[count-1])
characters[count++] = str[x];
}
characters[count] = 0;
if (count > 1 )
{
}
else printf("The 3rd most frequent character is '%c' - repeated %d times.\n", str[index3], top3);
}
return 0;
}
You could do something like this and handle the second and third cases using a similar logic :
len = strlen(str);
index = -1;
for(z = 0; str[z] != 0; z++)
{
if( ascii[str[z]] > top)
{
top = ascii[str[z]];
index = z;
}
}
if (index == -1) return printf("This string is empty\n");
else if (top == 1 && len > 1) return printf("There is no repeated character in that string\n");
else if (top == len) return printf("This string contains a single character '%c' - repeated %d times\n", str[index], top);
else {
// Checks the special case where several characters are repeated n times
char characters[len+1];
memset(character, 0, len+1);
int count = 0;
for(i = 0; str[i] != 0; i++)
{
if (ascii[str[i]] == top && !strchr(characters, str[i]))
characters[count++] = str[i];
}
characters[count] = 0;
if (count > 1) return printf("The most frequent characters are '%s' - repeated %d times.\n", characters, top);
else printf("The most frequent character is '%c' - repeated %d times.\n", str[index], top);
}
I am coding in C in a university course and we got a project to take equations from the user and give solutions for matrices etc...
My problem is that I am trying to use atof() function and for a reason I can't find in the same loop once it works and the other times it doesn't.
I have tried already other functions to replace atof like strtod but it doesn't work as well.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdbool>
void main()
{
int num, check = 0,i,j,k=0,len1=0;
char equ[80],tempx[20],tempy[20], tempz[20], tempd[20];
double *x, *y, *z, *d;
printf_s("Number of equations (1-3): ");
scanf_s("%d", &num);
getchar();
while (check == 0) //a check to see if we got a number between 1-3.
{
if (num > 0 && num < 4)
check = 1;
else
{
printf_s("Please enter a number between 1-3.\n");
printf_s("Number of equations (1-3): ");
scanf_s("%d", &num);
}
}
x = malloc(sizeof(double)*num);
if (!x) exit(1);
y = malloc(sizeof(double)*num);
if (!y) exit(1);
z = malloc(sizeof(double)*num);
if (!z) exit(1);
d = malloc(sizeof(double)*num);
if (!d) exit(1);
for (i = 0; i < num; i++) //getting the equations and putting them into the matrix
{
printf_s("Enter equation %d: ", i + 1);
gets_s(equ, sizeof(equ));
len1 = strlen(equ);
for (j = 0; j <len1 ; j++)
{
if (equ[j] == 'x')
{
k = 0;
while ((equ[j-k] != '+' || equ[j-k] != '-') && j-k>=0)
{
tempx[j-k] = equ[j-k];
k++;
}
x[i] = atof(tempx);
}
else if (equ[j] == 'y')
{
k = 0;
while ((equ[j-k] != '+' || equ[j-k] != '-') && j - k >= 0)
{
tempy[j-k] = equ[j-k];
k++;
}
y[i] = atof(tempy);
}
else if (equ[j] == 'z')
{
k = 0;
while ((equ[j - k] != '+' || equ[j - k] != '-') && j - k >= 0)
{
tempz[j-k] = equ[j - k];
k++;
}
z[i] = atof(tempz);
}
else if (equ[j] == '=')
{
k = 0;
while (equ[j+k])
{
tempd[k] = equ[j + k];
k++;
}
d[i] = atof(tempd);
}
}
}
free(x);
free(y);
free(z);
free(d);
}
I expected to get the same result in d[i] as I did in x[i] but every time I try to print d[i] I get 0.0000. When I tried the function _strrev on tempd inside atof I got the reverse result inside d[i].
So the problem was that in the last loop i inserted a string that start with "=" and not a number. Apparently atof() doesn't work when the first char is not a number.
So, I am trying to remove the blanks spaces from a sting input by the user. I already have an option where the program counts the vowels and inverts the string. The one where I need help starts with //espaços. What I did was something like: if the "palavra" string, the original one, has a space (' ') in any position, the new string with no space will have the next char from the string "palavra" in that position:
/*palavra = " o l a _ o l a"
[0][1][2][3][4][5][6]
palavra3 = "o l a o l a"
[0][1][2][3][4][5]*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(void) {
char palavra[10];
char palavra2[10];
char palavra3[10];
int i;
int vogais = 0;
int j;
int k;
int espaco = 0;
printf("Introduza uma string: \n");
scanf("%[^\n]", palavra);
//vogais
for (i = 0; palavra[i] != '\0'; i++) {
if (palavra[i] == 'a' || palavra[i] == 'e' || palavra[i] == 'i' ||
palavra[i] == 'o' || palavra[i] == 'u' ||
palavra[i] == 'A' || palavra[i] == 'E' || palavra[i] == 'I' ||
palavra[i] == 'O' || palavra[i] == 'U')
vogais ++;
// else
// printf("");
}
printf("Vogais: %i", vogais);
//invertida
for (i = 0; palavra[i] != '\0'; i++);
{
k = i-1;
}
for (j = 0; j <= i-1; j++) {
palavra2[j] = palavra[k];
k--;
}
printf("\nString invertida: %s", palavra2);
//espaços
for (i = 0; palavra[i]; i++) {
if (palavra[i] == ' ')
palavra3[i] = palavra[i + 1];
//espaco++;
}
// printf("\nNumero de espacos: %i", espaco);
printf("\nString sem espacos: %s", palavra3);
}
Remove the extra ; at the end of for (i = 0; palavra[i] != '\0'; i++);
With the extraneous ;, the loop is empty, the following code executes once with i equal to strlen(palavra).
You can avoid this kind of silly bug by using the Kernighan and Ritchie indentation style: put the { at the end of the line with the if, for, while, do or switch statement. This makes it much less likely to type a spurious ; between the control statement and its block.
To remove the spaces, use the 2 finger method:
//espaços
for (i = j = 0; palavra[i]; i++) {
if (palavra[i] != ' ') {
palavra3[j++] = palavra[i];
}
}
palavra3[j] = '\0'; // set the null terminator
Keep a counter of the new string length k ,check if a character is a space, if it is a space ignore it else increment k and add that character to new string.Example-
int k = 0; //k will be the new string length after the loop
for (int i = 0; palavra[i] != '\0'; i++)
{
if (palavra[i] != ' ')
{
palavra3[k++] = palavra[i];
}
}
palavra3[k] = '\0';
This example also works in the case of multiple consecutive spaces.