I want to make a program that counts the vowels in a sentence entered by the user.
For that compare a character that capitalizes with the vowel arrangement, but although they do not pulls error, do not type the correct output.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
//Program EJER004
int main(){
char vowels[5] = {'A','E','I','O','U'};
int count;
char letter;
count = 0;
printf("Enter a phrase, ending with a point\n");
do{
letter=getchar();
if (toupper(letter) == vowels[5]) /*attempt ask if is a vowel the letter introduced*/
count++;
}while (letter != '.');
printf("\n");
printf("\n");
printf("The number of vowels in the phrase introduced is% d", count);
getch();
return 0;
}
It think the problem is the comparison toupper(letter) == vowels[5]? vowels[5] is always out of array, but other vowels aren't checked.
You would need to add a loop like:
char upr=toupper(letter);
for(int i=0; i<5; i++)
if(vowels[i]==c)
{ cont++;
break;
}
You should write a small function for this comparison
int checkforVowels(char tobechecked)
{
//this only get vowels in CAPSLOCK tho... so dont forget toupper
char vowels[5] = {'A','E','I','O','U'};
int hasvowel = 0;
for(int i = 0; i < 5; i++)
{
if(tobechecked == vowels[i])
{
hasvowel = 1;
break;
}
}
return hasvowel;
}
so you can have it like that
if(checkforVowels(toupper(letter))
HTH
Related
I haven't been coding for long (just 2 months).
My question has to do with the iteration of the counter in loops. Below is my program
with while:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int cnt=0;
int match;
int position;
int tries=0;
char letter;
int main()
{FILE *Difficult= fopen("/GODlvl.txt", "r");
char singl[19];
if (Difficult==NULL){
return -1;}
for(int i = 0; i < rnd1; i++)
fgets(singl, 21, Difficult);
int DashN1= strlen(singl);
printf("%s", singl);
for (int i=0; i<DashN1-1; i++)
{
singl[i]='_';
singl[DashN1]='\0';
}
for (int i=0; i<DashN1; i++) /**it adds an extra character ..possibly a space..**/
{
printf(" ");
printf(" %c", singl[i]);
}
do{
scanf(" %c", &letter);
if(letter==singl[cnt])
{
match=1;
position=cnt;
printf("match found");
}
if (position==cnt)
{
singl[position]=letter;
printf(" %s", singl);
}
cnt++;
tries++;
}
while(tries!=8);
}
the do loop runs starting from 0, and iterates after every step. The problem with this is with the if conditions; they don't test for any arbitrary element in the char array (singl). How can i edit this code(whether the if conditions or the loop) to run for arbitrary index.
After reading the user input for letter, you can request a random index for use in singl, just by doing:
srand((unsigned)time(NULL));
cnt = rand() % DashN1;
This means that your do loop, should look like:
do{
scanf(" %c", &letter);
srand((unsigned)time(NULL));
cnt = rand() % DashN1;
if(letter==singl[cnt]){
//....
}
}while(/*...*/);
Make sure that you do:
#include<stdlib.h>
in order to access srand and rand
This is what I theorized it should be but it seemed like it doesn't work.
HELP PLEAZEE
int main()
{
char input[50];
int i;
do{
printf("ENTER A CHARACTER:");
scanf("%s",&input);
if(isalpha(input)!=0){
printf("YOU INPUTTED A CHARACTER");
i++;
}else{
printf("INVALID INPUT\n");
}
}while(i!=1);
}
isalpha takes an integer as an argument.
You are giving a char array.
You should loop for the number of characters given in input[], if you want more than one character (hard to tell from this code).
This exits if you give exactly one character but keeps going if you give more than one:
#include <stdio.h>
#include <string.h>
int main()
{
char input[50];
int i = 0, j;
size_t len = 0;
do
{
printf("ENTER A CHARACTER:");
scanf("%s",&input);
len = strlen(input);
for(j = 0; j < len; j++) {
if(isalpha(input[j]))
{
i++;
printf("YOU INPUTTED A CHARACTER %d\n", i);
}
else
{
printf("INVALID INPUT\n");
break;
}
}
} while(i!=1);
}
I am programming a palindrome code to check whether a given string is a palindrome or not. Eg: mom, 1001 ...
MY_CODE:
#include <stdio.h>
#include <stdlib.h>
#include <stdio_ext.h>
int main()
{
int i,n;
char p[999];
char flag;
printf("number of characters in the strings");
scanf("%d",&n);
printf("Enter string: ");
for (i=0;i<n;i++)
{
printf("\n");
__fpurge(stdin);
scanf("%c",&p[i]);
}
for (i=0;i<n;i++)
{
if (p[i]==p[n-1-i])
{
flag=0;
break;
}
else flag=1;
}
if (flag==1)
printf("It's not a palindrome");
if (flag==0)
printf("It's a palindrome.");
return 0;
}
I am trying to do that with the idea that if the last and first characters are matched and so on for the next characters. If they are all matched the string is a palindrome otherwise it is not,as simple as that but my output
shows 123;mom; and every nonsense, a palindrome ("even the word 'nonsense' :D).
Can someone guide me?
P.S.: I am a newbie and learning C. My OS: Ubuntu 15.10.
You are checking if characters are the same and is so, setting flag=0, to say it is a palindrome, but that will happen for the 1st match - if other matches do not occur, the flag is never set to not be a palindrome.
The better way is assume it is a palindrome and then if anything provides otherwise, set it as false.
#include <stdio.h>
#include <string.h>
int main()
{
char p[999];
printf("Enter string: ");
if(fgets(p, sizeof(p), stdin))
{
int palindrome = 1;
int i;
int n;
n = strlen(p);
/* handle new line at end of fgets input */
if(p[n-1] == '\n')
p[n-1] = '\0';
n = strlen(p);
/* 0 length string (after newline removed) - not a palindrome */
if(n == 0) palindrome = 0;
for(i=0;i<n/2;i++)
{
/* look for a chatacter pair that doesn't match - if find, then we don't have a palindrome */
if(p[i] != p[n-1-i])
palindrome = 0;
}
if(palindrome)
printf("is a palindrome\n");
else
printf("is not a palindrome\n");
}
return 0;
}
From your code, I have a couple of things to say as follows.
#include <string.h>
#include <stdio.h>
// #include <stdlib.h> // this is not necessary.
int main()
{
// int i,n;
// char p[999];
// char flag;
// ######################################################
// You literally don't need to input the number of characters since you can get the string's length from `strlen`.
// In addition, different inputs yield different lengths,
// therefore, it is not applicable if the input becomes extremely long, for example.
//
// printf("number of characters in the strings");
// scanf("%d",&n);
// printf("Enter string: ");
// for (i=0;i<n;i++)
// {
// printf("\n");
// __fpurge(stdin);
// scanf("%c",&p[i]);
// }
// ######################################################
char str[100];
printf( "Enter a value :");
gets( str );
int i=0;
int n;
n = strlen(str)-1;
while (i<n)
{
if (str[i++] != str[n--])
{
printf("%s is Not palidrome\n", str);
return 0;
}
}
printf("%s is palidrome\n", str);
// for (i=0;i<n;i++)
// {
// if (p[i]==p[n-1-i])
// {
// flag=0;
// break;
// }
// else flag=1;
// }
// if (flag==1)
// printf("It's not a palindrome");
// if (flag==0)
// printf("It's a palindrome.");
return 0;
}
With a slightly modification as above, that code works well.
Note: The palindrome problem is a classical problem, and you literally can find many solutions from the internet. You can refer the solution code in C at here.
Update: Here is my update with fgets and while-loop.
#include <string.h>
#include <stdio.h>
int main()
{
char str[100];
printf( "Enter a string (char/value) :");
fgets (str, 100, stdin);
int i=0;
int n;
n = strlen(str)-1;
while (i<n)
{
if (str[i++] != str[n--])
{
printf("%s is Not palidrome\n", str);
return 0;
}
}
printf("%s is palidrome\n", str);
return 0;
}
My program in C which is Palindrome has an error in its function. My function is not comparing the 2 characters in my string. When I type a single character it answers palindrome but if it is two or more always not palindrome.
Code:
int IntStrlength=strlen(StrWord);
int IntCtr2=0;
int IntCtr=1, IntAnswer;
while(IntCtr<=(IntStrlength/2)){
printf(" %d %d\n", IntCtr2,IntStrlength);
if(StrWord[IntStrlength] != StrWord[IntCtr2]){
IntAnswer=0;
printf(" %d=Not Palindrome", IntAnswer);
exit (0);
}//if(StrWord[IntCtr2]!=StrWord[IntStrlength]) <---------
else{
IntCtr2++;
IntStrlength--;
}// else <--------
IntCtr++;
}//while(IntCtr<IntStrlength/2) <-----------
IntAnswer=1;
printf(" %d=Palindrome", IntAnswer);
return ;
}
Single character:
Two or more characters:
Why not write it like this
int wordLength = strlen(StrWord);
for (int i=0;i<(wordLength/2);i++) {
if (StrWord[i] != StrWord[wordLength-i-1]) {
return 0;
}
}
return 1;
For words with an even length (say 8) the counter will go from 0 to 3, accessing all letters. For uneven words (say 7) the c ounter will go from 0 to 2, leaving the middle element unchecked. This is not necessary since its a palindrome and it always matches itself
#include<stdio.h>
int check_palindrom(char *);
int main()
{
char s1[20];
printf("Enter the string...\n");
gets(s1);
int x;
x=check_palindrom(s1);
x?printf("Palindrom\n"):printf("Not Palindrom\n");
}
int check_palindrom(char *s)
{
int i,j;
for(i=0;s[i];i++);
for(i=i-1,j=0;i>j;i--,j++)
if(s[i]!=s[j])
return 0;
if(s[i]==s[j])
return 1;
}
Enter the string...
radar
Palindrom
I've seen this algorithm before in a interview book called "Cracking the Coding Interview".
In it the author shows a very simple and easy implementation of the code. The code is below: Also here is a video explaining the code.
#include<stdio.h>
#include<string.h> // strlen()
void isPalindrome(char str[]);
int main(){
isPalindrome("MOM");
isPalindrome("M");
return 0;
}
void isPalindrome(char str[]){
int lm = 0;//left most index
int rm = strlen(str) - 1;//right most index
while(rm > lm){
if(str[lm++] != str[rm--]){
printf("No, %s is NOT a palindrome \n", str);
return;
}
}
printf("Yes, %s is a palindrome because the word reversed is the same \n", str);
}
You can do this like this:
#include <stdio.h>
#include <string.h>
int check_palindrome(char string []);
int main()
{
char string[20];
printf("Enter the string...\n");
scanf ("%s", &string);
int check;
check = check_palindrome (string);
if (check == 0)
printf ("Not Palindrome\n");
else
printf ("Palindrome\n");
return 0;
}
int check_palindrome (char string [])
{
char duplicate [];
strcpy (string, duplicate);
strrev (string);
if (strcmp (string, duplicate) == 0)
return 1;
else
return 0;
}
This uses the strcmp and strrev function.
Take a look at this code, that's how I have implemented it (remember to #include <stdbool.h> or it will not work):
for(i = 0; i < string_length; i++)
{
if(sentence[i] == sentence[string_lenght-1-i])
palindrome = true;
else
{
palindrome = false;
break;
}
}
Doing that it will check if your sentence is palindrome and, at the first occurence this is not true it will break the for loop. You can use something like
if(palindrome)
printf(..);
else
printf(..);
for a simple prompt for the user.
Example :
radar is palindrome
abba is palindrome
abcabc is not palindrome
Please , pay attention to the fact that
Abba
is not recognized as a palindrome due to the fact that ' A ' and 'a' have different ASCII codes :
'A' has the value of 65
'a' has the value of 97
according to the ASCII table. You can find out more here.
You can avoid this issue trasforming all the characters of the string to lower case characters.
You can do this including the <ctype.h> library and calling the function int tolower(int c); like that :
for ( ; *p; ++p) *p = tolower(*p);
or
for(int i = 0; str[i]; i++){
str[i] = tolower(str[i]);
}
Code by Earlz, take a look at this Q&A to look deeper into that.
EDIT : I made a simple program to do this, see if it can help you
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
void LowerCharacters(char *word, int word_lenth);
int main(void){
char *word = (char *) malloc(10);
bool palindrome = false;
if(word == 0)
{
printf("\nERROR : Out of memory.\n\n");
return 1;
}
printf("\nEnter a word to check if it is palindrome or not : ");
scanf("%s", word);
int word_length = strlen(word);
LowerCharacters(word,word_length);
for(int i = 0; i < word_length; i++)
{
if(word[i] == word[word_length-1-i])
palindrome = true;
else
{
palindrome = false;
break;
}
}
palindrome ? printf("\nThe word %s is palindrome.\n\n", word) : printf("\nThe word %s is not palindrome.\n\n", word);
free(word);
return 0;
}
void LowerCharacters(char *word, int word_length){
for(int i = 0; i < word_length; i++)
word[i] = tolower(word[i]);
}
Input :
Enter a word to check if it is palindrome or not : RadaR
Output :
The word radar is palindrome.
This code may help you to understand the concept:
#include<stdio.h>
int main()
{
char str[50];
int i,j,flag=1;
printf("Enter the string");
gets(str);
for(i=0;str[i]!='\0';i++);
for(i=i-1,j=0;j<i;j++,i--)
{
str[i]=str[i]+str[j];
str[j]=str[i]-str[j];
str[i]=str[i]-str[j];
}
for(i=0;str[i]!='\0';i++);
for(i=i-1,j=0;j<i;j++,i--)
{
if(str[i]==str[j]){
flag=0;
break;
}
}if(flag==0)
{
printf("Palindrome");
}else
{
printf("Not Palindrome");
}
}
I have solution for this
char a[]="abbba";
int i,j,b=strlen(a),flag=0;
for(i=0,j=0; i<b; i++,j++)
{
if(a[i]!=a[b-j-1])
{
flag=1;
break;
}
}
if(flag)
{
printf("the string is not palindrum");
}
else
{
printf("the string is palindrum");
}
This may works for you
#include <stdio.h>
#include <stdlib.h>
int main(void) {
setbuf(stdout,NULL);
int i,limit;
char string1[10];
int flag=0;
printf("enter a string");
scanf("%s",string1);
limit=strlen(string1);
for(i=0;i<limit;i++){
if(string1[i]!=string1[limit-i-1]){
flag=1;
break;
}
} if(flag==1){
printf("entered string is not palindrome");
}else{
printf("entered string is palindrome");
}
return EXIT_SUCCESS;
}
I am absolutely brand new at programming and im not sure how to explain what im doing here.
The whole purpose of this piece is to enter values and then print them out in the same order. Now I wanna quit from entering values when pressing 'q' and so I have to scanf for chars but when I assign them back to the int array the values are not the same.
Hope that makes any sense to you but in any case heres my code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5000
define flush fflush(stdin)
main() {
int input[SIZE] = {0},i = 0;
int counter = 0;
char inputs, quit;
do {
system("cls");
printf("Input number ('q' to quit and display numbers entered): ");
flush;
scanf("%c",&inputs);
flush;
if (inputs == 'q')
quit = 'q';
else {
input[i] = inputs;
counter++;
i++;
}
} while (i < SIZE && quit != 'q');
for(i = 0; i < counter; i++){
printf("%i.%i\n", i + 1, input[i]);
}
system("pause");
}
Ive been trying to do this on my own btw and also researched some information online regarding chars but couldnt find anything that would help me. Thanks a lot in advance.
You should nor be getting integer through %c neither assign char values to integers variables when that is not the intention, rather you should approach something like this
i = 0;
do {
printf("Enter a number: ");
scanf("%d", &input[i]);
i++; counter++;
printf("Do you want to continue? (y/n) : ");
scanf("%c", &inputs);
} while(inputs == 'y');
or u can get the number of integer inputs upfront and loop to get that much integers.
try instead (using your original code as much as possible):
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE 5000
int main()
{
int input[SIZE] = {0},i = 0;
int counter = 0;
char inputs[32];
bool quite = false;
do
{
system("cls");
printf("Input number ('q' to quit and display numbers entered): ");
// read a string from user, then convert when appropr
fgets(stdin, sizeof(inputs), inputs);
if (inputs[0] == 'q')
{
quit = true;
}
else if ( isdigit(inputs[0]) )
{
input[i] = atoi(inputs); // this will disregard any ending \n
counter++;
i++;
}
}
while (i < SIZE && !quit);
for(i = 0; i < counter; i++)
{
printf("%i.%i\n", i + 1, input[i]);
}
system("pause");
}
Another variant. This one will read in characters regardless of the use of whitespaces, since it uses getchar() rather than scanf(). I'm not sure if this is what you want. It seems as though you want integers but are reading characters. So this solution may be completely off base.
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5000
int main()
{
char input[SIZE] = {0};
int i = 0;
int counter = 0;
char inputs;
printf("Input number ('q' to quit and display numbers entered): ");
while (((inputs = getchar()) != EOF) && (counter < SIZE))
{
if (inputs == 'q')
break;
input[counter] = inputs;
counter++;
}
for(i = 0; i < counter; i++)
{
printf("%c\n", input[i]);
}
system("pause");
return 0;
}
If you do really want ints, this one should work.
Notice that the atoi() function can be used to convert a C-string to an int.
The fgets() function is used to read the C-string from STDIN. However, scanf("%s", input); would also work here, as opposed to the scanf("%c", &inputs); that you used.
#include <stdio.h>
#include <stdlib.h>
#define INPUT_SIZE 1000
#define SIZE 5000
int main()
{
char input[INPUT_SIZE] = {0};
int numbers[SIZE] = {0};
int i = 0;
int counter = 0;
while ((fgets(input, sizeof(input), stdin) != NULL) && (counter < SIZE))
{
system("cls");
printf("Input number ('q' to quit and display numbers entered): ");
if (input[0] == 'q')
break;
numbers[counter] = atoi(input);
counter++;
}
for(i = 0; i < counter; i++)
{
printf("%i\n", numbers[i]);
}
system("pause");
return 0;
}