Question

#include <stdio.h> #include <stdlib.h> #include <string.h> #include<ctype.h> #define MAX_LEN 255 int numWords(char *str); int numDigit(char *str);...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<ctype.h>

#define MAX_LEN 255

int numWords(char *str);
int numDigit(char *str);
int numUppltr(char *str);
int numLwrltr(char *str);
int punChar(char *str);
char*readString(char *str);

int main()
{
char givString[MAX_LEN];

puts("Enter your string(Max 255 characters):");
//readString(givString);
gets(givString);

printf("\nnumber of words :%d",numWords(givString));
printf("\nnumber of uppercase letters %d",numUppltr(givString));
printf("\nnumber of lowercase letters %d",numLwrltr(givString));

printf("\nnumber of punctuations %d\n",punChar(givString));

printf("\nnumber of digits:%d\n",numDigit(givString));
system("pause");
return 0;
}

char *readString(char *str)
{
int ch, i=0;
while((ch=getchar())!=EOF && ch!='\n')
{
if(i)
{
str[i]=ch;
i++;

}
str[i] = '\0';
}

return str;

}
int numUppltr(char *str)
{
int Upltr=0;
while(*str)
{
if(*str>='A' && *str<='Z')
{
Upltr++;

}
str++;
}
return Upltr;
}
int numLwrltr(char *str)
{
int lwrltr=0;
while(*str)
{
if(*str>='a' && *str<='z')
{
lwrltr++;
}
str++;
}
return lwrltr;
}
int punChar(char*str)
{
int count=0,i=0;
while(str[i])
{
if(ispunct(str[i]))
count++;
i++;

}
return count;

}
int numWords(char*str)
{
int wordcount=1,i=0;
while(str[i])
{
if(str[i]==' ')
++wordcount;
i++;


}
return wordcount;
}
int numDigit(char *str)
{
int digitcount=0;
while(*str)
{
if(*str>='0' && *str<='9')
{
digitcount++;
}
str++;

}
return digitcount;
}

Please I want to modify the above program Instead of reading data from the keyboard, get the data from a file. Ask the user for the filename.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The updated source code is given below:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<ctype.h>

#define MAX_LEN 255

int numWords(char *str);
int numDigit(char *str);
int numUppltr(char *str);
int numLwrltr(char *str);
int punChar(char *str);
char*readString(char *str);

int main()
{
char givString[MAX_LEN];
  
FILE *fptr;
char fileName[100];
  
puts("Enter the file name: ");
gets(fileName);
//open file
fptr=fopen(fileName,"r");
  
int i = 0;
char ch=fgetc(fptr);
while(ch!=EOF)
{
givString[i++]=ch;
ch=fgetc(fptr);
}
  
printf("\nnumber of words :%d",numWords(givString));
printf("\nnumber of uppercase letters %d",numUppltr(givString));
printf("\nnumber of lowercase letters %d",numLwrltr(givString));
  
printf("\nnumber of punctuations %d\n",punChar(givString));
  
printf("\nnumber of digits:%d\n",numDigit(givString));
system("pause");
return 0;
}

char *readString(char *str)
{
int ch, i=0;
while((ch=getchar())!=EOF && ch!='\n')
{
if(i)
{
str[i]=ch;
i++;

}
str[i] = '\0';
}

return str;

}
int numUppltr(char *str)
{
int Upltr=0;
while(*str)
{
if(*str>='A' && *str<='Z')
{
Upltr++;

}
str++;
}
return Upltr;
}
int numLwrltr(char *str)
{
int lwrltr=0;
while(*str)
{
if(*str>='a' && *str<='z')
{
lwrltr++;
}
str++;
}
return lwrltr;
}
int punChar(char*str)
{
int count=0,i=0;
while(str[i])
{
if(ispunct(str[i]))
count++;
i++;

}
return count;

}
int numWords(char*str)
{
int wordcount=1,i=0;
while(str[i])
{
if(str[i]==' ')
++wordcount;
i++;


}
return wordcount;
}
int numDigit(char *str)
{
int digitcount=0;
while(*str)
{
if(*str>='0' && *str<='9')
{
digitcount++;
}
str++;

}
return digitcount;
}

INPUT

input.txt

