c - compiler, different errors? - c

i'm running this code on a macbook with xCode, and it runs flawlessly without any errors. However, when I try to compile and run the exact code on a Red Hat Linux server, I get a segmentation fault at line 25. I'm not sure why this is working for one machine, and not the other.
#include <stdio.h>
#include <string.h>
//Count the number of times the letter appears in the string
int count_letter(char str[], char * ch)
{
int i, num = 0;
for(i=0; i < strlen(str)-1; i++)
if(str[i] == *ch)
{
//printf("The character is %c", ch);
num++;
}
return num;
}
//Get the sentence and character to search for from the user
char * get_info(char * ch)
{
char *str;
//int i=0;
printf("Enter a sentence to search: ");
while((str[i++]=getchar())!='\n');
str[i]='\0';
printf("Enter a character to search for: ");
*ch=getchar();
return str;
}
//Get a sentence and character from the user and count the number
//of times the character is found in the sentence.
int main()
{
char *sentence;
char ch;
int num_letters;
sentence = get_info(&ch);
num_letters = count_letter(sentence, &ch);
printf("%c is found %d times.\n", ch, num_letters);
return 0;
}
Thank you

char *str;
You have not allocated any memory for it in get_info().
So first allocate memory for it.like
str= (char *)malloc(MAX_LEN*sizeof(char)); //MAX_LEN=define yourself what you want

Related

how to see if a letter char *c exist in word char s[30]?

here is code and i need help to find the place of letter.The strcmp is not working and i dont now where is the proble to fix.
#include <string.h>
int main(void)
{
char s[30]="fiordi";
char *c;
int cp,i,place;
printf("Enter char: ");
scanf("%s",&c);
for(i=0; i<6; i++){
cp=strcmp(s[i],c);
if( cp == 0 ){
place=i;
}
}
printf("the place is :%d",place);
}
#include <string.h>
#include <stdio.h>
int main(void)
{
char s[30]="fiordi";
size_t s_len = strlen(s);
int place, c;
printf("Enter char: ");
scanf("%c",&c);
for(place = 0; place < s_len; place++)
if(s[place] == (char)c)
break;
if(place == s_len)
printf("\nchar not found in string\n");
else
printf("the place of char in \"%s\" is in position %d", s, place);
}
strcmp compares strings, not characters. Without going into the details of why it doesn't work, you could just compare the characters directly:
if(s[i] == c[0] ){
place=i;
}

program to reverse string skipping some characters while printing

It prints the characters in reverse order just fine except when the string is 8 characters long.
Eg -
"what man" gives "am tahw" Why ?
whereas "what many" gives "ynam tahw" just as it should.
#include <stdio.h>
int main(void)
{
char a[100];
char x;
char*i = a;
printf("Enter a message:");
while ((x = getchar()) != '\n')
{
*i = x;
i++;
}
while (i >= &a[0])
{
printf("%c", *i--);
}
printf ("\n");
}
Modification in your code:
Change printf("%c", *i--); to printf("%c", *--i); in while loop.
You can improve the quality of your program as shown below:
#include <stdio.h>
#include <conio.h>
void main()
{
char *s;
int len,i;
clrscr();
printf("\nENTER A STRING: ");
gets(s);
len=strlen(s);
printf("\nTHE REVERSE OF THE STRING IS:");
for(i=len;i>=0;i--)
printf("%c",*(s+i));
getch();
}

Store & display user input in an array in C

I am trying to display and print a word from the user and store it into my array which is called
char word[20]
But I am having trouble. I know we use a "for loop" to scan it into the array but I keep going in circles and I believe the problem is with the i < 20.
I researched this and found that the answers to this are extremely experienced and I need a really basic way of doing it without the extra stuff. So all I want is to get word from the user, store it and print it onto the screen.
Can someone help without experienced code?
Code in C
char getWord(char word[]);
int main()
{
char word[20];
getWord(word);
return 0;
}
char getWord(char word[])
{
int i;
printf("Enter a word: ");
for (i = 0; i < 20; i++)
{
scanf(" %c", &word[i]);
}
return word;
}
All you want is
#include <stdio.h>
int main() {
char word[20];
scanf("%s", word); // Read and store
printf("%s\n", word); // Print
return 0;
}
You can use fgets and puts to read and write a string.
#include<stdio.h>
#define MAX 20
int main()
{
int ar[MAX], i, count;
fgets(ar, MAX, stdin); //it will accept whitespaces as well
puts(ar); //displaying entered string
return;
}
if you want to read via characters, ending character should be set to null character for it to be string.
char getWord(char word[]);
int main()
{
char word[20]
getWord(word);
printf("%s\n", word);
return 0;
}
char getWord(char word[])
{
int i;
char c;
printf("Enter a word: ");
for (i = 0; i < 19; i++)
{
scanf("%c", &c);
if ( c == '\n' )
break;
word[i]=c;
}
word[i]='\0';
return word;
}

