strstr not performing upto expectations - c

i cant seem to find whats wrong with my logic that the strstr not performing upto my expectations
its a question to find prime number in a binary string, and i'm finding 10 or 11 in that string and the outcome is not what i've expected.
sorry if i've done a terrible silly mistake, i'm a noob.
#include <stdio.h>
#include <string.h>
int main(void)
{
int t;
scanf("%d", &t);
while (t--)
{
char str[100000];
scanf("%s", str);
char ten[] = "10";
char eleven[] = "11";
int flag = 0;
if (strstr(str, ten) == NULL)
{
flag = 1;
}
else if (strstr(str,eleven) == NULL)
{
flag = 1;
}
if (flag == 1)
{
printf("NO\n");
}
else if (flag == 0)
{
printf("YES\n");
}
}
return 0;
}
input providing is
3 //test cases
1 //strings
111
101101
output getting
NO
NO
YES
output expecting
NO
YES
YES

You say you want your code to output "YES" for "111". For that to happen, you must leave flag at zero. But you set flag to one if the string contains no "10" and "111" contains no "10".

this code is working fine. i've made some adjustments, thanks to you guys.
#include <stdio.h>
#include <string.h>
int main(void)
{
int t;
scanf("%d", &t);
while (t--)
{
char str[100000];
scanf("%s", str);
char ten[] = "10";
char eleven[] = "11";
int flag = 1;
char *one = strstr(str, ten);
char *two = strstr(str, eleven);
if ((one == NULL) && (two == NULL))
{
flag = 0;
}
else
{
flag = 1;
}
if (flag == 0)
{
printf("NO\n");
}
else if (flag == 1)
{
printf("YES\n");
}
}
return 0;
}

The problem is with the if Statement. If '10' is not found in the first two characters, it would terminate there. Not checking for 11 also.
#include <stdio.h>
#include <string.h>
int main(void)
{
int t;
scanf(" %d", &t);
while (t--)
{
char str[100000];
scanf(" %s", str);
char ten[] = "10";
char eleven[] = "11";
int flag = 0;
if (strstr(str, ten) == NULL && strstr(str,eleven) == NULL)
{
flag = 1;
}
if (flag == 1)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}

Related

How can I verify if a file only contains numbers in C?

I'm a beginner in learning C. If I have a file with the given format:
12 20 40 60 80
04 10 34 30 20
How can I verify with a function that it's integers only.
I'm working on an assignment that requires me to do this. That is why I can't include my main but I am passing a file into my function.
bool check(FILE *fp) {
int numArray[26];
int i = 0;
int num;
while(fscanf(fp, "%d", &num)) {
if (isdigit(num) == 0) {
fprintf(stderr, "Not correct format ");
return false;
} else
{
numArray[i] = num;
i++;
}
}
}
If I give it the incorrect format such as
12x 10 15 13 10
Nothing happens. I was wondering if anyone can point me in the right direction?
fscanf will return the number of successfully recognized patterns from the format in the input; if it sees something that does not match the next format pattern (such as a letter for a %d pattern) it will reject it and return a smaller number. So in your case, you can just look for fscanf returning 0:
bool check(FILE *fp) {
int numArray[26];
int i = 0;
int num;
int matched;
while ((matched = fscanf(fp, "%d", &num)) != EOF) {
if (matched == 0) {
fprintf(stderr, "Not correct format\n");
return false;
} else if (i >= 26) {
fprintf(stderr, "Too many numbers\n");
return false;
} else
{
numArray[i] = num;
i++;
}
}
return true;
}
int is_number(char *str)
{
int i = 0;
while (str[i] != '\0')
{
if (str[i] < '0' || str[i] > '9')
return 0;
i++;
}
return 1;
}
maybe this. so you are looking if variable only has numbers?
ok maybe this works better
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
FILE *fp;
char c;
char s[100];
int i = 0;
fp = fopen("test.txt", "r");
if(fp == NULL)
{
printf("Error opening file");
exit(1);
}
while((c = fgetc(fp)) != EOF)
{
if(isdigit(c) == 0)
{
printf("Not only integers\n");
break;
}
}
if(c == EOF)
printf("Only integers\n");
fclose(fp);
return 0;
}
but yeah its pretty much same thing

*pointer_variable != '\0' is not working for the check of unsuccessful conversion in strtol() function

