//schecking no of vowels in a string.
#include <stdio.h>
#include <string.h>
void printstring();
int main() {
char sai[8]; //allows 8 charecters.
char a,e,j,o,u; //I have used j as i have already used i for iteration
fgets(sai, 8, stdin); //going to enter my name
char *ptr = sai; ///setting pointer for string
int i,t = 0;
for(i = 0;i <= 7; i++){
if(*(ptr+i) == a) || (*(ptr+i) == e) || (*(ptr+i) == j) || (*(ptr + i) == o) || (*(ptr + i) == u){
t = t+1;
}
else {
t = t;
}
}
printf("%d", t);
}
OUTPUT:
The compiler generated an error:
jill.c: In function 'main':
jill.c:12:23: error: expected expression before '||' token
if(*(ptr+i) == a) || (*(ptr+i) == e) || (*(ptr+i) == j) || (*(ptr + i) == o) || (*(ptr + i) == u){
^~
I expected the number of vowels as output, but an error has occured. Where am I going wrong?
Below listed points are incorrect in your code
Englobe the full condition between brackets in your if condition is missing
Single quotes are missing for character in your if statements. please go through here more explanation.
As comment from Ted Lyngmo, Craig Estey a,e,j,o,u are uninitialized.
if (*(ptr + i) == a) ||(*(ptr + i) == e) || (*(ptr + i) == j) || (*(ptr + i) == o) || (*(ptr + i) == u)
changed to
if((ptr[i] == 'a') || (ptr[i] == 'e') || (ptr[i] == 'j') || (ptr[i] == 'o') || (ptr[i] == 'u'))
code
//schecking no of vowels in a string.
#include <stdio.h>
#include <string.h>
void printstring();
int main()
{
char sai[8];//allows 8 charecters.
char a,e,j,o,u; //I have used j as i have already used i for iteration
fgets(sai, 8, stdin); //going to enter my name
char *ptr = sai; ///setting pointer for string
int i,t = 0;
for(i = 0;i <= 7; i++)
{
if((ptr[i] == 'a') ||
(ptr[i] == 'e') ||
(ptr[i] == 'j') ||
(ptr[i] == 'o') ||
(ptr[i] == 'u'))
{
t = t+1;
}
else
{
t = t;
}
}
printf("%d", t);
}
Link for below Output:
apple
2
Related
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
Basically my project is for finding how many vowels and constants a string that I input has. But it doesn't work and I don't know why. I checked if the malloc works but it works find I guess.
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define E_A_LETTERS 26
int check_vowels(char *p_string);
int main(void) {
// Here your code !
char *string;
int vowels;
int constants;
printf("Enter a string: ");
gets(string);
string = (char *)malloc(strlen(string) * sizeof(char));
if (string == NULL) {
printf("Unable to allocate memory...");
exit(0);
}
vowels = check_vowels(&string[0]);
constants = E_A_LETTERS - vowels;
printf("\nNumber of vowels : %d", vowels);
printf("\nNumber of constants : %d\n", constants);
free(string);
}
int check_vowels(char *p_string) {
int i = 0;
int count = 0;
while (1) {
if(*(p_string + i) == 65 || *(p_string + i) == 69 || *(p_string + i) == 73 || *(p_string + i) == 79 || *(p_string + i) == 85)
count++;
if(*(p_string + i) == 97 || *(p_string + i) == 101 || *(p_string + i) == 105 || *(p_string + i) == 111 || *(p_string + i) == 117)
count ++;
if(*(p_string + i) == '\0')
break;
i++;
}
return count;
}
Output Example - Error :
Enter a string: apples
Segmentation fault (core dumped)
There are multiple problems in your code:
gets(string) attempts to reads input into an invalid address as string is an uninitialized pointer. You should pass an array.
gets is an obsolete function that cannot be used safely. You should use fgets() instead.
the number of consonants is not (26 - number_of_vowels). You should count the number of letters and subtract the number of vowels.
it is much more readable and portable to use character constants such as 'A' than actual ASCII codes like 65.
Here is a modified version:
#include <stdio.h>
int count_vowels(const char *p_string);
int count_letters(const char *p_string);
int main(void) {
char string[100];
int vowels;
int constants;
printf("Enter a string: ");
if (fgets(string, sizeof string, stdin) == NULL) {
printf("No input\n");
return 1;
}
vowels = count_vowels(string);
constants = count_letters(string) - vowels;
printf("Number of vowels: %d\n", vowels);
printf("Number of constants: %d\n", constants);
return 0;
}
int count_vowels(const char *p) {
int count = 0;
for (int i = 0; p[i] != '\0'; i++) {
if (p[i] == 'A' || p[i] == 'E' || p[i] == 'I' || p[i] == 'O' || p[i] == 'U')
count++;
if (p[i] == 'a' || p[i] == 'e' || p[i] == 'i' || p[i] == 'o' || p[i] == 'u')
count++;
}
return count;
}
int count_letters(const char *p) {
int count = 0;
for (int i = 0; p[i] != '\0'; i++) {
/* assuming A-Z and a-z are contiguous, as is the case in ASCII */
if ((p[i] >= 'A' && p[i] <= 'Z') || (p[i] >= 'a' && p[i] <= 'z'))
count++;
}
return count;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have an array formed from a text file imported by stdin.
The text file looks like this:
"Name"
"Number"
"Name"
"Number"
...
The entire code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char** argv)
{
//number of arguments
if (argc > 2)
{
fprintf(stderr, "Too many arguments\n");
return 1;
}
//check argument 1
{
if (argc == 2)
{
unsigned i = 0;
while (i < strlen(argv[1]))
{
if ((isdigit(argv[1][i])) == 0)
{
fprintf(stderr, "Enter a number\n");
return 1;
}
i++;
}
}
else
{
fprintf(stderr, "argument\n");
return -1;
}
}
//find \n and separate
int g = 0;
int c = 0;
char buffer[102];
char people[42][102];
char numbers[42][102];
while (fgets(buffer, sizeof buffer, stdin) != NULL)
{
if (g % 2 == 0)
{
strcpy(people[c], buffer);
//printf("%s", people[c]);
}
if (g % 2 == 1)
{
strcpy(numbers[c], buffer);
c++;
}
g++;
}
//convert and remove \n
char conv_people[42][102];
for (int i = 0; i < c; i++)
{
for (unsigned j = 0; j < strlen(people[i]); j++)
{
if (islower(people[i][j]) == 0 && people[i][j] != ' ' && people[i][j] != '.')
{
if (people[i][j] == '\n')
{
conv_people[i][j] = '\0';
}
people[i][j] = conv_people[i][j] + 32;
}
}
}
//covert to numbers
char conv[42][102];
for (int i = 0; i < c; i++)
{
for (unsigned j = 0; j < strlen(people[i]); j++)
{
if (conv_people[i][j] == ' ' || conv_people[i][i] == '.' || conv_people[i][i] == '\n' || conv_people[i][i] == '\0')
{
conv[i][j] = '0';
}
if (conv_people[i][j] == 'a' || conv_people[i][j] == 'b' || conv_people[i][j] == 'c')
{
conv[i][j] = '2';
}
if (conv_people[i][j] == 'd' || conv_people[i][j] == 'e' || conv_people[i][j] == 'f')
{
conv[i][j] = '3';
}
if (conv_people[i][j] == 'g' || conv_people[i][j] == 'h' || conv_people[i][j] == 'i')
{
conv[i][j] = '4';
}
if (conv_people[i][j] == 'j' || conv_people[i][j] == 'k' || conv_people[i][j] == 'l')
{
conv[i][j] = '5';
}
if (conv_people[i][j] == 'm' || conv_people[i][j] == 'n' || conv_people[i][j] == 'o')
{
conv[i][j] = '6';
}
if (conv_people[i][j] == 'p' || conv_people[i][j] == 'q' || conv_people[i][j] == 'r' || conv_people[i][j] == 's')
{
conv[i][j] = '7';
}
if (conv_people[i][j] == 't' || conv_people[i][j] == 'u' || conv_people[i][j] == 'v')
{
conv[i][j] = '8';
}
if (conv_people[i][j] == 'w' || conv_people[i][j] == 'x' || conv_people[i][j] == 'y' || conv_people[i][j] == 'z')
{
conv[i][j] = '9';
}
}
}
//compare
int i = 0;
while (i < c)
{
if (strstr(conv[i], argv[1]) != NULL)
printf("%s, %s", people[i], numbers[i]);
if (strstr(numbers[i], argv[1]) != NULL)
printf("%s, %s", people[i], numbers[i]);
i++;
}
return 0;
}
The program takes a list of people and their phone numbers and searches it using argv[1]
The output always omits the first capital letter in each word
So if the file contains a name like: Barrack Obama
the program returns arrack bama
The numbers and converted names are working fine
I didn't want to post the whole thing because it's extremely ugly.
I've run the code and John is output as Éohn. It likely comes from
people[i][j] = conv_people[i][j] + 32;
because you never set any values in conv_people[i] except a terminator.
If I add this first line in the loop
strcpy(conv_people[i], people[i]);
then is outputs
john
with a lower case initial letter.
Aside: it is safer and convenient to use
people[i][j] = tolower(conv_people[i][j]);
which doesn't even need to be tested to see if an uppercase letter was passed.
I'm trying to pass back an updated char array to the original main() function, however when i try to copy over the array with a pointer before it it gives me "Char " differs in levels of indirection from char() [15]'
This is a ISBN validator program.
Here it is:
int main(void)
{
int divisible;
char choice, trash;
char code[15];
do
{
printf("Enter an ISBN (book) number:\n");
fgets(code, 15, stdin);
printf("The ISBN entered is:%s\n", code);
divisible = testISBN(&code);
if (divisible == 1)
printf("\n\n\tValid ISBN Number\n");
else
printf("\n\n\tInvalid ISBN Number\n");
printf("\nDo you wish to continue? (Y/N)\n");
choice = getchar();
trash = getchar();
} while (toupper(choice) != 'N');
return 0;
}
int testISBN(char *code)
{
int i;
int sum = 0;
int weight = 10;
char codefinal[10];
int x = 0;
for (i = 0; i<15; i++)
{
int chVal;
if ((i == 9) && (toupper(code[i]) == 'X'))
{
printf("%c",code[i]);
chVal = 10;
}
else
{
chVal = code[i] - '0';
}
if (chVal == 0 || chVal == 1 || chVal == 2 || chVal == 3 || chVal == 4 || chVal == 5 || chVal == 6 || chVal == 7 || chVal == 8 || chVal == 9) {
char y = (char)chVal;
codefinal[x] = y;
x++;
}
sum += chVal * weight;
weight--;
}
printf("sum is %d", sum);
for (i = 0; i < 15; i++) {
*code[i] = codefinal[i];
printf("%c", code);
}
return (sum % 11) == 0;
}
At the very bottom where i said *code[i] = codefinal[i] is where i get the issue. I'm just trying to pass back by pointer my new updated array of numbers to my main.
Do following changes in your code:
Change prototype of function int testISBN(char* code) to int testISBN(char code[])
Change calling from testISBN(&code); to testISBN(code);
Change line *code[i] = codefinal[i]; to code[i] = codefinal[i];
In line printf("%c", code); you are printing not the value but the address of code[]. So change it to printf("%c", code[i]);. Note that code[] contains some non printable characters also.
Suggestion: You can change line if (chVal == 0 || chVal == 1 || chVal == 2 || chVal == 3 || chVal == 4 || chVal == 5 || chVal == 6 || chVal == 7 || chVal == 8 || chVal == 9) to if((chVal >= 0) && (chVal <= 9)) for simplicity.
Just pass code as is it will decay into pointer.
char code[15];
divisible = testISBN(code);
You may want to change testISBN signature to
int testISBN(char code[15]);
This way you have a higher chances to get compiler warning if you mess up indices.
Simple example:
#include <stdio.h>
void func(char arr[3]) {
arr[0] = 'H';
arr[1] = 'i';
arr[2] = 0;
}
int main() {
char arr[3] = {0};
func(arr);
printf("%s", arr);
}
Prints "Hi"
As commenters mentioned your code has other issues such as memory out of bounds access. To ease your life spend some time with your compiler's manual and learn how to turn on all the warnings, they indicate code issues most of the time.
I am coding a shell. When I execute it like this cat /dev/urandom | valgrind ./myshell to run some test and see if I don't have any segfault or other errors, valgrind sometimes tell me that I have an Invalid Write in function my_wordcpy at this line tab[++j] = str[*i];
It doesn't happen every time, but it does happen, and I just can't see why. Here is my code :
static int count_words(char *str, char *sep)
{
int quote;
int words;
int i;
i = -1;
if (count_quotes(str) == -1)
return (0);
words = 0;
quote = 0;
while (str[++i] != '\0')
{
if (str[i] == '"')
{
if (quote == 0)
quote = 1;
else
quote = 0;
}
if (quote == 0
&& (is_cinside(sep, str[i]) == 0 && str[i] != '\t' &&
(is_cinside(sep, str[i + 1]) == 1 ||
str[i + 1] == '\t' || str[i + 1] == '\0')))
++words;
}
return (words);
}
static int my_wordlen(char *str, int *i, char *sep)
{
int quote;
int j;
j = 0;
quote = 0;
while (str[++(*i)] != '\0')
if (str[*i] == '"' && quote == 0)
quote = 1;
else if (quote == 1 || (quote == 0 && is_cinside(sep, str[*i]) == 0 &&
str[*i] != '\t'))
{
++j;
if ((quote == 1 && str[*i + 1] == '"') ||
(quote == 0 && (is_cinside(sep, str[*i + 1]) == 1 ||
str[*i + 1] == '\t' ||
str[*i + 1] == '\0')))
{
if (quote == 1 && str[*i + 1] == '"')
++(*i);
return (j);
}
}
return (-1);
}
static char *my_wordcpy(char *tab, char *str, int *i, char *sep)
{
int quote;
int j;
j = -1;
quote = 0;
while (str[++(*i)] != '\0')
if (str[*i] == '"' && quote == 0)
quote = 1;
else if (quote == 1 || (quote == 0 &&
is_cinside(sep, str[*i]) == 0 && str[*i] != '\t'))
{
tab[++j] = str[*i]; /* here is the invalid write. */
if ((quote == 1 && str[*i + 1] == '"') ||
(quote == 0 && (is_cinside(sep, str[*i + 1]) == 1 ||
str[*i + 1] == '\t' || str[*i + 1] == '\0')))
{
if (quote == 1 && str[*i + 1] == '"')
++(*i);
tab[++j] = '\0';
return (tab);
}
}
return (NULL);
}
char **my_quotetowordtab(char *str, char *sep)
{
char **tab;
int words;
int i;
int j;
int k;
i = -1;
j = -1;
k = -1;
if (str == NULL)
return (NULL);
words = count_words(str, sep);
if ((tab = malloc(sizeof(char *) * (words + 1))) == NULL)
return (NULL);
while (++i < words)
{
if ((tab[i] = malloc(sizeof(char) * (my_wordlen(str, &j, sep) + 1)))
== NULL)
return (NULL);
tab[i] = my_wordcpy(tab[i], str, &k, sep);
}
tab[i] = NULL;
return (tab);
}
my_wordlen can return -1 and you don't check this before giving it to malloc. In this case 0 bytes are allocated hence in my_wordcopy a heap-buffer-overflow occurs.
What happens if you have a str with only a single or odd number of " quote characters? Seems like your code won't check for \0 in that case and therefore it could write passed the end of tab. I think you need to move your NUL character check outside of the 2nd if clause to catch both cases.