Question

getchar() redirect to input textfile in ASCI C programming. Hello, underneath is the code I did...

getchar() redirect to input textfile in ASCI C programming.

Hello, underneath is the code I did and it works. However, my professor is asking to use getchar() and have an input text file to redirect it. How would i do that on my code? Thank you for the help.

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

int main()
{
char string[100];
int c = 0, Lcase[26] = {0}, x;
int Ucase[26] = {0};
  

printf("Enter a string\n");
gets(string);

while (string[c] != '\0')
{
if (string[c] >= 'a' && string[c] <= 'z')
   {
x = string[c] - 'a';
Lcase[x]++;
}
   if( string[c] >= 'A' && string[c] <= 'Z')
   {
       x = string[c] - 'A';
       Ucase[x]++;  
   }
c++;
}

for (c = 0; c < 26; c++)
{
printf("%c occurs %d times in the string.\n", c + 'a', Lcase[c]);
}
for (c = 0; c < 26; c++)
{
       printf("%c occurs %d times in the string.\n", c + 'A', Ucase[c]);
}
return 0;

}

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

Using getchar() function, you can read the data from console(stdin) as follows:

while(((ch=getchar()) != EOF))

{

.................

}

Reading data from file:

while((ch = fgetc(fp)) != EOF)
{

...............

}

Program:

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

int main()
{
FILE *fp;
char ch;
char string[100];
int c = 0, Lcase[26] = {0}, x;
int Ucase[26] = {0};

//Opening file for reading
fp = fopen("ipData.txt", "r");

//Checking file
if(fp == NULL)
{
printf(" File doesn't Exist... ");
return 0;
}


//Reading data from stdin using getchar
//while(((ch=getchar()) != EOF))

//Reading data from file
while((ch = fgetc(fp)) != EOF)
{
if (ch >= 'a' && ch <= 'z')
{
x = ch - 'a';
Lcase[x]++;
}
if( ch >= 'A' && ch <= 'Z')
{
x = ch - 'A';
Ucase[x]++;
}
c++;
}

for (c = 0; c < 26; c++)
{
printf("%c occurs %d times in the string. ", c + 'a', Lcase[c]);
}
for (c = 0; c < 26; c++)
{
printf("%c occurs %d times in the string. ", c + 'A', Ucase[c]);
}

//Closing file
fclose(fp);

return 0;
}

Sample Run:

Add a comment
Know the answer?
Add Answer to:
getchar() redirect to input textfile in ASCI C programming. Hello, underneath is the code I did...
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
  • Hello, I have my piece of C programming code and I have a pointer initialized to...

    Hello, I have my piece of C programming code and I have a pointer initialized to point to my array and I need help to display the contents of my array using a while loop or do-while loop. The stop condition should be when the pointer reaches '\0'. The code is below: #include <stdio.h> int main () {    char array[80]; printf("Enter a string: "); scanf("%[^\n]", &array); printf("%s\n", array); char * p;    p = array;         }

  • Programming C....... function code is clear but compile is wrong .. I input 8 and compiled...

    Programming C....... function code is clear but compile is wrong .. I input 8 and compiled 2 0 1 1 2 3 5 8 13.. look first number 2 is wrong. The correct answer 8 is 0 1 1 2 3 5 8 13 21.. Need fix code to compile correctly.. Here code.c --------------------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <math.h> int fibonacciIterative( int n ) { int fib[1000]; int i; fib[ 0 ] = 0; fib[ 1 ] = 1; for (...

  • coding in C Programming Assign the size of userInput to stringSize. Ex: if userInput = "Hello",...

    coding in C Programming Assign the size of userInput to stringSize. Ex: if userInput = "Hello", output is: Size of userInput: 5 #include < stdio.h > #include < string.h > int main(void) { char userlnput[50] = "": int stringSize = 0: strcpy(userInput, "Hello"): /* Your solution goes here */ printf("Size of userlnput: %d\n", stringSize): return 0: }

  • Write a function that can return the length of a string in C language. Please leave...

    Write a function that can return the length of a string in C language. Please leave comments to explain the code and verify that it runs properly... Example: Input String: China The length of the string is 5. #include <stdio.h> int length(char * p) {     /write your code here } main(){     int len;     char str[20];     printf("Input string:");     scanf("%s", str);     len = length(str);     printf("The length of string is %d. ", len);     getchar();    ...

  • In Programming language C - How would I convert my words char array into a string...

    In Programming language C - How would I convert my words char array into a string array so I can use the strcmp() function and alphabetically sort the words? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]){ int i =0; int j =0; int count =0; int length = strlen(argv[1]); for(i =0; i < length; i++){ if(isalpha(argv[1][i]) == 0 ||isdigit(argv[1][i] != 0)){ count ++; } printf("%c",argv[1][i]); } char *strings; int wordNum =0; int charNum =0; strings...

  • /* •   The following code consists of 5 parts. •   Find the errors for each part...

    /* •   The following code consists of 5 parts. •   Find the errors for each part and fix them. •   Explain the errors using comments on top of each part. */ #include <stdio.h> #include <string.h> int main( ) { ////////////////////////////////////////////////////////////////////////// //////////////        Part A. (5 points)                ////////////////// int g(void) { printf("%s", Inside function g\ n " ); int h(void) {       printf(" % s ", Inside function h\ n "); } } printf("Part A: \n "); g(); printf(" \n"); ////////////////////////////////////////////////////////////////////////// //////////////       ...

  • Syntax checker. We will write C code to check to see if an input string can be derived from a gra...

    Syntax checker. We will write C code to check to see if an input string can be derived from a grammar. The grammar is: A ::= 0 B B ::= 1 A | 2 B | ; Your program "check.c" should output "yes" if the line of standard input can be derived from this grammar, and "no" otherwise. Here are some examples: $ ./check 0 ; yes $ ./check 0 2 1 ; no $ The input values will always...

  • Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the O...

    Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the OpenMP parallel program much longer? Serial Program #include <stdio.h> #include <string.h> #include <time.h> int main(){    char str[1000], ch;    int i, frequency = 0;    clock_t t; struct timespec ts, ts2;       printf("Enter a string: ");    gets(str);    int len = strlen(str);    printf("Enter a character...

  • Using C programming Using the supplied example code as a starting point, add 3 more conversions...

    Using C programming Using the supplied example code as a starting point, add 3 more conversions using strtod(). Make sure you change the output conversion for type double. Use the same input strings and base codes as the strtol() function example but notice how the numbers are now represented. Manipulate the width and precision as needed for a good presentation.   Supplied code: #include <stdio.h> #include <string.h> int main(void) { long num; char* ptr; num = strtol("12345 Decimal Constant: ", &ptr,...

  • Linux & C code help with creating a child process and kills itself with SIGNAL. I...

    Linux & C code help with creating a child process and kills itself with SIGNAL. I provided most of the code, I need some help to finish. Please provide output screen shot as well. Thank you! A Process creates a child process. Child process will print out "hello 0" "hello 1" ..... 1 line per second. After a certain number of seconds(user input), it kills itself WITH signal mechanism ONLY, don't use exit() on the child. You can use alarm()...

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
ADVERTISEMENT