C strings and character input, comparison - c

I am learning c programing, for exercise I found a task to solve.
It is something like a game hanging where someone chooses a word and other person guess letters. I have to check how many given words could be a correct answer to the choosen word.
Input:
3 3 // numbers n and m n-number of words to compare, m-number of letters checked
A C D // m letters separate by space
--CA // word that is given to guess
KUCA // n words to be checked if they match in this case "--CA"
ZGRADA // words are separated by enter(\n)
MACA // in this case "MACA" does not match "--CA" because there was a "A" in the guessed letters but not on the second dash meant for some letter
Output: 1
Idea is to check if the lenghts are the same if they are it should check if current character is "-" if so it should check every of m characters (guessed letters) if they match current one if so rez--;(sol--;)(before was declared rez=n;)
here is my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n=0,i=0,m=0,j=0,k=0,duzina=0,rez=0,br=0;
char slova[26], rec[30], pot[30],c;
scanf("%d %d\n",&n,&m);
br = 0;
while ( (c = getchar()) != '\n' ){
if ( c != ' ' ) { slova[br] = c; br++; }
}
slova[br] = '\0';
br = 0;
while ( (c = getchar()) != '\n' ){
rec[br] = c;
br++;
}
rec[br] = '\0';
rez=n;
duzina=strlen(rec);
for(i=0;i<n;i++){
br=0;
while ( (c = getchar()) != '\n' ){
pot[br] = c;
br++;
}
pot[br] = '\0';
if(duzina!=strlen(pot))
rez--;
else{
for(j=0;j<duzina;j++){
if(rec[j]=='-'){
for(k=0;k<m;k++){
if(pot[j]==slova[k]){
rez--;
break;
}
}
}
else{
if(rec[j]!=pot[j])
rez--;
}
}
}
}
printf("%d",rez);
return 0;
}
So my problem is in the wrong answer, and I don't know why.
Code works for example above but for this one it shows -8, it should 3:
10 5
A I L B X
--A--AB-X
PUAQFABHX
IIAFWABIX
MHAKVABPX
BQAXWABWX
TEAUIAB
PCALIABLX
RRAYHAEKJ
NWNKSGBNP
ZOCZKJWZR
ROAJRABZX

In the else portion of the following code:
if(duzina!=strlen(pot))
rez--;
else{
for(j=0;j<duzina;j++){
if(rec[j]=='-'){
for(k=0;k<m;k++){
if(pot[j]==slova[k]){
rez--;
break; // <-- This will break out of the 'for-k' loop
}
}
}
else{
if(rec[j]!=pot[j])
rez--; // <-- Are you missing a 'break' here?
}
}
}
After rez is decremented, you need to break out of the outer loop.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int n=0,i=0,m=0,j=0,k=0,duzina=0,rez=0,br=0;
char slova[26], rec[30], pot[30],c;
short booli=1;
scanf("%d %d\n",&n,&m);
br = 0;
while ( (c = getchar()) != '\n' ){
if ( c != ' ' ) { slova[br] = c; br++; }
}
slova[br] = '\0';
br = 0;
while ( (c = getchar()) != '\n' ){
rec[br] = c;
br++;
}
rec[br] = '\0';
rez=n;
duzina=strlen(rec);
for(i=0;i<n;i++){
br=0;
booli=1;// this is the line that i forgot, that caused all the trouble.
while ( (c = getchar()) != '\n' ){
pot[br] = c;
br++;
}
pot[br] = '\0';
if(duzina!=strlen(pot))
rez--;
else{
for(j=0;j<duzina;j++){
if(rec[j]=='-'){
for(k=0;k<m;k++){
if(pot[j]==slova[k]){
rez--;
booli=0;
break;
}
}
if(booli==0) break;/*this is for checking if the for-k loop was ended with break;*/
}
else{
if(rec[j]!=pot[j]){
rez--;
break;
}
}
}
}
}
printf("%d",rez);
return 0;
}
Thaks to agbinfo for saying that I that I need second break;

Related

C Program doesn't end after giving the correct output

