Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
So I've got a problem with int and string in C.
My task is to write a function that will output the error rate in string.
For example if the string is "aaabbbcccdxm" then the output should be "1/12".
By "error" I mean any letter from n to z, and any letter from a to m is "good".
I thought that I could do it by using a for loop to check every letter in the string, and then if to add value to int error which would be numbers of bad letters, but I don't know how to convert that "int error" value to string with output error value/string dimension. Any ideas?
You can use printf to format your output. I recommend reading the man 3 printf on a linux machine or from google.
Here is what such a program could look like:
#include <stdio.h>
#include <string.h>
int main()
{
const char * input_str = "aaabbbcccdxm";
int size = strlen(input_str);
int error = 0;
for (int i = 0; i < size; ++i)
{
if (input_str[i] >= 'n')
error++;
}
printf("%d/%d\n", error, size);
return 0;
}
size_t errors(const char *str, const char *legal)
{
size_t errcnt = 0;
while(str && *str)
{
errcnt += !strchr(legal, *str++);
}
return errcnt;
}
int main()
{
char *str = "aaabbbcccdxm";
printf("%zu/%zu\n", errors(str,"abcdefghijklm"), strlen(str));
return 0;
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 11 days ago.
Improve this question
I want to use the count variable to store numbers in the W array using malloc, but the output keeps repeating the same number entered.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int *w = malloc( sizeof(int) * 10000);
int x; int count = 0;
while (scanf("%d", &x) == 1) {
printf("%d", &x]);
*(w + x) = count;
count++;
}
free(w); w = NULL;
return 0;
}
argc, argv are not used so leave them out.
Prefer sizeof a variable (*w) opposed to a type (int). If the type changes you only need to make the change in one place.
Use constants instead of magic values.
You don't need the temporary variable x.
Prefer unsigned (size_t) types to (unsigned) int for values that should not be negative. It eliminates a possible error class.
Prefer a for to while-loop when counting things. It might be more readable like this:
for(size_t count = 0;; count++) {
if(scanf("%d", &w[count]) != 1)
break;
printf("%d\n", w[count]);
}
printf("%d", ...) takes a value not an address. This appears to be the main issue.
For readability by user add a new newline when printing the value.
#include <stdio.h>
#include <stdlib.h>
#define LEN 10000
int main() {
int *w = malloc(LEN * sizeof *w);
for(size_t count = 0; scanf("%d", &w[count]) == 1; count++)
printf("%d\n", w[count]);
free(w);
}
example session:
$ ./a.out
1
1
2
2
4
4
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
For a school assignment, I am to Create variables in main() that will store the counts and pass pointers to these variables to your functions so that the functions can modify the variables via the pointers. This is a school assignment so rather than someone Give me the answer, I would prefer is someone could help point me in the right direction of using pointers. The Code does work, but not in the way I would like yet.
the code is as follows
void myFunction(int *letters, int *numbers, int *otherCharacters){
}
int main(int argc, char * argv[]) {
// Code for command line argument
if (argc == 2) {
int letters = 0;
int numbers = 0;
int otherCharacters = 0;
int totalCharacters;
int length = strlen(argv[1]);
for (int i = 0; i < length; ++i){
if (isalpha(argv[1][i]) != 0)
++letters;
if (isdigit(argv[1][i]) != 0)
++numbers;
if (isdigit(argv[1][i]) == 0 && isalpha(argv[1][i]) == 0)
++otherCharacters;
}
totalCharacters = letters + numbers + otherCharacters;
printf("%i letters\n%i digits \n%i other characters\n%i characters total\n", letters, numbers, otherCharacters, totalCharacters);
}
I am hoping to rather than change the values of letters, numbers, otherCharacters, and totalCharacters in the main function use pointers to do so in myFunction(). any help on how to use pointers to do so would be much appreciated. Again, I am not asking for an answer, as I would like to complete this assignment myself.
Seems like the function is supposed to look at a string and tell you how many letters, numbers, and other characters there are. It needs to take the counts as pointers, and the string.
void countCharacters(const char *string, int *letters, int *numbers, int *other) {
....
}
Because they are pointers, when incrementing them you need to dereference them first to get their values. Instead of letters++ it would be (*letters)++.
And we can replace the main code to show how you'd call this.
int main(int argc, char * argv[]) {
// Exit early to avoid deeply nesting all the code.
if (argc != 2) {
perror("please supply a string");
return 1;
}
int letters = 0;
int numbers = 0;
int other = 0;
// Pass in the string (already a pointer) and the counts as pointers.
countCharacters(argv[1], &letters, &numbers, &other);
int total = letters + numbers + other;
printf("%i letters\n%i digits \n%i other characters\n%i characters total\n", letters, numbers, other, total);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Can anyone please help me.I want to remove character from char* in C.
For example, char *str this str equals to "apple" and first i remove 1. character and define a new variable then remove 2. character and define new variable and so on until the str ends.
How can ı do that ? I'm completely new to C, and just can't seem to figure it out
Assuming you only need to remove characters from the left.
Because strings in C are arrays of characters, and arrays are pointers to the first element of the array, you can create variables that point to specific indices in you original char array.
For example
char* myString = "apple";
char* subString = myString+1;
printf("%s\n%s\n", myString, substring);
would produce the output
apple
pple
We could generalize this to store all substrings obtained by removing characters from the left in an array of char* pointers (pointers to pointers). Like so:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char** GetSubStrings(char* input)
{
int length = strlen(input);
char** result = calloc(length, sizeof(char*));
for(int i = 0; i < length; i++)
{
result[i] = input + i;
}
return result;
}
int main()
{
char* myString = "apple";
char** subStrings = GetSubStrings(myString);
for(int i = 0; i < strlen(myString); i++)
{
printf("%s\n", subStrings[i]);
}
free(subStrings);
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to print random words with the use of rand().
I think that I've made a mistake on the pointer arithmetic since I get a weird output.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main ()
{
srand(time(0));
const int randomBit = rand() % 2;
char * sz[] ={"good", "bad"};
switch (randomBit)
{
case 0:
for (char *i=*sz; *i<(sz+3); i++)
{
printf("%c", *i);
}
break;
case 1:
for (char *i=*(sz+3); *i!=0x0; i++)
{
printf("%c", *i);
}
break;
default:
break;
}
return 0;
}
What's my mistake?
Thank you.
Given this declaration ...
char * sz[] ={"good", "bad"};
... this code has undefined behavior:
for (char *i=*(sz+3); *i!=0x0; i++)
The expression *(sz+3) is equivalent to sz[3], but array sz has only two elements, so the maximum index is 1.
Likewise, the expression *sz is equivalent to sz[0]. That one is semantically ok but stylistically poor. Likewise stylistically poor is splitting out two separate cases when you could instead cover both with the same code by using sz[randomBit] to select which string to print.
Furthermore, it is unclear why you are printing character by character. Perhaps that's part of the assignment, but in the real world a programmer would probably write ...
printf("%s", sz[randomBit]);
... instead of that entire switch statement.
Your code is incredibly complicated (and wrong).
What's wrong with this:
int main()
{
srand(time(0));
const int randomBit = rand() % 2;
char* sz[] = { "good", "bad" };
printf("%s\n", sz[randomBit]);
return 0;
}
or if you are not allowed to use the %s format specifier as part of your assignement:
for (char* i = sz[randomBit]; *i != 0; i++)
{
printf("%c", *i); // or putchar(*i);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
How do I resolve this? Thank you
struct node
{
char sym[100];
}s[20];
int main()
{
for(int i=0;i<5;i++)
s[i].sym=i;
return 0;
}
Not sure what the problem is, When I use an array to store the integer value/ ascii value I get this error. Why is that so? For a non array variable, the input is taken without errors
What you effectively have in your code is no different to:
char sym[100];
sym = 0;
You cannot assign an integer to an array. Your code would be more likely to work with:
int main() {
for (int i = 0; i < 5; i++) {
s[i].sym[SOMETHING] = i;
}
return 0;
}
Although, of course, you need to decide the correct value of SOMETHING.
You cannot put int in the array (char)
Try this...
#include <iostream>
using namespace std;
struct node {
char sym[100];
}s;
int main() {
int i = 0;
for(char a = '1'; a <= '5'; a++){
s.sym[i] = a;
cout << s.sym[i] << " ";
i++;
}
cout << endl;
return 0;
}