The program was not working for input 5r i.e in input when first character is number and remaining next character is any alphabet or negative number. For example when I am giving input as 5r in the output I am getting factorial of 5.
So I tried putting check for strtol unsuccessful conversion :-
if (p == buf || *p != '\0'){ printf("\nInvalid input: not a number\n");}
but I am getting output as Invalid input: not a number for all the input.
I found many similar questions in Stack Overflow. However, they don't resolve my issue. I am not understanding what is wrong with this simple check? How can I successfully detect errors from strtol?
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <errno.h>
int display();
void fact_fun(int num_fact);
int main()
{
int num;
while ((num = display()) >= 0)
{
fact_fun(num);
}
return 0;
}
int display()
{
char buf[256];
char *p;
long value;
for (;;)
{
printf("\nEnter number to find factorial or press ENTER KEY to exit: ");
if (fgets(buf, sizeof buf, stdin) == NULL || *buf == '\n')
return -1;
errno = 0;
value = strtol(buf, &p, 0);
if (p == buf || *p != '\0')
{
printf("\nInvalid input: not a number\n");
}
else
{
if (value < 0)
{
printf("\nInvalid input: negative values not allowed\n");
}
else if (errno != 0 || value > INT_MAX)
{
printf("\nInvalid input: value too large for type int\n");
}
else
{
return (int)value;
}
}
}
}
void fact_fun(int num_fact)
{
int fact = 1;
for (int i = 1; i <= num_fact; i++)
{
if (fact > INT_MAX / i)
{
printf("\nInvalid input: arithmetic overflow\n");
return;
}
fact = fact * i;
}
printf("\nFactorial of %d is %d\n", num_fact, fact);
}
The string you get from fgets contains '\n' as last char because you hit enter, so replace it with '\0'. That is a common error we C coders sometimes make.
Edit:
So I have tested it myself, and you're right, the reason is that strtoI does not mess with line terminator, so now it works fine with the following check:
*p != '\n'
The full working code is this:
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <errno.h>
int display();
void fact_fun(int num_fact);
int main()
{
int num;
while ((num = display()) >= 0)
{
fact_fun(num);
}
return 0;
}
int display()
{
char buf[256];
char *p;
long value;
for (;;)
{
printf("\nEnter number to find factorial or press ENTER KEY to exit: ");
if (fgets(buf, sizeof buf, stdin) == NULL || *buf == '\n')
return -1;
errno = 0;
value = strtol(buf, &p, 0);
if (p == buf || *p != '\n')
{
printf("\nInvalid input: not a number\n");
}
else
{
if (value < 0)
{
printf("\nInvalid input: negative values not allowed\n");
}
else if (errno != 0 || value > INT_MAX)
{
printf("\nInvalid input: value too large for type int\n");
}
else
{
return (int)value;
}
}
}
}
void fact_fun(int num_fact)
{
int fact = 1;
for (int i = 1; i <= num_fact; i++)
{
if (fact > INT_MAX / i)
{
printf("\nInvalid input: arithmetic overflow\n");
return;
}
fact = fact * i;
}
printf("\nFactorial of %d is %d\n", num_fact, fact);
}

I am getting a _\377 in my output

I have a school assignment to make a hangman game. The game works how I want it to except for one small glitch. If the user entered word is 4 letters or less, the hidden word is displayed with an extra "_\377" at the end. When the user entered word is 5 letters or more, then there is no glitch. I am hoping that someone would be kind enough to help me trouble shoot the problem. Thanks in advance!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int letterfinder(char string[], char a, int vari)
{
int length = strlen(string);
int i = vari;
int val = 0;
while( i <= length && val != 1)
{
if( string[i] == a)
{
val = 1;
}
i++;
}
if( val == 0)
{
return 100;
}
else
{
return i;
}
}
int main()
{
char inWord[] = "1111111111111111111111111111";
char outWord2[] = "1111111111111111111111111111";
char guess;
int gameover = 0;
int trys = 10;
int vari = 0;
printf("Please enter a word: ");
gets(inWord);
printf("%s\n", inWord);
printf(" \n");
printf(" \n");
printf(" \n");
printf(" \n");
printf(" \n");
printf(" \n");
int i2 = 0;
int j2 = 0;
int i3 = 0;
i2 = strcspn(inWord, outWord2);
char outWord[80];
while(i3 < i2)
{
outWord[i3] = '1';
i3++;
}
while(j2 < i2)
{
outWord[j2] = '-';
j2++;
}
puts(outWord);
while(gameover != 1 )
{
printf("What is your guess: ");
scanf("%s", &guess);
vari = 0;
if(letterfinder(inWord, guess, vari) == 100)
{
printf("Wrong!");
trys--;
printf("You have %d attempts left\n", trys);
if(trys == 0)
{
gameover = 1;
printf("You ran out of attempts. Game over\n");
}
}
else
{
outWord[(letterfinder(inWord, guess, vari) - 1)] = guess;
vari = (letterfinder(inWord, guess, vari));
while(letterfinder(inWord, guess, vari) != 100)
{
outWord[(letterfinder(inWord, guess, vari) - 1)] = guess;
vari = letterfinder(inWord, guess, vari);
}
puts(outWord);
}
int value = 0;
i3 = 0;
while( i3 <= i2)
{
if( outWord[i3] == '-')
{
value = 1;
}
i3++;
}
if(value != 1)
{
printf("Congratulations, you have guessed the word!\n");
gameover = 1;
}
}
return 0;
}
Your code has Undefined Behaviour. In the cases it "works" it is only by chance/luck. char guess; scanf("%s", &guess); That causes memory corruption as you are writing a string to a variable that can only hold a single char. Even a single letter guess will require two characters to store as all C strings are NUL terminated.
– kaylum

Palindrome program in C

My program in C which is Palindrome has an error in its function. My function is not comparing the 2 characters in my string. When I type a single character it answers palindrome but if it is two or more always not palindrome.
Code:
int IntStrlength=strlen(StrWord);
int IntCtr2=0;
int IntCtr=1, IntAnswer;
while(IntCtr<=(IntStrlength/2)){
printf(" %d %d\n", IntCtr2,IntStrlength);
if(StrWord[IntStrlength] != StrWord[IntCtr2]){
IntAnswer=0;
printf(" %d=Not Palindrome", IntAnswer);
exit (0);
}//if(StrWord[IntCtr2]!=StrWord[IntStrlength]) <---------
else{
IntCtr2++;
IntStrlength--;
}// else <--------
IntCtr++;
}//while(IntCtr<IntStrlength/2) <-----------
IntAnswer=1;
printf(" %d=Palindrome", IntAnswer);
return ;
}
Single character:
Two or more characters:
Why not write it like this
int wordLength = strlen(StrWord);
for (int i=0;i<(wordLength/2);i++) {
if (StrWord[i] != StrWord[wordLength-i-1]) {
return 0;
}
}
return 1;
For words with an even length (say 8) the counter will go from 0 to 3, accessing all letters. For uneven words (say 7) the c ounter will go from 0 to 2, leaving the middle element unchecked. This is not necessary since its a palindrome and it always matches itself
#include<stdio.h>
int check_palindrom(char *);
int main()
{
char s1[20];
printf("Enter the string...\n");
gets(s1);
int x;
x=check_palindrom(s1);
x?printf("Palindrom\n"):printf("Not Palindrom\n");
}
int check_palindrom(char *s)
{
int i,j;
for(i=0;s[i];i++);
for(i=i-1,j=0;i>j;i--,j++)
if(s[i]!=s[j])
return 0;
if(s[i]==s[j])
return 1;
}
Enter the string...
radar
Palindrom
I've seen this algorithm before in a interview book called "Cracking the Coding Interview".
In it the author shows a very simple and easy implementation of the code. The code is below: Also here is a video explaining the code.
#include<stdio.h>
#include<string.h> // strlen()
void isPalindrome(char str[]);
int main(){
isPalindrome("MOM");
isPalindrome("M");
return 0;
}
void isPalindrome(char str[]){
int lm = 0;//left most index
int rm = strlen(str) - 1;//right most index
while(rm > lm){
if(str[lm++] != str[rm--]){
printf("No, %s is NOT a palindrome \n", str);
return;
}
}
printf("Yes, %s is a palindrome because the word reversed is the same \n", str);
}
You can do this like this:
#include <stdio.h>
#include <string.h>
int check_palindrome(char string []);
int main()
{
char string[20];
printf("Enter the string...\n");
scanf ("%s", &string);
int check;
check = check_palindrome (string);
if (check == 0)
printf ("Not Palindrome\n");
else
printf ("Palindrome\n");
return 0;
}
int check_palindrome (char string [])
{
char duplicate [];
strcpy (string, duplicate);
strrev (string);
if (strcmp (string, duplicate) == 0)
return 1;
else
return 0;
}
This uses the strcmp and strrev function.
Take a look at this code, that's how I have implemented it (remember to #include <stdbool.h> or it will not work):
for(i = 0; i < string_length; i++)
{
if(sentence[i] == sentence[string_lenght-1-i])
palindrome = true;
else
{
palindrome = false;
break;
}
}
Doing that it will check if your sentence is palindrome and, at the first occurence this is not true it will break the for loop. You can use something like
if(palindrome)
printf(..);
else
printf(..);
for a simple prompt for the user.
Example :
radar is palindrome
abba is palindrome
abcabc is not palindrome
Please , pay attention to the fact that
Abba
is not recognized as a palindrome due to the fact that ' A ' and 'a' have different ASCII codes :
'A' has the value of 65
'a' has the value of 97
according to the ASCII table. You can find out more here.
You can avoid this issue trasforming all the characters of the string to lower case characters.
You can do this including the <ctype.h> library and calling the function int tolower(int c); like that :
for ( ; *p; ++p) *p = tolower(*p);
or
for(int i = 0; str[i]; i++){
str[i] = tolower(str[i]);
}
Code by Earlz, take a look at this Q&A to look deeper into that.
EDIT : I made a simple program to do this, see if it can help you
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
void LowerCharacters(char *word, int word_lenth);
int main(void){
char *word = (char *) malloc(10);
bool palindrome = false;
if(word == 0)
{
printf("\nERROR : Out of memory.\n\n");
return 1;
}
printf("\nEnter a word to check if it is palindrome or not : ");
scanf("%s", word);
int word_length = strlen(word);
LowerCharacters(word,word_length);
for(int i = 0; i < word_length; i++)
{
if(word[i] == word[word_length-1-i])
palindrome = true;
else
{
palindrome = false;
break;
}
}
palindrome ? printf("\nThe word %s is palindrome.\n\n", word) : printf("\nThe word %s is not palindrome.\n\n", word);
free(word);
return 0;
}
void LowerCharacters(char *word, int word_length){
for(int i = 0; i < word_length; i++)
word[i] = tolower(word[i]);
}
Input :
Enter a word to check if it is palindrome or not : RadaR
Output :
The word radar is palindrome.
This code may help you to understand the concept:
#include<stdio.h>
int main()
{
char str[50];
int i,j,flag=1;
printf("Enter the string");
gets(str);
for(i=0;str[i]!='\0';i++);
for(i=i-1,j=0;j<i;j++,i--)
{
str[i]=str[i]+str[j];
str[j]=str[i]-str[j];
str[i]=str[i]-str[j];
}
for(i=0;str[i]!='\0';i++);
for(i=i-1,j=0;j<i;j++,i--)
{
if(str[i]==str[j]){
flag=0;
break;
}
}if(flag==0)
{
printf("Palindrome");
}else
{
printf("Not Palindrome");
}
}
I have solution for this
char a[]="abbba";
int i,j,b=strlen(a),flag=0;
for(i=0,j=0; i<b; i++,j++)
{
if(a[i]!=a[b-j-1])
{
flag=1;
break;
}
}
if(flag)
{
printf("the string is not palindrum");
}
else
{
printf("the string is palindrum");
}
This may works for you
#include <stdio.h>
#include <stdlib.h>
int main(void) {
setbuf(stdout,NULL);
int i,limit;
char string1[10];
int flag=0;
printf("enter a string");
scanf("%s",string1);
limit=strlen(string1);
for(i=0;i<limit;i++){
if(string1[i]!=string1[limit-i-1]){
flag=1;
break;
}
} if(flag==1){
printf("entered string is not palindrome");
}else{
printf("entered string is palindrome");
}
return EXIT_SUCCESS;
}