So I'm trying to do a program that reads a sequence of numbers separated by spaces and new lines. The output should be the same sequence, but erasing unnecessary zeros(The sequence of charachters 'EOF' ends the program). Per example
01492 102934 should come out as 1492 102934
9312 0 01923 should come out as 9312 0 1923
0001249 0000 should come out as 1249 0
Well I've achieved that purpose but have come across a roadblock. The program doesn't exit unless I type the EOF sequence. Maybe it's because I have a while(1) running that gives an infinite loop. But when I try to delete it the program doesn't even print at all. I'm still learning this is for a school project.
Any help would be apreciated!
Here's the code:
#include <stdio.h>
int main(){
char c;
int i=0;
while(1){
c=getchar();
if (i==0){
if(c=='0'){
while (c=='0'){
c=getchar();
}
}
printf("%c",c);
i=i+1;
}
else if (c==' '){
printf("%c",c);
c=getchar();
if(c=='0'){
while (c=='0'){
c=getchar();
}
}
printf("%c",c);
}
else if (c=='E'){
c=getchar();
if (c=='O'){
c=getchar();
if(c=='F'){
printf("\n");
return 0;
}
}
}
else{
printf("%c",c);
}
}
}
The important stuff:
int c; // IMPORTANT, cannot be char
while (1) {
c = getchar();
if (c == EOF) break; // exit loop
// ...
}
There has to be some way to tell the program to exit.
With this, the program will exit on the letter x or two consecutive newlines or entering END.
getchar will return EOF when there is nothing left to read from a file. That can be simulated from stdin ( the keyboard) with ctrl + z on Windows or ctrl + d on Linux.
#include <stdio.h>
#include <string.h>
int main ( void) {
char done[4] = "";
int c = 0;
int prior = 0;
int reading = 0;
int zero = 1;
while ( EOF != ( c = getchar ( )) && 'x' != c) {
if ( '\n' == c && '\n' == prior) {
break;
}
if ( c >= '0' && c <= '9') {
reading = 1;
if ( '0' != c) {
zero = 0;
}
if ( ! zero) {
putchar ( c);
}
}
else {
if ( reading) {
if ( zero) {
putchar ( '0');
}
if ( ' ' == c || '\n' == c) {
putchar ( c);
}
else {
putchar ( ' ');
}
}
reading = 0;
zero = 1;
}
prior = c;
done[0] = done[1];
done[1] = done[2];
done[2] = c;
done[3] = 0;
if ( 0 == strcmp ( done, "END")) {
break;
}
}
putchar ( '\n');
return 0;
}
getchar() returns an int, not a char. If it only returned a char, there would be no way for it to return a value that indicates end of file, since all char values are valid and can’t be used for another purpose.
A motivating example in decimal system may be: A function checks the temperature returns a two-digit number. Any temperature between 0 and 99 is valid. How do you report errors when the thermometer is disconnected? You have to return a number with more digits, and use a special value like UNPLUGGED = 100.
But int is a wider type: it has many more values than char, and the “extra” values can be used to indicate some special condition that means “hey, this is not a valid character, but something else I had to tell you”.
getchar() returns the EOF constant upon failure (any failure), for example if no more input is available. There’s nothing sensible you can do even if the reason for the failure other than end of input. You should end processing at the first EOF.
Thus, change the type of c to int, and every time you call getchar(), you must check that its value is not EOF, and return when you encounter it.
The nested structure of your loops means that EOF checking has to be repeated all over the place. There are other ways to structure the code to keep this check in one place, but, admittedly, the nested loops have at least the potential to exploit the branch predictor, whereas a single getchar followed by a state-machine style switch statement will make it perform potentially worse. None of this matters in a simple homework problem, but it’s something to keep in mind. In any case, performance has to be benchmarked - no other way around it.
Try this code, I think it does what you requested:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static int getLine(char *prmpt, char *buff, size_t sz) {
int ch, extra;
// Get line with buffer overrun protection.
if (prmpt != NULL) {
printf("%s", prmpt);
fflush(stdout);
}
if (fgets(buff, sz, stdin) == NULL)
return -2;
// If it was too long, there'll be no newline. In that case, we flush
// to end of line so that excess doesn't affect the next call.
if (buff[strlen(buff) - 1] != '\n') {
extra = 0;
while (((ch = getchar()) != '\n') && (ch != EOF))
extra = 1;
return (extra == 1) ? -1 : 0;
}
// Otherwise remove newline and give string back to caller.
buff[strlen(buff) - 1] = '\0';
return 0;
}
int* convert2numbers(char* arr, int size) {
int i;
int j;
int k;
char token[100];
int* numbers;
int last_space = 0;
int index = 1;
int amount = 1;
// Count the amount of tokens.
for (i = 0; i < size; ++i) {
if (arr[i] == ' ') {
++amount;
}
}
numbers = (int *)malloc(amount * sizeof(int));
numbers[0] = amount;
for (j = 0; j <= size; ++j) {
if (arr[j] == ' ' || arr[j] == '\0') {
// Copy token from input string.
for (k = 0; k < j; ++k) {
token[k] = arr[k + last_space];
}
token[j] = '\0';
numbers[index] = atoi(token);
// Clear the token and continue.
memset(token, '\0', sizeof(token));
last_space = j;
++index;
}
}
return numbers;
}
int main(void) {
int i;
int size;
int* numbers;
int amount;
char input[100];
char help[] = "Numbers> ";
printf("Input numbers below or press enter to exit!\n");
while (1) {
getLine(help, input, sizeof(input));
// If input is empty exit.
if (input[0] == '\0') {
break;
}
size = strlen(input);
numbers = convert2numbers(input, size);
amount = numbers[0];
for (i = 1; i < amount + 1; ++i) {
printf("%d ", numbers[i]);
}
printf("\n");
}
return 0;
}
When run with these inputs this code outputs:
Input numbers below or press enter to exit!
Numbers> 01492 102934
1492 102934
Numbers> 9312 0 01923
9312 0 1923
Numbers> 0001249 0000
1249 0
Also if you press enter in console, it exits, as to escape the while(1) loop, easily.