hello

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include<ctype.h> #define MAX_LEN 255 int numWords(char *str); int numDigit(char *str);...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here....

    #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } }    return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...

  • You have to clearly define what each if statement is doing in both programs. #include <stdio.h>...

    You have to clearly define what each if statement is doing in both programs. #include <stdio.h> static unsigned char count = 31; void callMe ( char ch ) { if ( ch == 'U' || ch == 'u') { if ( count > 15 ) count -= 16; printf ( "count = %d \n", count ) ; return; } if ( ch == 'A' || ch == 'a') { if ( count % 2 == 1) count -= 1; printf...

  • Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[],...

    Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[], int *p_top, char value); char pop(char S[], int *p_top); void printCurrentStack(char S[], int *p_top); int validation(char infix[], char S[], int *p_top); char *infix2postfix(char infix[], char postfix[], char S[], int *p_top); int precedence(char symbol); int main() { // int choice; int top1=0; //top for S1 stack int top2=0; //top for S2 stack int *p_top1=&top1; int *p_top2=&top2; char infix[]="(2+3)*(4-3)"; //Stores infix string int n=strlen(infix); //length of...

  • #include <errno.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> /* * Expected usage:...

    #include <errno.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> /* * Expected usage: * ./wc <words | lines> <file> * * If argv[1] is "words", then you should count the number of words. If it * is "lines", then you should count the number of lines. * * For example: * $ cat a.txt * a b c d * $ ./wc words a.txt * 4 * $ ./wc lines a.txt * 1 * * YOUR PROGRAM...

  • I'm having trouble getting my program to output this statement Enter a sentence: Test! There are...

    I'm having trouble getting my program to output this statement Enter a sentence: Test! There are 5 total characters. There are 1 vowel. There are 1 UPPERCASE letters. There are 3 lowercase letters. There are 1 other characters. Here's my code: #include<string.h> #include<stdio.h> #include<stdbool.h> int main() { char str[100]; int i; int vowels=0; int UC; int LC; int Others; int c; printf("Enter a sentence: \n"); gets(s); LC=countLC(&s); UC=countUC(&s); Others=countOthers(&s); printf("There are %d total characters.\n", ; for(i=0; i<strlen(str); i++){ if(isVowel(str[i])) vowels++;...

  • #include <stdio.h> #include<string.h> int main() { char strText[100] ="Start"; char i; int nTextASCIISum = strText[0] +...

    #include <stdio.h> #include<string.h> int main() { char strText[100] ="Start"; char i; int nTextASCIISum = strText[0] + strText[1] + strText[2]; int nTextLen = strlen(strText); int count = 0; printf("Welcome to token generator!\n"); printf("Enter a word to use in the token generator.\n You may enter as many words as you like. \n Press q and key when finished.\n"); scanf("%c", &strText[i]); //compute when to stop loop //check nTextLen == 1 and strText[0] == 'q' for(i=0;i< nTextLen;i++) if ((strlen(strText) != 1) || (strText[0] !=...

  • What are the description of approach or technique used in this? #include <stdio.h> long charCount=0; long...

    What are the description of approach or technique used in this? #include <stdio.h> long charCount=0; long digitCount=0; long lineCount=0; long wordCount=0; long digitFreq[10]; int c=0; int outOfWord=0; int state=0; int inWord=1; void printStats(); void totalWords(); void totalLines(); void digitFrequency(); int main(int argc,char **argv) { int i; for(i=0;i<10;i++) { digitFreq[i]=0; } state=outOfWord; c=getchar(); while((c !=EOF)) { charCount++; digitFrequency(c); totalLines(c); totalWords(c); c=getchar(); } printStats(); return 1; } void totalLines(int c) { if(c == '\n') { lineCount++; } } void digitFrequency(int c) {...

  • #include <stdio.h> #include <stdlib.h> #include <math.h> int count,i, tracker = 0; int main() { scanf("%d\n", &count);...

    #include <stdio.h> #include <stdlib.h> #include <math.h> int count,i, tracker = 0; int main() { scanf("%d\n", &count); int numbers[count]; //GEtting the numbers and satisfy BS conditions for (i = 0; i < count; i++) { if (scanf("%d", &numbers[i]) != EOF) { tracker++; } if (numbers[i] < 0 || numbers[i] > 99) { printf("%d is not in the [0, 99] range.\n",numbers[i]); exit(i); } } if( tracker != count) { printf("%d numbers are required, but only %d were provided.\n", count, tracker); exit(1); }...

  • what is the output of the following program? #include<stdio.h> #include<string.h> int main(void){ char word[20]; int i...

    what is the output of the following program? #include<stdio.h> #include<string.h> int main(void){ char word[20]; int i =0    strcpy(word, "ORGANISE"); while(word[i] !='\0'){ if(i%2 ==1) word[i] = 'C'; i++; } printf("%s",word); return 0; }

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT