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;
}
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:

getchar() redirect to input textfile in ASCI C programming. Hello, underneath is the code I did...
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 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", 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 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 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...
This should be done in C. I have the code written to where it gives me the output however it is out of order from where it should be. The input should be 5 3 -7 3 5 -7 3 the output should be -7 occurs 2 times 3 occurs 3 times 5 occurs 2 times. My issue is I believe I need a bubble sort before my last loop but am unsure how to do it. #include <stdio.h> int...
/* • 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"); ////////////////////////////////////////////////////////////////////////// ////////////// ...
Hello, I need help with the following C code. This program asks the user to enter three numbers and outputs the sum on the screen , all im trying to do is have the program reprompt the user for a number if anything other than a number is entered for each of the entries. I wanted to use do while for each of the functions but that did not work for example: Enter Number 1: 2 Enter Number 2: Hello...
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 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...