Copying input to output without unnecessary Spaces in C

I'm trying to write a program in C that copies its input to its output while replacing each string of one or more Spaces with a single Space.
My code isn't doing that but is instead taking away every second character.
This is my code:
#include <stdio.h>
main()
{
int c;
int lastc;
lastc = 0;
while(getchar() != EOF){
c = getchar();
if(c == 32 && lastc == 32)
;
else
putchar(c);
lastc = c;
}
}
Your loop should look like:
while((c = getchar()) != EOF){
if(c == 32 && lastc == 32)
;
else
putchar(c);
lastc = c;
}
In your version you get a char with getchar while checking the condition for the while loop and then as a next step you again get a char with getchar. So the first one is not used in your code. Therefore it is taking away every second character.
Keep running in while loop until you get non-space character and print just one space after you get out.
int main()
{
int c;
bool space=false;
while ((c=getchar()) != EOF) {
while (isspace(c)) {
space = true;
c = getchar();
}
if (space) {
putchar(' ');
space = false;
}
putchar(c);
}
return 0;
}
I use fgets() function to getting string from input i.e stdin and store in the scroll string.
Then you must implement a way to analyze string to find spaces in it.
When you find first space, increase index if you face another space.
This is the code.
Code
#include <stdio.h>
int main(void){
char scroll[100];// = "kang c heng junga";
fgets(scroll, 100, stdin);
printf ("Full name: %s\n", scroll);
int flag = 0;
int i=0;
while (scroll[i] != '\0')
{
if (scroll[i] == ' ' )
flag=1;//first space find
printf("%c",scroll[i]);
if (flag==0){
i++;
}else {
while(scroll[i]==' ')
i++;
flag=0;
}
}
return 0;
}
Sample input: Salam be shoma doostane aziz
Program output: Salam be shoma doostane aziz
[Edit]
Use new string st to hold space eliminated string an print as output.
Also this code work for Persian string.
char scroll[100]={0};// = "kang c heng junga";
printf("Enter a string: ");
fgets(scroll, 100, stdin);
printf ("Original string: %s\n", scroll);
char st[100]={0};
int flag = 0;
int i=0;
int j=0;
while (scroll[i] != '\0')
{
if (scroll[i] == ' ' )
flag=1;//first space find
st[j]=scroll[i];
j++;
if (flag==0){
i++;
}else {
while(scroll[i]==' ')
i++;
flag=0;
}
}
printf("Eliminate Spaces: %s", st);

