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 5 years ago.
Improve this question
I want to get string(including white spaces)input save to 2-dimensional array.
my code is this :
char a[10000][6];
scanf("%d", &n);
for (int i = 0;i < n;i++)
{
scanf("%[^\n]s", a[i]);
}
for (int i = 0;i < n;i++)
{
printf("%s\n", a[i]);
}
What I got that is wrong output.Please give me any suggestion!
Use
char a[10000][6];
scanf("%d",&n);
int i;
for (i = 0;i < n;i++)
{
scanf("%s",&a[i]);
}
I recently solved this question in my program. I came to the conclusion that the best way to read input from keyboard is to handle character one by one. You can use code below.:
bool in()
{
int i;
int a;
int len;
int max;
char *text[10000];
//change 'max' somehow here before loop
for(i = 0, len = 0; i < max; i++)
{
text[i] = NULL;
do
{
do
{
a = getchar();
}
while(a == '\n' && !text[i]);
if(text[i])
{
len = strlen(text[i]);
len++;
}
else
{
len = 1;
}
text[i] = realloc(text[i], sizeof(char)*(len+1));
if(!text[i])
{
printf("cant realloc\n");
return false;
}
if(a != '\n')
{
text[i][len-1] = (char) a;
text[i][len] = '\0';
}
else
{
text[i][len-1] = '\0';
}
}
while(a != '\n');
}
return true;
}
Related
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 2 years ago.
Improve this question
I have created the below program to detect the shortest word in a sentence for me. However, the result is not what I expected. I went through the code a few times and I still could not find the problem.
I would be very grateful if someone could lend me some help.
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
ssize_t find_short(const char *s);
int main(void)
{
char s[100] ="lets talk about C the best language";
printf("%zu", find_short(s));
}
ssize_t find_short(const char *s)
{
int n = strlen(s);
int smallest = n;
int counter = 0;
for (int i = 0; i <= n; i++)
{
if (s[i] == ' ' || s[i] == '\0')
{
if (counter <= smallest)
{
smallest = (ssize_t) counter;
counter = 0;
}
}
else
counter ++;
}
return smallest;
}
Thank you very much.
You reset counter only when counter is smallest than smallest. If a new word is shorter than any previous, it will be ignored.
ssize_t find_short(const char *s)
{
int n = strlen(s);
int smallest = n;
int counter = 0;
for (int i = 0; i <= n; i++)
{
if (s[i] == ' ' || s[i] == '\0')
{
if (counter <= smallest)
{
smallest = (ssize_t) counter;
// <-- not here
}
counter = 0; // < -- here
}
else
counter ++;
}
return smallest;
}
counter=0 should be outside the if statement
ssize_t find_short(const char *s)
{
int n = strlen(s);
int smallest = n;
int counter = 0;
for (int i = 0; i <= n; i++)
{
if (s[i] == ' ' || s[i] == '\0')
{
if (counter <= smallest)
{
smallest = (ssize_t) counter;
}
counter = 0;
}
else
counter ++;
}
return smallest;
}
This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 5 years ago.
In the below code I am getting unexpected result. During first iteration s[0] is getting printed as the first element. But during the remaining iteration, 0 is getting printed. Is it wrong to use = operator instead of strcpy? I tried using strcpy but it results in segmentation fault. Why is wrong value getting printed?
int main() {
int n, i;
scanf("%d", &n);
int c, j;
char* s[n];
char* ch = (char*)malloc(100);
for (i = 0; i < n; i++) {
scanf("%s", ch);
s[i] = strdup(ch);
}
char* final[n];
int count[n];
for (i = 0; i < n; i++) {
final[i] = "";
count[i] = 0;
}
int end;
for (i = 0; i < n; i++) {
end = 0;
while (1) {
printf("%s\n", s[0]);
if (strcmp(final[end], "")) {
count[end]++;
final[end] = s[i];
// printf("%s\n",final[end]);
end++;
break;
}
else if (strcmp(final[end], s[i])) {
final[end] = s[i];
sprintf(final[end], "%d", count[end]);
count[end]++;
end++;
break;
}
end++;
}
}
for (i = 0; i < n; i++) {
// printf("%s\n",final[i]);
}
return 1;
}
if (strcmp(final[end], ""))
strcmp returns 0 if final[end] compares equally to the empty string.
A 0 in the if condition means false, so the if-block is not executed. I believe you want it executed. So you should do
if (strcmp(final[end], "") == 0)
The same applies to the else if statement.
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 6 years ago.
Improve this question
I am getting errors while compiling.
incompatible integer to pointer conversion assigning to 'string'
(aka 'char *') from 'char'; take the address with &
My Code:
#include<stdio.h>
#include<cs50.h>
#include<string.h>
int pallin(string A);
int main(void)
{
printf("Enter the string to analyze\n");
string S[10];
S = GetString();
int flag = pallin(S);
if(flag == 0)
{
printf("Invalid input\n");
}
else if (flag == 1)
{
printf("Yes, the input is a pallindrome\n");
}
else{
printf("The input is not a pallindrome\n");
}
}
int pallin(string A)
{
int flag;
int n = strlen(A);
if(n<=1)
{
return 0;
}
else
{string B[10];int i = 0;
while(A[i]!="\0")
{
B[i]=A[n-i-1]; //Getting error here.
i++;
}
for(int j = 0; j < n; j++)
{
if(B[j]!=A[j])
{
flag = 2;
}
else
{
flag = 1;
}
}
return flag;
}
}
I'm not fond of the CS50 typedef char *string; — it doesn't help enough and does cause far too much confusion. You can't declare arrays of characters using string.
This code works:
#include <stdio.h>
#include <cs50.h>
#include <string.h>
int palin(string A);
int main(void)
{
printf("Enter the string to analyze\n");
string S = GetString();
int flag = palin(S);
if (flag == 0)
{
printf("Invalid input\n");
}
else if (flag == 1)
{
printf("Yes, the input is a palindrome\n");
}
else
{
printf("The input is not a palindrome\n");
}
}
int palin(string A)
{
int flag;
int n = strlen(A);
if (n <= 1)
{
return 0;
}
else
{
char B[100];
int i = 0;
//while (A[i] != "\0")
while (A[i] != '\0')
{
B[i] = A[n - i - 1]; // Getting error here.
i++;
}
for (int j = 0; j < n; j++)
{
if (B[j] != A[j])
{
flag = 2;
}
else
{
flag = 1;
}
}
return flag;
}
}
Changes are to string S = GetString(); in main(); char B[100]; in palin(); respelled 'palindrome'; use '\0' in place of "\0" (which has other problems too; it's the same as "" in this context, and that isn't how you compare strings (in the generic sense as well as the CS50 sense) — you need strcmp() if you want to compare strings, but you don't in this context).
It doesn't free the allocated string. It does produce the correct answers (program name pa19):
$ pa19
Enter the string to analyze
amanaplanacanalpanama
Yes, the input is a palindrome
$ pa19
Enter the string to analyze
abcde
The input is not a palindrome
$ pa19
Enter the string to analyze
Invalid input
$
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 7 years ago.
Improve this question
I'm receiving an uknown error found in the stdio.h library.
Please can someone check it at tell me what is wrong with the code (But I thing it should work fine).
P.S. I'm new here so please don't blame me if this is a bad question.
Code:
#include <stdio.h>
#include <stdlib.h>
// Conversion from a number to a string
char *i2s(int broj);
int main()
{
char string1;
int br, n;
do
{
printf("How much numbers?\n -"), scanf("%d", &n);
} while (n < 1);
for (int i = 0; i < n; i++)
{
printf("\nbr = "), scanf("%d", &br);
string1 = *i2s(br);
printf(" %s", string1);
}
free(string1);
getch();
return 0;
}
char *i2s(int broj)
{
char *pom;
int z=0,br=0,p;
if (broj < 0)
{
z = 1;
broj = -broj;
}
p = broj;
do
{
br++;
p /= 10;
} while (p);
pom = (char *)calloc(br + 1 + z, sizeof(char));
if (z)
pom[0] = '-';
do
{
pom[--br + z] = '0' + broj % 10;
} while (broj /= 10);
return pom;
}
char string1;
free(string1);
string 1 is not a pointer.
Also with the following section you overwrite string1 everytime you run through the loop. that way you have no pointer to free() the memory that you allocate inside your function unless you do it inside the loop.
for (int i = 0; i < n; i++)
{
printf("\nbr = "), scanf("%d", &br);
string1 = *i2s(br);
printf(" %s", string1);
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I wrote the following code block I have all the time error in the function find_brackets and calculation. can someone explain to me how to fix it. And the two functions will function together
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void find_brackets(char str[], int len);
void calculation(char str1);
int main(void) {
int len;
char str1[99];
char str[99]; // (4/2)
printf("Enter a math exercises: \n");
gets(str);
len = strlen(str);
find_brackets(str);
calculation(str1);
}
void find_brackets(str[], len) {
char str1[len];
int i, j;
for(i = 0; i < len; i++) {
if(str[i] == '(') {
i++;
while(str[i] != ')') {
str1[j] = str[i];
i++;
j++;
}
}
}
}
void calculation(str1[], len) {
char str[len];
char strp[len];
char str2[len];
char str3[len];
char *rev;
int i, k, j = 0, aPos, zPos;
int sum1, sum2;
float sum;
strcpy (str, str1);
strcpy (strp, str1);
aPos = zPos = -1;
for(i = 0; i < len; i++) {
if(str[i] == '+') {
aPos = i;
}
else if(str[i] == '/') {
zPos = i;
break;
}
}
if(aPos != -1 && zPos != -1) {
for(k = 0, i = zPos-1; i > aPos; --i, ++k) {
str2[k] = str[i];
}
}
rev = strrev(str2);
printf("%s\n", rev);
for(i = 0; i < len; i++) {
if(strp[i] == '/') {
while(strp[i+1] != '+') {
str3[j++] = strp[++i];
}
}
}
printf("%s\n", str2);
sum1 = atoi(str2);
sum2 = atoi(str3);
sum = sum1 / sum2;
printf("%.0f\n", sum);
}
Thanks for the help I appreciate it
Function declaration is void find_brackets(char str[], int len); and the caller from main() is find_brackets(str); which is wrong. Where is the 2nd arg.
Also function calculation() has differnce in declaration and how it is invoked. Maintain a match in function formal arguments followed by callee actual arguments passed.
void find_brackets(str[],len)
void calculation(str1[],len)
please specify data type of len and str[] at function definations.
Also
find_brackets(str);
calculation(str1);
which pass one argument but you declared that with two which is also wrong.