Find sum where user has to enter a command with parameters such as 'sum(par1, par2,...)'

User enters command with parameters.There are no errors, but I am having trouble with the weird output.
I took input as string, identified if the input matches the command 'sum', if it does then extracted the parameters in between sum[], stored them in an array and send them as arguments to sum function.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int sumOfArray(int arr[])
{
int sum=0;
for(int i=0;i++;i<sizeof(arr))
{
sum = sum + arr[i];
}
printf("%d\n",sum);
return sum;
}
int main()
{
char input[256];
int j=0,temp=0;
int arraySum[6]={0};
printf("user_account $> ");
fgets(input, sizeof(input), stdin);
if (input[0]=='s' && input[1]=='u' && input[2]=='m')
{
if (input[3]!='(')
{
printf("Please enter proper parameters : sum(num1, num2, ..)\n");
}
else
{
for (int i = 4; i++; input[i] < ']')
{
if (input[i] !=',')
{
if (!isdigit(input[i]))
{
printf("Please enter only digits\n");
break;
}
else
{
temp = int(input[i]);
printf("%d\n",temp);
arraySum[j] = arraySum[j]*10 + temp;
}
}
else j++;
}
}
sumOfArray(arraySum);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int sumOfPara(void){
int v, sum = 0;
char ch;
while(1){
if(2 == fscanf(stdin, "%d %c", &v, &ch) && (ch == ',' || ch == ')')){
sum += v;
if(ch == ')')
break;
} else {
fprintf(stderr, "Parameter is invalid.\n");
exit(EXIT_FAILURE);
}
}
return sum;
}
int main(void){
char command[32];
char left_paren;
printf("user_account $> ");
if(2 != fscanf(stdin, "%31[a-z] %c", command, &left_paren) || strcmp(command, "sum") != 0 || left_paren != '('){
fprintf(stderr, "Please enter proper parameters : sum(num1, num2, ..)\n");
exit(EXIT_FAILURE);
} else { //rest
printf("%d\n", sumOfPara());
}
return 0;
}

Resources