Word counting program in c (it should exclude comments)

I currently got assignment to make word counting program in c. Task is your program should count characters, lines and words. Lines should be counted even if it is inside comment. Comments should be represented as an " " space character.
I tried to use switch function to implement cases inside and outside word. But I am having trouble with comment. In first if statement I tried to check if we are in comment or not and if so skip it.
#include <stdio.h>
#include <ctype.h>
#define word 1
#define nword 0
int main(void)
{
int c,d,nmb = 0 , state = nword , ch = 0, ln = 0;
while((c=getchar())!=EOF)
{
if( c == '/' )
{
if((d=getchar())=='*')
{
int f;
c = getchar();
d = getchar();
while(c!='*' || d !='/')
{
c = d;
d = getchar();
if(d == '\n')
{ ln++ ; ch++; }
if(d == EOF)
{ f=1 ; break; }
}
if(f==1)
printf("problem in line: %d\n", ln);
state=word;
nmb++;
}
}
switch(state)
{
case nword:
if(isspace(c))
{
state=word;
nmb++;
}
break;
case word:
if(!isspace(c))
state=nword;
break;
}
if (c == '\n')
ln++;
ch++;
}
printf("words: %d\n", nmb);
printf("chars: %d\n", ch);
printf("lines: %d\n", ln);
return 0;
}
I cant get right answers to test cases. I cant debug too because I cant see certain pattern which can be fixed. Some test cases matches right but others dont.(Test cases in https://ee209-2019-spring.github.io/assignment/wc209/ )

counting the number of sentences in a paragraph in c

As part of my course, I have to learn C using Turbo C (unfortunately).
Our teacher asked us to make a piece of code that counts the number of characters, words and sentences in a paragraph (only using printf, getch() and a while loop.. he doesn't want us to use any other commands yet). Here is the code I wrote:
#include <stdio.h>
#include <conio.h>
void main(void)
{
clrscr();
int count = 0;
int words = 0;
int sentences = 0;
char ch;
while ((ch = getch()) != '\n')
{
printf("%c", ch);
while ((ch = getch()) != '.')
{
printf("%c", ch);
while ((ch = getch()) != ' ')
{
printf("%c", ch);
count++;
}
printf("%c", ch);
words++;
}
sentences++;
}
printf("The number of characters are %d", count);
printf("\nThe number of words are %d", words);
printf("\nThe number of sentences are %d", sentences);
getch();
}
It does work (counts the number of characters and words at least). However when I compile the code and check it out on the console window I can't get the program to stop running. It is supposed to end as soon as I input the enter key. Why is that?
Here you have the solution to your problem:
#include <stdio.h>
#include <conio.h>
void main(void)
{
clrscr();
int count = 0;
int words = 0;
int sentences = 0;
char ch;
ch = getch();
while (ch != '\n')
{
while (ch != '.' && ch != '\n')
{
while (ch != ' ' && ch != '\n' && ch != '.')
{
count++;
ch = getch();
printf("%c", ch);
}
words++;
while(ch == ' ') {
ch = getch();
printf("%c", ch);
}
}
sentences++;
while(ch == '.' && ch == ' ') {
ch = getch();
printf("%c", ch);
}
}
printf("The number of characters are %d", count);
printf("\nThe number of words are %d", words);
printf("\nThe number of sentences are %d", sentences);
getch();
}
The problem with your code is that the innermost while loop was consuming all the characters. Whenever you enter there and you type a dot or a newline it stays inside that loop because ch is different from a blank. However, when you exit from the innermost loop you risk to remain stuck at the second loop because ch will be a blank and so always different from '.' and '\n'. Since in my solution you only acquire a character in the innermost loop, in the other loops you need to "eat" the blank and the dot in order to go on with the other characters.
Checking these conditions in the two inner loops makes the code work.
Notice that I removed some of your prints.
Hope it helps.
Edit: I added the instructions to print what you type and a last check in the while loop after sentences++ to check the blank, otherwise it will count one word more.
int ch;
int flag;
while ((ch = getch()) != '\r'){
++count;
flag = 1;
while(flag && (ch == ' ' || ch == '.')){
++words;//no good E.g Contiguous space, Space at the beginning of the sentence
flag = 0;;
}
flag = 1;
while(flag && ch == '.'){
++sentences;
flag=0;
}
printf("%c", ch);
}
printf("\n");
I think the problem is because of your outer while loop's condition. It checks for a newline character '\n', as soon as it finds one the loop terminates. You can try to include your code in a while loop with the following condition
while((c=getchar())!=EOF)
this will stop taking input when the user presses Ctrl+z
Hope this helps..
You can implement with ease an if statement using while statement:
bool flag = true;
while(IF_COND && flag)
{
//DO SOMETHING
flag = false;
}
just plug it in a simple solution that uses if statements.
For example:
#include <stdio.h>
#include <conio.h>
void main(void)
{
int count = 0;
int words = 1;
int sentences = 1;
char ch;
bool if_flag;
while ((ch = getch()) != '\n')
{
count++;
if_flag = true;
while (ch==' ' && if_flag)
{
words++;
if_flag = false;
}
if_flag = true;
while (ch=='.' && if_flag)
{
sentences++;
if_flag = false;
}
}
printf("The number of characters are %d", count);
printf("\nThe number of words are %d", words);
printf("\nThe number of sentences are %d", sentences);
getch();
}
#include <stdio.h>
#include <ctype.h>
int main(void){
int sentence=0,characters =0,words =0,c=0,inside_word = 0,temp =0;
// while ((c = getchar()) != EOF)
while ((c = getchar()) != '\n') {
//a word is complete when we arrive at a space after we
// are inside a word or when we reach a full stop
while(c == '.'){
sentence++;
temp = c;
c = 0;
}
while (isalnum(c)) {
inside_word = 1;
characters++;
c =0;
}
while ((isspace(c) || temp == '.') && inside_word == 1){
words++;
inside_word = 0;
temp = 0;
c =0;
}
}
printf(" %d %d %d",characters,words,sentence);
return 0;
}
this should do it,
isalnum checks if the letter is alphanumeric, if its an alphabetical letter or a number, I dont expect random ascii characters in my sentences in this program.
isspace as the name says check for space
you need the ctype.h header for this. or you could add in
while(c == ' ') and whie((c>='a' && c<='z') || (c >= 'A' && c<='Z')
if you don't want to use isalpace and isalnum, your choice, but it will be less elegant :)
The trouble with your code is that you consume the characters in each of your loops.
a '\n' will be consumed either by the loop that scans for words of for sentences, so the outer loop will never see it.
Here is a possible solution to your problem:
int sentences = 0;
int words = 0;
int characters = 0;
int in_word = 0; // state of our parser
int ch;
do
{
int end_word = 1; // consider a word wil end by default
ch = getch();
characters++; // count characters
switch (ch)
{
case '.':
sentences++; // any dot is considered end of a sentence and a word
break;
case ' ': // a space is the end of a word
break;
default:
in_word = 1; // any non-space non-dot char is considered part of a word
end_word = 0; // cancel word ending
}
// handle word termination
if (in_word and end_word)
{
in_word = 0;
words++;
}
} while (ch != '\n');
A general approach to these parsing problems is to write a finite-state machine that will read one character at a time and react to all the possible transitions this character can trigger.
In this example, the machine has to remember if it is currently parsing a word, so that one new word is counted only the first time a terminating space or dot is encountered.
This piece of code uses a switch for concision. You can replace it with an if...else if sequence to please your teacher :).
If your teacher forced you to use only while loops, then your teacher has done a stupid thing. The equivalent code without other conditional expressions will be heavier, less understandable and redundant.
Since some people seem to think it's important, here is one possible solution:
int sentences = 0;
int words = 0;
int characters = 0;
int in_word = 0; // state of our parser
int ch;
// read initial character
ch = getch();
// do it with only while loops
while (ch != '\n')
{
// count characters
characters++;
// count words
while (in_word)
{
in_word = 0;
words++;
}
// skip spaces
while (ch == ' ')
{
ch = -1;
}
// detect sentences
while (ch == '.')
{
sentences++;
ch = -1;
}
// detect words
while ((ch != '\n')
{
word_detected = 1;
ch = -1;
}
// read next character
ch = getch();
}
Basically you can replace if (c== xxx) ... with while (c== xxx) { c = -1; ... }, which is an artifical, contrieved way of programming.
An exercise should not promote stupid ways of doing things, IMHO.
That's why I suspect you misunderstood what the teacher asked.
Obviously if you can use while loops you can also use if statements.
Trying to do this exercise with only while loops is futile and results in something that as little or nothing to do with real parser code.
All these solutions are incorrect. The only way you can solve this is by creating an AI program that uses Natural Language Processing which is not very easy to do.
Input:
"This is a paragraph about the Turing machine. Dr. Allan Turing invented the Turing Machine. It solved a problem that has a .1% change of being solved."
Checkout OpenNLP
https://sourceforge.net/projects/opennlp/
http://opennlp.apache.org/

Can't print in largest line in C

The program is suppose to print out the longest line in all the outputs.But the program is acting pretty weird. Sometimes it doesn't terminate even when the EOF(ctrl+Z) is triggered and some times it prints blank or weird symbols. I don't why it is not working; can somebody help me fix it please?
//START
#include <stdio.h>
#include <stdlib.h>
#define mx 100
int main(void)
{
int line[mx],lng[mx],c,word,maxim;
word=1;
maxim=10;
int i=0;
while((c=getchar())!=EOF)
{
while((c=getchar())!='\n')
{
line[i]=(c=getchar());
if(((c=getchar())==' ') || ((c=getchar())=='\t'))
{
word++;
}
i++;
}
if(woasdrd>=maxim)
{
for(int d=0;d<=99;d++)
{
copyline(lng[d],line[d]);
}
word=1;
i=0;
}
else
{
i=0;
word=1;
}
}
for(int g;g<=99;g++)
{
putchar(lng[g]);
}
}
copyline(int to[],int from[])
{
for(int i=0;i<=99;i++)
{
to[i]=from[i];
}
}
//END
#include <stdio.h>
#include <stdlib.h>
#define mx 100
//copy string array from from[] to to[],and both of it is end with '\0'
void copyline(char to[],char from[])
{
int i = 0;
while( from[i] )
{
to[i]=from[i];
i++;
}
}
int main(void)
{
char line[mx] = { ' ' },lng[mx] = { ' ' }; //line keep the line you just input,lng keep the longest line
int maxim , c; //maxim keep the longest num of longest line
int i=0;
maxim=0;
//get input from stdin ,if EOF then end(Ctrl + C or Ctrl + d is EOF)
while( ( c = getchar() ) != EOF )
{
//if you input a Enter then compare it's length with maxim
if( c == '\n' )
{
line[i++] = '\0'; //turn '\n' into '\0',for string end with '\0'
if( i > maxim ) //compare the line you just input with the longest line,if get longer one,copy it to lng
{
maxim = i;
copyline( lng , line );
lng[ i ] = '\0'; //for string end with '\0'
}
i = 0; //if you get a '\n' ,then you should be ready for next input line,so i = 0,and continue for new get
continue;
}
line[i++] = c; //keep input to line
}
//that's output,for string end with '\0',so put it as condition for while loop
i = 0;
while( lng[i] )
{
printf("%c",lng[i++]);
}
printf("\n");
}
maybe this is what you want,at first I want to improve your code,but it have much errors,include logic and code,so I rewrite your code.if you have problem with this code,please let me know.
I think you are calling getchar() in too many places. You are removing a character from input every time you call that. Really you should call c=getchar() once before the first while loop to read the first character. Remove the rest of the c=getchar() and just use c. Then at the at the end of the inner while loop right after the i++; you should use c=getchar() to read the next character.

Resources