How to count the amount of times a specific character appears in an array in C?

So I am trying to count the amount of times a specific character occurs in my program. For example, If I entered, ABCDA, I want the program to print, "There are 2 A's." My code is as follows:
int main(void)
{
char array[10000];
printf("Enter input: \n");
scanf("%s", array);
printf("Array entered is: %s\n", array);
char A; //variable I want to count
char *k //used just to loop
int a_counter; //number of times A, occurs
fgets(array, sizeof(array), stdin);
A = fgetc(stdin);
a_counter = 0;
for(k = array; *k; k++)
{
if (*k == A)
{
a_counter++;
}
}
printf("Number of A's: %d\n", a_counter);
return 0;
}
The following loop was found on another forum which also attempted to count the a specific character but I can not seem get mine to work. Is my approach at this wrong? I also would like to get this all out of main but I am also confused on how to do so. I appreciate any help that is given. Thank you.
New Attempt at the count loop that is also not working. I got rid of fgets because it was confusing me.
int a_counter = 0;
if (array == 'A')
{
a_counter++;
}
printf("Number of A's: %d\n", a_counter);
Attempt after #bjorn help.
#include <stdio.h>
int main(void)
{
char array[1000];
printf("Enter input: \n);
scanf("%s", array);
printf("Input is: %s\n", array);
int c,n =0;
while((c = getchar()) != EOF)
if (c = 'A')
n++;
printf("Amount of A's is: %d\n", n);
return 0;
}
KISM - Keep it simple, mate :) Your code is way too complicated for a simple task. Here's an alternative, which hopefully illustrates what I mean:
#include <stdio.h>
int main(void)
{
int c, n = 0;
while ((c = getchar()) != EOF)
if (c == 'a')
n++;
printf("Where were %d a characters\n", n);
return 0;
}

Count all character occurrences in a text file

The following code snippet is intended to count all the symbols met in a file after text is entered, next step is counting the occurrences of all characters (For instance 'a' met 3 times, 'b' 0 times etc.). However when I compile the loop goes infinite and the counting is always 0. My question is if it could be fixed or rewritten in another way.
char type, c, text[100]; counts[100];
int count=0, i;
while((type=getchar())!=EOF) {
fputc(type, f); count++;
}
printf("Symbols found: %d", count-1);
rewind(f);
while(fscanf(f, "%s", &text)) {
for (i = 0; i < strlen(text); i++) {
counts[(text[i])]++;
printf("The %d. character has %d occurrences.\n", i, counts[i]);
}
}
You can build your histogram as you read the input. The return value from getchar() is an int, not a char, since it has to represent EOF in addition to the 256 char values. Once the histogram has been built, you can iterate over the buckets and print them. Here, I have assumed that all 256 char values are possible, and included code to display unprintable characters in hex notation.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(int argc, char **argv)
{
int c;
int i;
int histogram[256];
int total;
memset(histogram, 0, sizeof(histogram));
total = 0;
while ((c = getchar()) != EOF) {
histogram[c]++;
total++;
}
printf("Symbols found: %d\n", total);
for (i = 0; i < 256; i++) {
if (histogram[i]) {
char repr[5];
sprintf(repr, isprint(i) ? "%c" : "\\x%02x", i);
printf("The '%s'. character has %d occurrences.\n", repr, histogram[i]);
}
}
return 0;
}
Your for loop scans the string with variable i being an index to the character tested, but your printf says i is a symbol accounted.
You should separate counting and printing results:
char * ptr;
while(fscanf(f, "%s", text))
for (ptr = text; * ptr != 0; ptr++)
counts[ (unsigned char)*ptr ]++;
for( i = 0; i < 256; i++)
printf("The %d. character has %d occurrences.\n", i, counts[i]);
Don't forget to declare count[ 256] and note that scanf gets text, not `&text~as a destination.

Resources