I'm trying to count chars from input, and I noticed that while(getchar()!=EOF) produces an extra count, Is it because it counts the null-terminated from input?
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define LINE 5
#define MEM_SIZE 10
char *getInput(int *counter);
void printInput(char *input);
int main() {
char *mainInput;
int counter = 0;
printf("Please enter your input:\n");
mainInput = getInput(&counter);
if (mainInput == NULL) {
return 1;
}
printf("\n\n\nOutput:\n%s\n", mainInput);
printf("number of chars: %d\n", counter);
printInput(mainInput);
free(mainInput);
return 0;
}
char *getInput(int *counter) {
char *p = (char *)malloc(MEM_SIZE * sizeof(char));
char *q = p; /* backup pointer, if realloc fails to allocate, function will return last address values stored */
int c;
int temp_counter = 0;
long current = 0, last = MEM_SIZE - 1;
while ((c = getchar()) != EOF) {
if (current >= last) {
q = p;
p = (char *)realloc(q, last + (MEM_SIZE * sizeof(char)));
if (p == NULL) {
printf("Memory allocation failed, printing only stored values \n");
return q;
}
last += MEM_SIZE;
}
p[current] = c;
temp_counter++;
printf("number of chars: %d\n", temp_counter);
++current;
}
p[current] = '\0';
(*counter) = temp_counter - 1;
return p;
}
void printInput(char *input) {
int i, j = 0;
while (input[j] != '\0') {
for (i = 0; i < LINE; i++) {
if (input[j] == '\0')
break;
putchar(input[j]);
++j;
}
if (input[j] != '\0')
putchar('\n');
}
}
Related
Basically I'm trying to make QuizMaker by asking user how many questions they want, then make a string of an array. I'm new to C, so I might be missing some little details. Please look at the screenshot of console which I included.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 4096
int input(char *str, int n);
int main() {
int a, i, length = 0;
printf("How many questions do you want?\n");
scanf("%d", &a);
char *strarr[a];
char buffer[BUFFER_SIZE]; //string holder
while (getchar() != '\n'); //in case there is \n, flush it
for(i = 0; i < a; i++) {
printf("Question number #%d:\n", i + 1);
length = input(buffer, BUFFER_SIZE);
//input method returns number of chars we've entered
strarr[i] = malloc((length) * sizeof(char));
//allocating memory for each pointers to array of chars
strcpy(strarr[i], buffer);
//copy the string you've just created to an array of strings
}
//printing results
printf("_____________\n");
for(i = 0; i < a; i++) {
printf("%s\n", strarr[i]);
}
return 0;
}
int input(char *str, int n) {
int ch, i = 0;
while ((ch = getchar()) != '\n') {
if (i < n)
str[i++] = ch;
str[i] = '\0';
}
return i;
}
There are some problems in the code:
the test if (i < n) is not strict enough: you should stop storing characters to the destination array before it is full to save space for the null terminator.
you must allocate one extra byte for the null terminator: malloc((length + 1) * sizeof(char)) or just malloc(length + 1) as sizeof(char) is 1 by definition.
you should test for EOF in addition to '\n' in the reading loops.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 4096
int input(char *str, int n);
int main() {
int a, c, i, length = 0;
printf("How many questions do you want?\n");
if (scanf("%d", &a) != 1 || a <= 0) {
fprintf(stderr, "invalid input\n");
return 1;
}
char *strarr[a];
char buffer[BUFFER_SIZE]; //string holder
// flush the rest of the input line
while ((c = getchar()) != EOF && c != '\n')
continue;
for (i = 0; i < a; i++) {
printf("Question number #%d:\n", i + 1);
//input method returns number of chars we've entered
length = input(buffer, BUFFER_SIZE);
//allocating memory for each pointers to array of chars
strarr[i] = malloc((length + 1) * sizeof(char));
if (strarr[i] == NULL) {
fprintf(stderr, "allocation error\n");
a = i;
break;
}
//copy the string you've just created to an array of strings
strcpy(strarr[i], buffer);
}
//printing results
printf("_____________\n");
for (i = 0; i < a; i++) {
printf("%s\n", strarr[i]);
}
return 0;
}
int input(char *str, int n) {
int ch, i = 0;
while ((ch = getchar()) != EOF && ch != '\n') {
if (i + 1 < n)
str[i++] = ch;
}
if (i < n)
str[i] = '\0';
return i;
}
Also note that you can use strdup() to allocate and copy the string in a single call:
for (i = 0; i < a; i++) {
printf("Question number #%d:\n", i + 1);
input(buffer, BUFFER_SIZE);
/* allocate a copy of the string */
strarr[i] = strdup(buffer);
if (strarr[i] == NULL) {
fprintf(stderr, "allocation error\n");
a = i;
break;
}
}
I'm stuck on an assignment I have to read from console really long number and then print it out using char* arr. Then I need to add and subtract number 1 array to number2 array. To be honest adding and subtracting I will probably deal on my own but I cannot figure out how to read those input characters, character by character and make while break after enter in console.
My code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int subtract(const char* number1, const char* number2, char** result){
if(number1 == NULL || number2 == NULL){
return 1;
}
return 0;
}
int add(const char* number1, const char* number2, char** result) {
if(number1 == NULL || number2 == NULL){
return 1;
}
return 0;
}
int input_check(int check, char* number) {
if (check != 1) {
return 1;
}
else {
return 0;
}
}
int main()
{
char* number1;
//char* number2;
//char** result;
int check = 0;
number1 = (char*)calloc(200,sizeof(char));
//number2 = (char*)calloc(200, sizeof(char));
//result = (char*)malloc(sizeof(char) * sizeof(char) * 400);
if (number1 == NULL) {
printf("Failed to allocate memory");
return 8;
}
printf("Input first num: ");
int i = 0;
while (1) {
char retVal;
scanf("%c", &retVal);
if (retVal >= 48 || retVal <= 57 || retVal != '\0') {
*(number1 + i) = retVal;
if ((number1 + i) == NULL) {
break;
}
printf("%d", atoi((number1 + i)));
i++;
}
else
{
break;
}
}
return 0;
}
Thanks for any help
As there is no limit on the numbers, you need to use dynamic memory allocation.
The straightforward (brute-force) way is to keep increasing the allocated size
char *input = calloc(1, 1); // space for '\0'
size_t len = 0;
for (;;) {
int ch = getchar();
if (ch != '\n') {
input[len] = ch; // replace '\0' with ch
len++;
char *tmp = realloc(input, len + 1);
if (tmp == NULL) exit(EXIT_FAILURE);
input = tmp;
input[len] = 0; // add '\0'
} else {
break;
}
}
// use input and len
free(input);
I want to fill an array of char pointers in the following format:
[name, number of email addresses, all the email addresses]*number of people. The number of mail adresses for each person isn't known at first.
I have to use char *contacts[N]
When printing the array it only prints contacts[0], so I guess my way of scanning the input is wrong
This is my main so far:
int main()
{
char *contacts[N];
int nextAvailable = 0;
int * const nextAvailableP = &nextAvailable;
add(contacts, nextAvailableP);
//Printing this way will only print contacts[0]
int i;
for (i = 0; i < nextAvailable; i++) {
printf("%s", contacts[i]);
}
}
This is the add function:
int add(char *contacts[], int *nextAvailableP) {
if (*nextAvailableP > 97) {
printf("Not enough memory\n");
return FALSE;
}
char tempName[50];
char tempMail[50];
int numberOfMails = 1;
printf("Enter your name\n");
fgets(tempName, 50, stdin);
if ((strlen(tempName) > 0) && (tempName[strlen(tempName) - 1] == '\n'))
tempName[strlen(tempName) - 1] = '\0';
contacts[*nextAvailableP] = (char *)malloc((strlen(tempName) + 1));
if (contacts[*nextAvailableP] == NULL) {
printf("Not enough memory\n");
return FALSE;
} else {
strcpy(contacts[*nextAvailableP], tempName);
}
(*nextAvailableP)++;
int numberOfMailsIndex = *nextAvailableP;
itoa(numberOfMails, contacts[numberOfMailsIndex], 10);
(*nextAvailableP)++;
printf("Enter your mail/s, use enter to enter a mail and '-1' to signal you finished\n");
fgets(tempMail, 50, stdin);
while (strcmp(tempMail, "-1") != 0) {
if ((strlen(tempMail) > 0) && (tempMail[strlen(tempMail) - 1] == '\n')) {
tempMail[strlen(tempMail) - 1] = '\0';
}
contacts[*nextAvailableP] = (char *)malloc((strlen(tempMail) + 1));
if (contacts[*nextAvailableP] == NULL) {
printf("Not enough memory");
return FALSE;
} else {
strcpy(contacts[*nextAvailableP], tempMail);
}
(*nextAvailableP)++;
numberOfMails++;
itoa(numberOfMails, contacts[numberOfMailsIndex], 10);
fgets(tempMail, 50, stdin);
}
}
I thought I was initializing each cell in contacts to the requested size and then copying the word I scan from the user into it - but obviously I'm not. Should I iterate through each memory I've allocated char by char?
Btw, I know that the casting to *char isn't necessary
So this would be your corrected code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_ENTRIES 50
int add(char *contacts[], int *nextAvailable);
int main(void){
int i;
char *contacts[MAX_ENTRIES];
int nextAvailable = 0;
add(contacts, &nextAvailable);
add(contacts, &nextAvailable);
for(i=0; i < nextAvailable ; i++){
printf("%s\n", contacts[i]);
}
}
char* intToString(int val, int base){
//source: www.strudel.org.uk/itoa/
static char buf[32] = {0};
int i = 30;
for(; val && i ; --i, val /= base)
buf[i] = "0123456789abcdef"[val % base];
return &buf[i+1];
}
int add(char *contacts[], int *nextAvailableP) {
if (*nextAvailableP > 97) {
printf("Not enough memory\n");
return 0;
}
char tempName[50];
char tempMail[50];
int numberOfMails = 1;
printf("Enter your name\n");
fgets(tempName, 50, stdin);
if ((strlen(tempName) > 0) && (tempName[strlen(tempName) - 1] == '\n'))
tempName[strlen(tempName) - 1] = '\0';
contacts[*nextAvailableP] = (char *)malloc((strlen(tempName) + 1));
if (contacts[*nextAvailableP] == NULL) {
printf("Not enough memory\n");
return 0;
} else {
strcpy(contacts[*nextAvailableP], tempName);
}
(*nextAvailableP)++;
int numberOfMailsIndex = (*nextAvailableP);
contacts[numberOfMailsIndex] = malloc(5);
(*nextAvailableP)++;
printf("Enter your mail/s, use enter to enter a mail and '-1' to signal you finished\n");
fgets(tempMail, 50, stdin);
while (strcmp(tempMail, "-1\n") != 0) {
if ((strlen(tempMail) > 0) && (tempMail[strlen(tempMail) - 1] == '\n')) {
tempMail[strlen(tempMail) - 1] = '\0';
}
contacts[*nextAvailableP] = (char *)malloc((strlen(tempMail) + 1));
if (contacts[*nextAvailableP] == NULL) {
printf("Not enough memory");
return 0;
} else {
strcpy(contacts[*nextAvailableP], tempMail);
}
(*nextAvailableP)++;
numberOfMails++;
fgets(tempMail, 50, stdin);
}
strcpy(contacts[numberOfMailsIndex], intToString(numberOfMails - 1,10));
}
Your function add could be cut in a subfunction which gets a string from the user. This string will be stored in a allocated memory space which has the size of string.
Keep in mind : It's not a good practice to mix data of different nature in one array (names and email). It would be better to use structs (as #Barmar said).
I would do following steps:
Get name from user (char *)
Allocate memory and copy the name into it.
insert the pointer to this allocated memory into your array (contacts)
Get a email address put it in a dynamic allocated memory space
add its pointer to your array (Contacts
Increment emails counter
repeat
-1 detected
convert your email counter to string and the pointer into your array
Anyway here's a code that you can start play with:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_ENTRIES 50
#define BUFFERSIZE 50
int add(char *contacts[], int *nextAvailable);
char *getString(char *queryText);
char* intToString(int val, int base);
int main(void){
int i;
char *contacts[MAX_ENTRIES];
int nextAvailable = 0;
add(contacts, &nextAvailable);
add(contacts, &nextAvailable);
for(i=0; i < nextAvailable ; i++){
printf("%s", contacts[i]);
}
}
int add(char *contacts[], int *nextAvailable){
if(*nextAvailable > MAX_ENTRIES - 1){
printf("Not enough memory\n");
return 1;
}
contacts[*nextAvailable] = getString("Enter your name\n");
if(contacts[*nextAvailable] == NULL){
printf("Malloc Error\n");
return 1;
}
++(*nextAvailable);
int counterfield = *nextAvailable;
++(*nextAvailable);
int emailCounter = 0;
while(1){
if(*nextAvailable > MAX_ENTRIES - 1){
printf("Not enough memory\n");
return 1;
}
char *email = getString("Enter email\n");
if(strcmp(email, "-1\n") == 0){
contacts[counterfield] = malloc(5);
contacts[counterfield] = intToString(emailCounter, 10);
return 0;
}
emailCounter++;
contacts[*nextAvailable] = email;
++(*nextAvailable);
}
}
char *getString(char *queryText){
char buffer[BUFFERSIZE];
char *ret;
printf("%s", queryText);
fgets(buffer, BUFFERSIZE, stdin);
ret = malloc(strlen(buffer));
strcpy(ret, buffer);
return ret;
}
char* intToString(int val, int base){
//source: www.strudel.org.uk/itoa/
static char buf[32] = {0};
int i = 30;
for(; val && i ; --i, val /= base)
buf[i] = "0123456789abcdef"[val % base];
return &buf[i+1];
}
I have problem with my alignement. This time I want my program to return words that ends and starts with the same letter. I've wrote something like this, but it seems to return random words.
#include <stdio.h>
#include <string.h>
void main()
{
char str[100];
int i, t, j, len;
printf("Enter a string : ");
scanf("%[^\n]s", str);
len = strlen(str);
str[len] = ' ';
for (t = 0, i = 0; i < strlen(str); i++)
{
if ((str[i] == ' ') && (str[i - 1] == str[0]))
{
for (j = t; j < i; j++)
printf("%c", str[j]);
t = i + 1;
printf("\n");
}
else
{
if (str[i] == ' ')
{
t = i + 1;
}
}
}
}
You can use strtok to split the strings from stdin, then apply a letter checker on each parsed word one at a time.
Something like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXCHAR 100
int is_start_end(char *word);
void exit_if_null(void *ptr, const char *msg);
int
main(void) {
char str[MAXCHAR];
char *word;
char **all_words;
int words_size = 1, word_count = 0;
int i, found;
all_words = malloc(words_size * sizeof(*all_words));
exit_if_null(all_words, "initial Allocation");
printf("Enter words(enter empty line to terminate):\n");
while (fgets(str, MAXCHAR, stdin) != NULL && strlen(str) != 1) {
word = strtok(str, " \n");
while (word !=NULL) {
if (words_size == word_count) {
words_size *= 2;
all_words = realloc(all_words, words_size * sizeof(*all_words));
exit_if_null(all_words, "Reallocation");
}
all_words[word_count] = malloc(strlen(word)+1);
exit_if_null(all_words[word_count], "Initial Allocation");
strcpy(all_words[word_count], word);
word_count++;
word = strtok(NULL, " \n");
}
}
printf("Words that have equal first and last letters:\n");
found = 0;
for (i = 0; i < word_count; i++) {
if (is_start_end(all_words[i])) {
found = 1;
printf("%s\n", all_words[i]);
}
free(all_words[i]);
all_words[i] = NULL;
}
if (found == 0) {
printf("None Found\n");
}
free(all_words);
all_words = NULL;
return 0;
}
int
is_start_end(char *word) {
int len;
len = strlen(word);
if ((len == 1) || (tolower(word[0]) == tolower(word[len-1]))) {
return 1;
}
return 0;
}
void
exit_if_null(void *ptr, const char *msg) {
if (!ptr) {
printf("Unexpected null pointer: %s\n", msg);
exit(EXIT_FAILURE);
}
}
This line removes the null terminator of the string:
len = strlen(str);
str[len] = ' ';
thus the string no longer exists, what is left is just an ordinary array of characters.
The next call to strlen, in the body of the for loop, will cause undefined behavior.
I have the following code to accept any number of lines from the user and print out the ones whose length is > 80 characters :-
#include <stdio.h>
#include <stdlib.h>
#include "shared.h"
#include <string.h>
int MAXLINE = 10;
int INCREMENT = 10;
int NUM = 1;
char* longest = NULL;
char* line = NULL;
char** row = NULL;
void _memcleanup(){
int i =0;
free(line);
free(longest);
for(i=0;i<NUM;i++){
free(row[i]);
}
free(row);
}
void print_lines(int len){
int i;
for(i=0;i<len;i++){
if(strlen(row[i])>80){
printf("%s\n",row[i]);
}
}
}
void copy(char** longest, char** line){
int i=0;
char* temp = realloc(*longest,(MAXLINE)*sizeof(char));
if(temp == NULL){
printf("%s","Unable to allocate memory");
_memcleanup();
exit(1);
}
*longest = temp;
while(((*longest)[i] = (*line)[i]) != '\0'){
++i;
}
longest[i] = '\0';
}
void store(char** s, int pos){
int i=0;
char* temp = realloc(row[pos],(MAXLINE)*sizeof(char));
if(temp == NULL){
printf("%s","Unable to allocate memory");
_memcleanup();
exit(1);
}
row[pos] = temp;
while((row[pos][i] = (*s)[i]) != '\0'){
++i;
}
row[pos][i] = '\0';
}
int _getline(char** s, int pos){
int i,c;
for(i=0; ((c=getchar())!=EOF && c!='\n'); i++){
if(i == MAXLINE - 2){
char* temp = realloc(*s,(MAXLINE + INCREMENT)*sizeof(char));
if(temp == NULL){
printf("%s","Unable to allocate memory");
_memcleanup();
exit(1);
}
*s= temp;
MAXLINE += INCREMENT;
}
(*s)[i] = c;
}
if(c == '\n'){
(*s)[i++] = c;
}
(*s)[i]= '\0';
store(s, pos);
return i;
}
int main(){
int max=0, len, i=0;
line = malloc(MAXLINE*sizeof(char));
longest = malloc(MAXLINE*sizeof(char));
//array of character pointers
row = malloc(NUM*sizeof(char*));
//allocate memory for each row in the array
for(i = 0; i < NUM; i++){
row[i]= malloc(MAXLINE*(sizeof(char)));
}
i=0;
//for(i=0; len = _getline(&line)) > 0; i++){
while((len = _getline(&line, i)) > 0){
printf("%d %d", len, MAXLINE);
/* if(len > max){ */
/* max = len; */
/* copy(&longest, &line); */
/* } */
i++;
}
/* if(max>0){ */
/* printf("%s",longest); */
/* } */
print_lines(i);
_memcleanup();
return 0;
}
The idea that i am following is to reallocate the 2D array when the number of lines exceed NUM. Now to test it out, i set NUM as 1. However, even after doing so, the program gladly accepts upto 3 inputs and segfaults on the 4th input i.e. pos=3 in the program's context.
Why does it accept 3 inputs (ideally it should give a segfault pos=1 itself since i have given size as only 1 and i am not allocating more space for the 2D array)
Working code is as follows:
#include <stdio.h>
#include <stdlib.h>
#include "shared.h"
#include <string.h>
int MAXLINE = 10;
int INCREMENT = 10;
int NUM = 1;
char* longest = NULL;
char* line = NULL;
char** row = NULL;
void _memcleanup(){
int i =0;
free(line);
free(longest);
for(i=0;i<NUM;i++){
free(row[i]);
}
free(row);
}
void print_lines(int len){
int i;
for(i=0;i<len;i++){
if(strlen(row[i])>80){
printf("%s\n",row[i]);
}
}
}
void copy(char** longest, char** line){
int i=0;
char* temp = realloc(*longest,(MAXLINE)*sizeof(char));
if(temp == NULL){
printf("%s","Unable to allocate memory");
_memcleanup();
exit(1);
}
*longest = temp;
while(((*longest)[i] = (*line)[i]) != '\0'){
++i;
}
longest[i] = '\0';
}
void store(char** s, int pos){
int i=0;
if(pos == NUM){
char** temprow = realloc(row, (NUM + INCREMENT)*sizeof(char*));
if(temprow == NULL){
printf("%s","Unable to allocate memory");
_memcleanup();
exit(1);
}
row = temprow;
//allocate space for extra elements
for(i=NUM;i<NUM+INCREMENT;i++){
row[i] = malloc(MAXLINE*sizeof(char));
}
NUM = NUM + INCREMENT;
}
char* temp = realloc(row[pos],(MAXLINE)*sizeof(char));
if(temp == NULL){
printf("%s","Unable to allocate memory");
_memcleanup();
exit(1);
}
row[pos] = temp;
while((row[pos][i] = (*s)[i]) != '\0'){
++i;
}
row[pos][i] = '\0';
}
int _getline(char** s, int pos){
int i,c;
for(i=0; ((c=getchar())!=EOF && c!='\n'); i++){
if(i == MAXLINE - 2){
char* temp = realloc(*s,(MAXLINE + INCREMENT)*sizeof(char));
if(temp == NULL){
printf("%s","Unable to allocate memory");
_memcleanup();
exit(1);
}
*s= temp;
MAXLINE += INCREMENT;
}
(*s)[i] = c;
}
if(c == '\n'){
(*s)[i++] = c;
}
(*s)[i]= '\0';
store(s, pos);
return i;
}
int main(){
int max=0, len, i=0;
line = malloc(MAXLINE*sizeof(char));
longest = malloc(MAXLINE*sizeof(char));
//array of character pointers
row = malloc(NUM*sizeof(char*));
//allocate memory for each row in the array
for(i = 0; i < NUM; i++){
row[i]= malloc(MAXLINE*(sizeof(char)));
}
i=0;
//for(i=0; len = _getline(&line)) > 0; i++){
while((len = _getline(&line, i)) > 0){
printf("%d %d", len, MAXLINE);
/* if(len > max){ */
/* max = len; */
/* copy(&longest, &line); */
/* } */
i++;
}
/* if(max>0){ */
/* printf("%s",longest); */
/* } */
print_lines(i);
_memcleanup();
return 0;
}
You are asking:
Why does it accept 3 inputs (ideally it should give a segfault pos=1 itself since i have given size as only 1 and i am not allocating more space for the 2D array)
and you are right, that if you allocate memory for one row only, it invokes Undefined Behaviour if you attempt to access rows[1]. But that behaviour is just -- undefined -- what means that you can't rely on the program crashing. Anything might happen, including that the program seems to work flawlessly
In the main(), instead of using
while((len = _getline(&line, i)) > 0){
use
while(i< NUM)
{
len = _getline(&line, i);
For me with this code change, it worked correctly.