#include <stdio.h>
#define SIZE 30
int main()
{
char string1[SIZE];
printf("%s","Enter a string less than 29 characters");
scanf("%s",string1);
int countword=0;
int countvowel=0;
int countspace=0;
int numberofcons;
for(size_t i=0;i<SIZE;++i){
if(string1[i]==' '){
countspace++;
countword++;
printf("h");
}
if(string1[i]=='a'||string1[i]=='A'||string1[i]=='e'||string1[i]=='E'||string1[i]=='i'||string1[i]=='I'||string1[i]=='o'||string1[i]=='O'||string1[i]=='u'||string1[i]=='U'){
countvowel=countvowel+1;
printf("h");
}
}
numberofcons=SIZE-countvowel-countspace;
printf("Your sentence include\n");
printf("Number of words:");
printf("%d\n",countword);
printf("Number of spaces:");
printf("%d\n",countspace);
printf("Number of vowels:");
printf("%d\n",countvowel);
printf("Number of consonants and special characters:");
printf("%d\n",numberofcons);
printf("%s",string1);
}
so this is my program but somehow it does not run properly. the result shows every count i write is still 0 and I found the input string1 only reads til the first space how can I fix it
Related
#include <stdio.h>
#include <string.h>
int countLetters(char *string1, char letter){
int count=0;
for(int i=0; i<strlen(string1); i++){
if(string1[i]=letter){
count++;
}
}
return count;
}
int main(){
char string1[200];
char letter;
int count;
printf("\nEnter the string: \n");
fgets(string1, 200, stdin);
printf("\nEnter character to be searched: \n");
scanf("%c", &letter);
count = countLetters(string1, letter);
printf("\nThe number of occurrences: %d", count);
}
I was expecting for the function to output the number of times each letter of the array was equal to the char(letter) inputted by the user, but it is just giving me the length of the string.
Change the line:
if(string1[i]=letter){
to
if(string1[i]==letter){
Note, that the string1[i]=letter was overwriting data in string1[i].
You have to use equal equal to operator instead of assignment operator in if condition like this
if(string1==latter)
in your if condition if(string1=latter) value of latter variable is assign to string1[i]
Sample input: aaxdxddd
Output: even
Sample input : aada
Output: odd
Sample input: aadssssdddd
Output: both
#include <stdio.h>
#include <string.h>
int main(){
char s[1000];
int i,j,k,count=0,n;
printf("Enter the string: ");
gets(s);
int len = strlen(s);
for(i=0;i<len;i++){
count=1;
if(s[i]){
for(j=i+1;j<len;j++){
if(s[i]==s[j]){
count++;
s[j]='\0';
}
}
printf(" '%c' = %d \n",s[i],count);
if (count%2==0){
printf("even\n");
//break;
}
else if (count%2==1){
printf("odd\n");
//break;
}
}
}
return 0;
}
My problem to my code is that it determines each character if it occurred an even or odd number of times. What I want to happen is that it determines if all unique characters occurred an even, odd, or both even and odd number of times. Also I didn't write the entire code, I found a similar code and tried to revise it.
I Want to write C program to print words length and their frequency by letters number with basic loops techniques. I could get the word length work but I stuck with frequency
(example: Do 2 not 3 judge 5 a 1 book 4 (had solved this))
there are # words with 1 letter
there are # words with 2 letter
etc...
#include <stdio.h>
int main(void) {
char word[30];
int i = 0,b=0,c=0,j=0,d=0;
printf("Please enter a word: ");
for (i = 0; i < 30 ; i++){
scanf("%s", word);
while (word[b]!='\0'){
b++;
}
printf("%s %d ", word, b);
b = 0;
}
return 0;
}
Your question wasn't completely clear. But from what I understood, you also wanted to print the number of times(frequency) a word of length 'l' is inputted by the user. So I will answer that :
You could just store the length of the word in an array that the user inputs. Once all the inputs are read, you can just print the frequency of each word length from the stored array
Refer the following code to understand what I meant :
#include <stdio.h>
int main(void) {
char word[30];
int i = 0,b=0,c=0,j=0,d=0;
int word_length_freq[30]={0}; //an array which will store the frequency of word length(all initialized to 0)
//eg. if word is "hello" it will increase count of word_length_freq[5] by 1
printf("Please enter a word: ");
for (i = 0; i < 3 ; i++){
scanf("%s", word);
while (word[b]!='\0'){
b++;
}
word_length_freq[b]++;
printf("%s %d ", word, b);
b = 0;
}
for(int i=1;i<30;i++){ //This will print the frequency of all words from length 1 to 30
printf("There are %d words of length %d\n",word_length_freq[i],i);
}
return 0;
}
I hope this solves your question !
Given task is:
Enter 10 characters. For each character entered the corresponding function prints whether it is a digit 0-9 or not.(Also I use older compiler if anyone concerns about my "gets()" and goal is to do this without pointers.)
So far I tried something like this, but for some reason it does not work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
char character[10][1];
char zero ='0';
char nine ='9';
int i;
printf("Enter 10 characters:\n");
for(i=0;i<10;i++){
gets(character[i]);
}
for(i=0;i<10;i++){
if(strcmp(character[i],zero)>=0 && strcmp(character[i],nine)<=0){
printf("%d. character '%c' is a digit.", i, character[i]);
}
else{
printf("%d. character '%c' is not a digit.", i, character[i]);
}
putchar('\n');
}
return 0;
}
Also I tried this, but it can not output correctly characters:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
char character[10][1], pom[10][1];
int num_character[10];
int i;
printf("Enter 10 characters:\n");
for(i=0;i<10;i++){
gets(character[i]);
strcpy(pom[i],character[i]);
num_character[i]=atoi(character[i]);
}
for(i=0;i<10;i++){
if(num_character[i]!=0){
printf("Character '%c' is digit.", pom[i]);
}
else{
printf("Character '%c' is not digit.", pom[i]);
}
putchar('\n');
}
return 0;
}
isdigit() also does not work after I include ctype header.
You are doing several things wrong here, gets is never recommended and fgets will put new line character in the end.
The syntax for strcmp is:
int strcmp(const char *s1, const char *s2);
You need two strings as input in strcmp but you are using a string and a character.
Here, it is better to use a character array than a 2D array.
#include <stdio.h>
int main(void)
{
char character[10]; //a simple character array
char zero ='0';
char nine ='9';
int i;
printf("Enter 10 characters:\n");
for(i=0;i<10;i++){
scanf(" %c", &character[i]); //scan characters
}
for(i=0;i<10;i++)
{
if(character[i] >= zero && character[i] <= nine) //print
printf("%d. %c is a digit \n", i+1, character[i]);
else
printf("%d. %c is not a digit \n", i+1, character[i]);
}
}
This is my code in C that reads data from input:
#include<string.h>
#define MAX 3
char a[MAX];
char b[MAX];
void ReadFirstNumber();
void ReadSecondNumber();
int lb,la=0;
void main()
{
ReadFirstNumber();
ReadSecondNumber();
printf("\n First Number > %d %d %d \n",a[0],a[1],a[2]);
printf(" Second Number > %d %d %d \n",b[0],b[1],b[2]);
}
void ReadFirstNumber()
{
int i=0;
printf("Enter the first number:");
scanf("%s", a);
la=strlen(a)-1;
for(i=0;i<=la;i++)
{
a[i] = a[i] -48;
}
}
void ReadSecondNumber()
{
int j=0;
printf("Enter the Second number:");
scanf("%s", b);
lb=strlen(b)-1;
for(j=0;j<=lb;j++)
{
b[j] = b[j] -48;
}
}
input first number example: 123
input second number example: 456 or any 3-digit number
//output
First Number **0**23
Second Number 456
The output for first number is 023
The first character is Zero! but the output for second number is ok.
When I comment out second function //ReadSecondNumber(); it worked perfectly!
You failed to allow enough space for the null terminator char that scanf("%s",...) writes at the end of 'strings'. Increase the value of the MAX #define. You may as well put in something sanely larger, eg 32.