I want the user to enter 3 names, and right after the program prints these 3 names
can someone tell me why is not printing anything???
I tried everything
if someone could explain it.....
there is no error, it simply exits after inserting the strings
#include <stdio.h>
#include <stdlib.h>
int main(){
int i, a, componentes;
char *nome;
componentes = 3;
nome = (char*) malloc(sizeof(char)*100);
printf("\n");
for(i = 0; i < componentes; i++){
// printf("String %d: ", i+1);
scanf("%s", &nome[i]);
}
printf("\n");
for(a = 0; a < componentes; a++){
printf("%s\n", nome[i]);
}
return 0;
}
I fixed the issues raised (plus free'ing the memory):
#include <stdio.h>
#include <stdlib.h>
#define N 3
#define LEN 100
int main() {
char *components[N];
for(int i = 0; i < N; i++) {
components[i] = malloc(LEN);
fgets(components[i], LEN, stdin);
}
for(int i = 0; i < N; i++) {
printf("%s", components[i]);
free(components[i]);
}
return 0;
}
You could also allocate the 300 bytes for the 3 strings via local variable on the stack, either with char components[N][LEN]; or as a single string that you index just to show you a different way:
#include <stdio.h>
#include <stdlib.h>
#define N 3
#define LEN 100
int main() {
char components[N * LEN];
for(int i = 0; i < N; i++) {
fgets(components + i * LEN, LEN, stdin);
}
for(int i = 0; i < N; i++) {
printf("%s", components + i * LEN);
}
return 0;
}
Related
My goal is to write a function, that calculates the number of all the unique characters from a redirected text file (meaning until EOF is reached). The code I wrote:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define ASCII_VALS 128
int strLen (char inp[])
{
int len = 0;
for(int i = 0; inp[i] != '\0'; i++){
len++;
}
return len;
}
int countUniqueChars (char inp[])
{
int everyCharValArr[ASCII_VALS] = {0};
int i, j = 0;
for(i = 0; i < strLen(inp); i++){
int convToInt = inp[i] - '0';
everyCharValArr[convToInt] = 1;
}
for (i = 0; i < ASCII_VALS; i++) {
j += everyCharValArr[i];
}
return j;
}
works for one string entered via scanf() like so:
int main ()
{
char inp[100];
printf("Enter a string: \n");
scanf("%99s", inp);
printf("%d\n", countUniqueChars(inp));
return 0;
}
But after I change the main function to read a redirected text file, like so:
int main ()
{
char inp[100];
int total = 0;
while(fgets(inp, 100, stdin)){
total += countUniqueChars(inp);
}
printf("%d\n", total);
return 0;
}
and runinng the program (./binary <input.txt) on a input.txt file with contents below:
Toydolls
Flies
trees
rocks
things
the value becomes 26, which is correct (1. word = 6 unique chars, 2. word = 5 unique chars, 3. word = 4 unique chars, 4. word = 5, 5. word = 6 unique chars), but it obviously does not take into consideration chars that appear on more lines, which should not be counted as unique chars at all. My question is How do I fix the function to accomplish this?
Try something like that: Note that I've added a mechanism not to count a duplicate uppercase letter and its lower case letter as unique.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#define ASCII_VALS 128
int everyCharValArr[ASCII_VALS] = {0};
int strLen (char inp[])
{
int len = 0;
for(int i = 0; inp[i] != '\0'; i++){
len++;
}
return len;
}
void FindUniqueChars (char inp[])
{
int i;
for(i = 0; i < strLen(inp); i++){
if (inp[i] > ' ' && inp[i] != (char)127)
{
if (inp[i] >= 'A' && inp[i] <='Z')
{
inp[i] = tolower(inp[i]);
}
everyCharValArr[(int)inp[i]] = 1;
}
}
}
int CountUniqueChars( void )
{
int i, j = 0;
for (i = 0; i < ASCII_VALS; i++) {
j += everyCharValArr[i];
}
return j;
}
int main ()
{
char inp[100];
while(fgets(inp, 100, stdin)){
FindUniqueChars(inp);
}
printf("%d\n", CountUniqueChars());
return 0;
}
I want to write a program where it duplicates every letter in a given string.
For example, if the input is abc then the output would be aabbcc.
How can I do this?
Here is my code so far. It only copies the string:
#include <stdio.h>
int main () {
char str_in[100];
char str_out[200] = "";
int i;
printf("Enter a word: ");
scanf("%s", str_in);
for (i = 0; i < sizeof(str_in); i++) {
str_out[i] += str_in[i];
}
printf("Duplicated word: %s", str_out);
return 0;
}
For starters the destination character array should be at least two times larger than the source array.
The loop that performs the copying can look the following way
size_t j = 0;
for ( size_t i = 0; str_in[i] != '\0'; i++ )
{
str_out[j++] = str_in[i];
str_out[j++] = str_in[i];
}
str_out[j] = '\0';
This is one way to do what you want - since you know that the two indexes in str_out that you want to access correspond to the ith and i+1th positions of str_in:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
int main(void)
{
char str_in[SIZE];
char str_out[SIZE*2];
int i, len;
printf("Enter a word: ");
scanf("%s", str_in);
len = strlen(str_in);
for (i = 0; i < len; i++) {
str_out[i*2] = str_in[i];
str_out[i*2+1] = str_in[i];
}
str_out[i*2] = '\0';
printf("Duplicated word: %s\n", str_out);
return EXIT_SUCCESS;
}
You could also use another variable and update inside the loop as such:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
int main(void)
{
char str_in[SIZE];
char str_out[SIZE*2];
int i, j, len;
printf("Enter a word: ");
scanf("%s", str_in);
len = strlen(str_in);
for (i = 0, j = 0; i < len; i++, j += 2) {
str_out[j] = str_in[i];
str_out[j+1] = str_in[i];
}
str_out[j] = '\0';
printf("Duplicated word: %s\n", str_out);
return EXIT_SUCCESS;
}
As a side note, it's best to #define your constants instead of having them in your code.
I would suggest only allocating the amount of memory you need:
char *str_out = malloc(strlen(str_in) + 1);
and then using another loop variable that counts by 2 each time so you have 2 times as much space for every character in your loop.
If you do that, you should also put + 1 next to the new loop variable to copy it to the current and next elements of the string, thereby duplicating the characters.
I would also advise you to use size_t for your loop variables to match the return of str_len.
Here I have implemented my suggestions:
size_t str_ctr;
size_t str_ctrx2;
size_t in_str_len = strlen(in_str)
/* ... */
for( str_ctrx2 = str_ctr = 0;
str_ctr < in_str_len;
str_ctr++, str_ctrx2 += 2 )
{
out_str[str_ctrx2] = in_str[str_ctr];
out_str[str_ctrx2 + 1] = in_str[str_ctr];
}
/* null-terminate the string */
out_str[str_ctrx2] = '\0';
I would also like to mention that you forget to null-terminate your string in your program. I have made a comment about that and have shown you how to do it above.
#include <stdio.h>
int main () {
char str_in[100];
char str_out[200] = "";
int i, j;
printf("Enter a word: ");
scanf("%s", str_in);
j=0;
for (i = 0; i < sizeof(str_in); i++) {
str_out[i] = str_in[j];
str_out[i+1] = str_in[j];
i++;
j++;
}
printf("Duplicated word: %s", str_out);
return 0;
}
I'm trying to execute this code (yes, with that two lines commented out), but every time I get a Segmentation Fault.
I can't understand why.
(linux, gcc)
#include <stdio.h>
#include <math.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *current;
while(strcmp("99999999zz", current) != 0)
{
for(int i = 0; i < pow(10, 10); i++)
{
sprintf(current, "%010d", i);
printf("%s\n", current);
for(int a = 97; a <= 122; a++)
{
for(int j = 0; j < 10; j++)
{
//current[j] = (char)a;
//printf("%s\n", current);
}
}
}
}
}
This code, instead, runs without problems:
#include <stdio.h>
#include <math.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *current;
while(strcmp("99999999zz", current) != 0)
{
for(int i = 0; i < pow(10, 10); i++)
{
sprintf(current, "%010d", i);
printf("%s\n", current);
}
}
}
You invoked undefined behavior in both programs by using value of uninitialized variable having automatic storage duration, which is indeterminate.
You should declare an array instead of a pointer and initialize it.
#include <stdio.h>
#include <math.h>
#include <string.h>
int main(int argc, char *argv[])
{
double limit = pow(10, 10); /* calculating this every time in the loop may cause loss of performance */
char current[128] = ""; /* allocate enough memory and initialize */
while(strcmp("99999999zz", current) != 0)
{
for(int i = 0; i < limit; i++)
{
sprintf(current, "%010d", i);
printf("%s\n", current);
for(int a = 97; a <= 122; a++)
{
for(int j = 0; j < 10; j++)
{
//current[j] = (char)a;
//printf("%s\n", current);
}
}
}
}
}
I need to write a program that will recieve (from the user) the number of players in a basketball team and then I need to create an array in a dynamic way. The program will sort the array in an alphabetic order and then print it like that aswell.
I wrote this code:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define LENGTH 20
int main(void)
{
int numplayers, i, j;
char persname[LENGTH], tname[LENGTH], temp[LENGTH];
printf("Please insert the amount of basketball players in the group\n");
scanf("%d", &numplayers);
char **players = (char **) malloc(numplayers * sizeof(char *));
printf("Please insert the names of your %d basketball players\n", numplayers);
for (i = 0; i < numplayers; i++) {
gets(persname);
strcpy(persname[i], tname[i]);
}
for (i = 0; i < numplayers-1; i++) {
for (j = i+1; j < numplayers; j++) {
if (strcmp(persname[i], persname[j])) {
strcpy(temp[i], persname[i]);
strcpy(persname[i], persname[j]);
strcpy(persname[j], temp);
}
}
}
for (i = 0; i < numplayers; i++) {
printf("%s\t\t%s\n", tname[i], persname[i]);
}
return 0;
}
But when I run the code, I get an error right after typing the amount of players in the team, Unhandled exception at 0x507340E3 (msvcr120d.dll) in Question4.exe: 0xC0000005: Access violation reading location 0xFFFFFFCC.
What did I do wrong.
Your loop that inputs all the names doesn't use players. Instead it uses pername and tname, incorrectly. This line:
strcpy(persname[i], tname[i]);
shouldn't compile, you're mixing up types in a way that just doesn't make any sense. You should input a line, then dynamically allocate new memory into players[i] and copy the input there. If you have strdup(), that's what it's good for.
Basically the input loop should be something like:
for (i = 0; i < numplayers; i++)
{
char line[1024];
if(fgets(line, sizeof line, stdin) != NULL)
{
const size_t len = strlen(line);
players[i] = malloc(len + 1);
if(players[i] == NULL)
{
fprintf(stderr, "**Out of memory!\n");
exit(1);
}
memcpy(players[i], line, len + 1);
}
else
fprintf(stderr, "**I/O error!\n");
}
This uses fgets() which is way safer than the horribly never-to-be-used gets() monster.
Also, you're not allocating any room for the individual names, just for the array of string pointers.
This line:
char** players = (char**)malloc(numplayers*sizeof(char*));
can be simplified to the much clearer:
char** players = malloc(numplayers * sizeof *players);
No need to repeat type names, and no need to cast the return value of malloc().
I managed to solve it! :)
Here is my updated code:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define LENGTH 20
#define LENGTH2 20
int main(void)
{
int numplayers, i, j;
char persname[LENGTH][LENGTH2], temp[LENGTH];
printf("Please insert the amount of basketball players in the group\n");
scanf("%d", &numplayers);
char** players = (char**)malloc(numplayers*sizeof(char));
printf("Please insert the names of your %d basketball players\n", numplayers);
for (i = 0; i < numplayers+1; i++)
{
gets(persname[i]);
}
for (i = 1; i < numplayers+1; i++)
{
for (j = 1; j < numplayers+1; j++)
{
if (strcmp(persname[j - 1], persname[j]) > 0)
{
strcpy(temp, persname[j - 1]);
strcpy(persname[j - 1], persname[j]);
strcpy(persname[j], temp);
}
}
}
printf("\nBasketball players names in order are : ");
for (i = 0; i < numplayers+1; i++)
{
puts(persname[i]);
}
getch();
free(players);
system("PAUSE");
return 0;
}
I have the following code:
#include "stdafx.h"
#include "string.h"
#include "ctype.h"
/*selection sort*/
void swap(int A[], int j, int k)
{
int p = A[k];
int i;
for (i = 0; i < (k - j); i++)
{
A[k - i] = A[k - i - 1];
}
A[j] = p;
}
/*greatest number in an array*/
int max(int A[], int N, int k)
{
int max = k, i;
for (i = k; i < N; i++)
{
if (A[max] < A[i])
max = i;
}
return max;
}
int count_nonspace(const char* str)
{
int count = 0;
while(*str)
{
if(!isspace(*str++))
count++;
}
return count;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[256];
int i = 0, j = 0, count[256] = { 0 };
char string[100] = "Hello world";
for (i = 0; i < 100; i++)
{
for (j = 0; j<256; j++)
{
if (tolower(string[i]) == (j))
{
count[j]++;
}
}
}
for (j = 0; j<256; j++)
{
printf("\n%c -> %d \n", j, count[j]);
}
}
Program is calculating the number of apperances of each character in a string. Now it prints the number of apperances of all 256 characters, whereas i want it to prinf only the character with greatest number of apperances in a string. My idea was to use the selection sort method to the array with the nubmer of apperances, but this is not working, thus my question is how to printf only the character with the greatest number of apperances in the string?
If anybody would have doubts, this is NOT my homework question.
EDIT: I've just noticed that this code printf apperances of characters begining with "j" why is that?
I started typing this before the others showed up, so I'll post it anyway. This is probably nearly the most efficient (increasing efficiency would add some clutter) way of getting an answer, but it doesn't include code to ignore spaces, count characters without regard to case, etc (easy modifications).
most_frequent(const char * str)
{
unsigned counts[256];
unsigned char * cur;
unsigned pos, max;
/* set all counts to zero */
memset(counts, 0, sizeof(counts));
/* count occurences of each character */
for (cur = (unsigned char *)str; *cur; ++cur)
++counts[*cur];
/* find most frequent character */
for (max = 0, pos = 1; pos < 256; ++pos)
if ( counts[pos] > counts[max] )
max = pos;
printf("Character %c occurs %u times.\n", max, counts[max]);
}
Create an array with your char as index.
Keep incrementing the value in the array based on the characters read.
Now get the max out of the array which gives you the most occurring char in your input.
Code will look like:
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
int main(void) {
char buf[100];
int i=0,max =0,t=0;
int a[256];
memset(a,0,sizeof(a));
fgets(buf,100,stdin);
buf[strlen(buf)-1] = '\0';
while(buf[i] != '\0')
{
a[(int)buf[i]]++;
i++;
}
i=0;
for(i=0;i<256;i++)
{
if(a[i] > max)
{
max = a[i];
t = i;
}
}
printf("The most occurring character is %c: Times: %d",t,max);
return 0;
}
Here is a solution for that, based on your own solution, and using qsort().
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
struct Frequency
{
int character;
int count;
};
int compare(const void *const lhs, const void *const rhs)
{
return ((struct Frequency *)rhs)->count - ((struct Frequency *)lhs)->count;
}
int main(int argc, char* argv[])
{
int i = 0, j = 0;
struct Frequency count[256];
memset(&count, 0, sizeof(count));
char string[100] = "Hello world";
for (i = 0 ; i < 100 ; i++)
{
for (j = 0 ; j < 256 ; j++)
{
count[j].character = j;
if (tolower(string[i]) == j)
{
count[j].count += 1;
}
}
}
qsort(count, sizeof(count) / sizeof(*count), sizeof(*count), compare);
/* skip the '\0' which was counted many times */
if (isprint(count[1].character))
printf("\nThe most popular character is: %c\n", count[1].character);
else
printf("\nThe most popular character is: \\%03x\n", count[1].character);
for (j = 0 ; j < 256 ; j++)
{
if (isprint(count[j].character))
printf("\n%c -> %d \n", count[j].character, count[j].count);
else
printf("\n\\%03x -> %d \n", count[j].character, count[j].count);
}
}
notice that the '\0' is set for all the remainig bytes in
char string[100] = "Hello world";
so the count of '\0' will be the highest.
You could use strlen() to skip '\0', in the counting loop, but don't
for (i = 0 ; i < strlen(string) ; ++i) ...
do it this way
size_t length = strlen(string);
for (i = 0 ; i < length ; ++i) ...