My program is supposed to count in a string an entered string.
For example:
Enter a text: Hello World
Enter a string: llo
occurs 1 times in Hello World
Unfortunately, my program does not give me anything.
Does anyone have a tip for me?
void aufgabe7(char string[MAX], char stringw[MAX]){
printf("Enter a Text: \n");
fgets(string, MAX, stdin);
int i, j, len, len2;
int count = 0;
int count2 = 0;
len = strlen(string);
printf("Enter a string: \n");
fgets(stringw, MAX, stdin);
len2 = strlen(stringw);
for (i = 0; i < len;)
{
j = 0;
count = 0;
while ((string[i] == stringw[j]))
{
count++;
i++;
j++;
}
if (count == len2)
{
count2++;
count = 0;
}
else
i++;
}
printf("%s occurs %d times in %s \n", stringw, count2, string);
}
void main (void){
char data[MAX];
task7(&data[0], &data[0]);
}
As commenter #Steve Summit noted, you are searching for a string read from fgets, which has a trailing newline. Normally, you would need to remove the newline from the input from fgets, otherwise it is part of the searched for string. The simplest way to do this is to call buffer[strcspn(buffer, "\n")] = '\0'; as documented in this question.
However, I feel the correct answer to this question is that there is a more idiomatic way of doing this with C than comparing char by char: using strstr with pointer arithmetic will work just as well and is cleaner to read:
char input[SIZE];
char search[SIZE];
size_t count = 0;
printf("Enter a phrase: ");
fgets(input, SIZE, stdin);
input[strcspn(input, "\n")] = '\0';
printf("\nEnter a sub-phrase: ");
fgets(search, SIZE, stdin);
search[strcspn(search, "\n")] = '\0';
char *p = input;
while((p = strstr(p, search)) != NULL)
{
printf("Found occurrence of '%s' at position %td\n", search, (ptrdiff_t)(p - input));
count++;
p++;
}
printf("Found %zu occurrences of '%s'.\n", count, search);
Related
This is the code I am not able to fix it. It works fine if 5 or more characters are used but using less than 4 characters breaks it.
#include <stdio.h>
void main()
{
char str[1000], rev[1000];
int i, j, count = 0;
printf("Enter the string :");
scanf("%s", str);
printf("\nString Before Reverse: %s", str);
while (str[count] != '\0')
{
count++;
}
j = count-1 ;
for (i = 0; i<count; i++)
{
rev[i] = str[j];
j--;
}
printf("\nString After Reverse: %s", rev);
}
Your have to null terminate your rev string like:
rev[count] = '\0';
Also, please make your code more readable.
When I try to run the scanf inside of the if function after entering either 'a' or 'b', it immediately runs and exits the program without getting input. Is there a way to fix it so the scanf works inside of the if and else if functions?
#include <stdio.h>
#include <string.h>
int top=-1;
int word;
char stack_string[100];
void push(char word);
char pop(void);
int main()
{
char input;
char str[100];
int i;
printf("press [a] for palindrome check, [b] for string reversal:");
scanf("%c", &input);
if (input == 'b'){
printf("Input a string: ");
scanf("%[^\n]s", str);
for(i=0;i<strlen(str);i++)
push(str[i]);
for(i=0;i<strlen(str);i++)
str[i]=pop();
printf("Reversed String is: %s\n",str);
}
else if(input == 'a'){
char str[100];
int count = 0;
printf("Input a string: ");
scanf("%[^\n]s", str);
for (i = 0; i < strlen(str); i++)
{
push(str[i]);
}
for (i = 0; i < strlen(str); i++)
{
if (str[i]==pop())
count++;
}
if (count == strlen(str))
printf("%s is a palindrome\n", str);
else
printf("%s is not a palindrome\n", str);
}
return 0;
}
void push(char word)
{
top=top+1;
stack_string[top]=word;
}
char pop()
{
word = stack_string[top];
top=top-1;
return word;
}
Use this:
scanf(" %[^\n]s", str);
^^^
Whitespace
instead of
scanf("%[^\n]s", str);
For more information, refer this link.
Stop using scanf(). The terminal sends your application a full line at a time, but scanf() only consumes the parts it needs, i.e. one character in the case of %c or just all the digits in case of %d. The rest is left in the input buffer, where the following scanf() get them. That causes the user's input and what the program receives to be somewhat "out of sync".
It's better to read full lines at a time, with fgets() or getline(), and then parse whatever you want from them. E.g.:
#define LINELEN 255
char line[LINELEN];
fgets(line, LINELEN, stdin);
int num;
int a = sscanf(line, "%d", &num);
Use whatever format string you require on the sscanf() and put the whole thing in a function if you like. (LINELEN there is of course an arbitrary
limit, but fgets() requires some limit.)
See also: http://c-faq.com/stdio/scanfprobs.html and the longer explanation linked there.
use scanf like this: scanf("%s", str)
Only scanf statements need to be changed:
#include <stdio.h>
#include <string.h>
int top=-1;
int word;
char stack_string[100];
void push(char word);
char pop(void);
int main()
{
char input;
char str[100];
int i;
printf("press [a] for palindrome check, [b] for string reversal:");
scanf("%c", &input);
if (input == 'b'){
printf("Input a string:\n");
scanf("%s", str);
for(i=0;i<strlen(str);i++)
push(str[i]);
for(i=0;i<strlen(str);i++)
str[i]=pop();
printf("Reversed String is: %s\n",str);
}
else if(input == 'a'){
char str[100];
int count = 0;
printf("Input a string:\n");
scanf("%s", str);
for (i = 0; i < strlen(str); i++)
{
push(str[i]);
}
for (i = 0; i < strlen(str); i++)
{
if (str[i]==pop())
count++;
}
if (count == strlen(str))
printf("%s is a palindrome\n", str);
else
printf("%s is not a palindrome\n", str);
}
return 0;
}
void push(char word)
{
top=top+1;
stack_string[top]=word;
}
char pop()
{
word = stack_string[top];
top=top-1;
return word;
}
For the most part everything is working and it's able to build error free. However when I try to run it, it seems to crash. To be more specific, whenever I start my code and press 'a' or 'b', the program keeps crashing. Can someone tell me how to fix that?
#include <stdio.h>
#include <string.h>
int top=-1;
int word;
char stack_string[100];
void push(char word);
char pop(void);
int main()
{
char input;
char str[100];
int i;
printf("press [a] for palindrome check, [b] for string reversal:");
scanf("%s", input);
if (input == 'b'){
printf("Input a string: ");
scanf("%[^\n]s", str);
for(i=0;i<strlen(str);i++)
push(str[i]);
for(i=0;i<strlen(str);i++)
str[i]=pop();
printf("Reversed String is: %s\n",str);
}
else if(input == 'a'){
char str[100];
int count = 0;
printf("Input a string: ");
scanf("%[^\n]s", str);
for (i = 0; i < strlen(str); i++)
{
push(str[i]);
}
for (i = 0; i < strlen(str); i++)
{
if (str[i]==pop())
count++;
}
if (count == strlen(str))
printf("%s is a palindrome\n", str);
else
printf("%s is not a palindrome\n", str);
}
return 0;
}
void push(char word)
{
top=top+1;
stack_string[top]=word;
}
char pop()
{
word = stack_string[top];
top=top-1;
return word;
}
the main problem is input stream
for single char input in scanf
we use scanf( "%c", &input);
but for whole non-space array of characters / string we use
scanf( " %s", str);
for string contains whitespace except new line
gets_s( str, sizeof( str ) ) ;
for your case
correct the following code
scanf( "%s", input);
to
scanf( "%c", &input);
Happy Coding
main problem is you are reading a string but comparing it to a single char
that's why if condition is not working
just convert
scanf( "%s", input);
to
scanf( "%c", &input);
that will be enough
The problem is: you are getting the input with scanf("%s", input);
therefore the condition input == 'a' is the same as "a" == 'a' which never meets
you need to get the input as a char using
scanf("%c", &input);
not
scanf("%s", input);
Actually recently i found a problem where i need to count occurrence a given(by oj) char in a given string(with test case).So i write this code but output is not as i desired.I'm a beginner so i'll be greatly thankful for any kind of instructive advice or help.THANK YOU.
#include<stdio.h>
#include<string.h>
int main ()
{
int ara [123];
char s[1000];
int l, j, i, len;
char c;
scanf ("%d\n", &l);
while (l >= 0){
for (i = 65; i <= 122; i++)
{
ara[i] = 0;
}
fgets(s, 1000, stdin);
len = strlen(s);
for (i = 0;i <= len; i++)
{
ara[s[i]]++;
}
scanf(" %c\n", &c);
j = c;
printf("count : %d\n", ara[j]);
l--;
}
return 0;
}
The problem is that scanf is leaving a newline in the input to be read as the target sentence.
You can get around this by using fgets and sscanf instead. I also added some cues to make it easier to know what is expected.
#include <stdio.h>
#include <string.h>
int main (void)
{
int ara [128]; // increased array size
char s[1000];
char t[10];
int l, j, i, len;
char c;
printf("Enter how many loops:\n");
fgets(t, sizeof t, stdin); // replace scanf
sscanf (t, "%d\n", &l); // with sscanf
while (l > 0){ // changed end test
for (i = 'A'; i <= 'z'; i++) // replaced magic numbers
{
ara[i] = 0;
}
printf("Enter the string:\n");
fgets(s, sizeof s, stdin);
len = strlen(s);
for (i = 0;i <= len; i++)
{
ara[s[i]]++;
}
printf("Enter the letter:\n");
fgets(t, sizeof t, stdin); // replace scanf
sscanf (t, "%c\n", &c); // with sscanf
j = c;
printf("count : %d\n", ara[j]);
l--;
}
return 0;
}
Program session
Enter how many loops:
2
Enter the string:
one two three four
Enter the letter:
o
count : 3
Enter the string:
three seven
Enter the letter:
e
count : 4
Note that ideally, you should also check the function return values from fgets and sscanf.
This program need to read two strings, this two strings will be passed to the "confirm" function, they will be read and the the function will have to find a word in common.
But in the main i cant read the "string2" string! No matter how many getchar i insert.
#include <stdio.h>
#include <string.h>
void confirm(char string1[],char string2[]){
char word[20];
char w_aux[20];
int i, j, k, l, m;
int len1, len2;
int find;
i = 0;
len1 = strlen(string1);
len2 = strlen(string2);
while (i < len1){
j = 0;
while ((string1[i] != ' ') && (i < len1)){
word[j] = string1[i];
i++;
j++;
}
word[j] = '\0';
k = 0;
find = 0;
while((k < len2) && (find == 0)){
l = 0;
while ((string2[k] != ' ') && (k < len2)){
w_aux[l] = string2[k];
k++;
l++;
}
w_aux[l] = '\0';
if (j == l){
m = 0;
while((word[m] == w_aux[m]) && (m<j)){
m++;
}
if (m == j){
find = 1;
}
}
k++;
}
i++;
}
printf("%s\n", word);
}
int main (){
char string1[20];
char string2[20];
printf("Type the first text: \n");
scanf("%[^\n]s", string1);
printf("Type the second text: \n");
scanf("%[^\n]s", string2);
getchar();
confirm(string1, string2);
return 0;
}
Use getchar after input of string 1 for \n:
printf("Type the first text: \n");
scanf("%[^\n]", string1);
getchar(); //here
printf("Type the second text: \n");
scanf("%[^\n]", string2);
Just change the format specifier to "%19[^\n]*c", it will take the trailing '\n' character away.