#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main()
{
int upper=0;
int digit=0;
int i=0;
char s[30];
char c;
printf("Enter sentence: ");
fgets(s, 30, stdin);
//s[strlen(s) - 1] = '\0';
while(c=getchar() && c!='\n')
{
c = s[i];
if(isupper(c))
{
upper++;
}
if(isdigit(c))
{
digit++;
}
i++;
}
printf("Number of upper case letters............... %d", upper);
printf("\n");
printf("Number of digits........................... %d", digit);
printf("\n");
printf("Program done. ");
return 0;
system("PAUSE");
}
How can I remove the multiple newlines after the fgets() ?
I have tried implementing the following line after fgets()
s[strlen(s) - 1] = '\0';
but this does not work and my program does not run through all the code.
Without the code -----> s[strlen(s) - 1] = '\0';
Here is the Output:
Enter sentence: Whats UP 1234
Number of upper case letters............... 3
Number of digits........................... 4
Program done.
Process returned 0 (0x0) execution time : 9.340 s
Press any key to continue.
As you can see my program is able to run however I have to press enter multiple times and there is a lot of newlines
and then at the very end the program runs the last bit of code.
The program is suppose to count the number of upper case letters and digits in the string
entered. Can someone please explain why this is happening ?
Thanks to Jongware:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main()
{
int upper=0;
int digit=0;
int i=0;
char s[30];
char c;
printf("Enter sentence: ");
fgets(s, 30, stdin);
//correction: getchar() has been removed from the while condition.
while( c!='\n')
{
c = s[i];
if(isupper(c))
{
upper++;
}
if(isdigit(c))
{
digit++;
}
i++;
}
printf("Number of upper case letters............... %d", upper);
printf("\n");
printf("Number of digits........................... %d", digit);
printf("\n");
return 0;
system("PAUSE");
}
Related
I'm in the middle of homework and I need some help here.
So this is the code
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
int upper=0,lower=0,number = 0;
char ch[500];
printf("Enter the String:\n");
gets(ch);
i=0;
while(ch[i]!=0)
{
if(ch[i]>='A' && ch[i]<='Z')
{
upper++;
}
else if(ch[i]>='a' && ch[i]<='z')
{
lower++;
}
else if(ch[i]>='0' && ch[i]<='9')
{
number++;
}
i++;
}
printf("lowercase letters: %d",lower);
printf("\nuppercase letters: %d",upper);
printf("\nnumber letters: %d",number);
getch();
return 0;
}
As you can see here. When you give a string input. The code will give a total number of uppercase,lowercase and number
for example: If I'm giving "Hello World 123" To the code. The result will be 2 Uppercases 8 Lowercases and 3 Numbers
The problem is the task want me to print all 3 types of letter seperately
for example: from "Hello World 123" Should print "HW" , "elloorld" and "123"
I know that I have to create another 3 arrays for seperate the letter. I tried to create like upperc[i],lowerc[i] and num[i] to input the letter in each of if command but It doesn't work.
So how can I do that?
Here's one way of going about it. It doesn't require 3 separate arrays like you thought because it prints the characters directly:
#include <stdio.h>
#include <ctype.h>
int main(void)
{
printf("Enter a string: ");
char str[1024] = {0};
fgets(str, sizeof str, stdin);
// Use different functions in each iteration
int (*ctype_fns[])(int) = {isupper, islower, isdigit};
for (unsigned i = 0; i < sizeof ctype_fns / sizeof *ctype_fns; ++i)
{
for (const char *it = str; *it; ++it)
{
// If char matches requirements, print it
if (ctype_fns[i]((unsigned char)*it))
putchar(*it);
}
putchar('\n');
}
}
Example of running:
Enter a string: Hello World 123
HW
elloorld
123
For your simple homework project you must assume some decent values. We assume the input is no longer than 128 characters and each of the three types is no more than 64. In real life projects we must check on these values.
Adapting your main, the following would work:
#include <stdio.h>
#include <stdlib.h>
#define MAX_IN 128
#define MAX_X 64
int main()
{
int i;
int upper=0,lower=0,number = 0;
char ch[MAX_IN], chUpper[MAX_X], chLower[MAX_X], chNumber[MAX_X];
printf("Enter the String:\n");
gets(ch);
i=0;
while(ch[i]!=0)
{
if(ch[i]>='A' && ch[i]<='Z')
{
chUpper[upper++]= ch[i];
}
else if(ch[i]>='a' && ch[i]<='z')
{
chLower[lower++]= ch[i];
}
else if(ch[i]>='0' && ch[i]<='9')
{
chNumber[number++]= i;
}
i++;
}
chLower[lower]= '\0';
chUpper[upper]= '\0';
chNumer[number]= '\0';
printf("lowercase letters: %d: %s\n",lower, chLower);
printf("uppercase letters: %d: %s\n",upper, chUpper);
printf("number letters: %d: %s\n",number, chNumber);
getch();
return 0;
}
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_IN 128
#define MAX_X 64
int main()
{
int i;
int upper=0,lower=0,number = 0;
char ch[MAX_IN], chUpper[MAX_X], chLower[MAX_X], chNumber[MAX_X];
printf("Enter the String:\n");
gets(ch);
printf("input given is : %s\n",ch);
for (i=0;ch[i]!='\0';i++)
{
if(ch[i]>='A' && ch[i]<='Z')
{
chUpper[upper++]= ch[i];
}
else if(ch[i]>='a' && ch[i]<='z')
{
chLower[lower++]= ch[i];
}
else if(ch[i]>='0' && ch[i]<='9')
{
chNumber[number++]= ch[i];
}
if ((ch[i+1]) == ' '){
i += 1;
}
}
printf("lowercase letters: %d: %s\n",lower, chLower);
printf("uppercase letters: %d: %s\n",upper, chUpper);
printf("number letters: %d: %s\n",number, chNumber);
return 0;
}
It prints the characters in reverse order just fine except when the string is 8 characters long.
Eg -
"what man" gives "am tahw" Why ?
whereas "what many" gives "ynam tahw" just as it should.
#include <stdio.h>
int main(void)
{
char a[100];
char x;
char*i = a;
printf("Enter a message:");
while ((x = getchar()) != '\n')
{
*i = x;
i++;
}
while (i >= &a[0])
{
printf("%c", *i--);
}
printf ("\n");
}
Modification in your code:
Change printf("%c", *i--); to printf("%c", *--i); in while loop.
You can improve the quality of your program as shown below:
#include <stdio.h>
#include <conio.h>
void main()
{
char *s;
int len,i;
clrscr();
printf("\nENTER A STRING: ");
gets(s);
len=strlen(s);
printf("\nTHE REVERSE OF THE STRING IS:");
for(i=len;i>=0;i--)
printf("%c",*(s+i));
getch();
}
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
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;
}
What I'm trying to do is have the user input a hex number this number will then be converted to a char and displayed to the monitor this will continue until an EOF is encountered.I have the opposite of this code done which converts a char to a hex number. The problem I'm running into is how do i get a hex number from the user I used getchar() for the char2hex program. Is there any similar function for hex numbers?
this is the code for the char2hex program
#include <stdio.h>
int main(void) {
char myChar;
int counter = 0;
while (EOF != (myChar = getchar())) {
/* don't convert newline into hex */
if (myChar == '\n')
continue;
printf("%02x ", myChar);
if (counter > 18) {
printf("\n");
counter = -1;
}
counter++;
}
system("pause");
return 0;
}
this is what i want to the program to do except it would do this continuously
#include <stdio.h>
int main() {
char myChar;
printf("Enter any hex number: ");
scanf("%x", &myChar);
printf("Equivalent Char is: %c\n", myChar);
system("pause");
return 0;
}
any help would be appreciated
thank you
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void) {
int myChar;
int counter = 0;
char buff[3] = {0};
while (EOF != (myChar = getchar())) {
if(isxdigit(myChar)){
buff[counter++] = myChar;
if(counter == 2){
counter = 0;
myChar = strtol(buff, NULL, 16);
putchar(myChar);
}
}
}
printf("\n");
system("pause");
return 0;
}
Because chars and ints can be used interchangably in C, you can use the following code:
int main(void) {
int myChar;
printf("Enter any hex number: ");
scanf("%x", &myChar);
printf("Equivalent Char is: %c\n", myChar);
system("pause");
return 0;
}
If you want it to loop then just enclose it in the while loop as in your example code.
Edit: You can try out the working code here http://ideone.com/yyvz85