called object 'strn' is not a function [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
While compiling the below code, i am getting error
"called object strn is not a function"
tired of this error !! need a solution!!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define num 400
int main()
{
char strn[num];
int count;
int a=0,e=0,i=0,o=0,u=0;
printf("enter your string!\n");
gets(strn);
for(count=0;count<strlen(strn);count++)
{
if ( strn(count)=='a' )
{
a++;
}
if (strn(count)=='e')
{
e++;
}

You are trying to use strn as if it's a function: strn(count).
You are presumably trying to access the value at the count index, so you should use strn[count].

The error is quite indicative. You have declared strn as an array of characters.
char strn[num];
And using it as strn(count) which is wrong. The compiler considers it as a function. You should use square brackets [ ] instead of parentheses ( ).

In your code, strn(count) represents a function call to strn() with one argument count. What you need is to use the Array subscripting operator [], not ()
You need to change
strn(count)
to
strn[count]
Also, please consider using fgets() over gets().

The subscript operator uses symbols [] for enclosing index. So for example instead of
strn(count)=='a'
you have to write
strn[count]=='a'
Also function gets is not supported any more by the C Standard because it is an unsafe function. Use instead fgets.
The program can look like
#include <stdio.h>
#include <ctype.h>
#define num 400
int main( void )
{
char strn[num];
char *p;
int a = 0, e = 0, i = 0, o = 0, u = 0;
printf( "Enter your string: " );
fgets( strn, num, stdin );
for ( p = strn; *p != '\0'; ++p )
{
char c = tolower( *p );
switch ( c )
{
case 'a':
a++;
break;
case 'e':
e++;
break;
// and so on...

Related

Extracting text from char in C [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
I am a beginner C-programmer. Recently I've been trying to practise using string functions in C.
As such, I wrote the following program:
MessageDetector.c
#include <stdio.h>
#include <string.h>
int main(void)
{
char a[100] = "Alex:HeyGoodMorning!:1911hrs:0:1012:2017:::";
char *p = strtok(a,":");
char n[20];
int i = 1;
while(p != NULL) {
strcpy(n,p);
p = strtok(NULL,":"); //limit to characters before semi-colon
i++;
if (i = 2) { //after 2 occurrences of the semi-colon. print a string
printf("%s\n",n);
break;
}
}
return 0;
}
The output of my program is as follows:
Alex
However, I would like the program to output
HeyGoodMorning!
What are the changes I should make to the above program? Your help is greatfully appreciated
Initialize the variable i with 0 and use comparison instead of assignment in this condition
int i = 0;
//...
if(i == 2){//
Take into account that the first call of strcpy is redundant.
In fact you could do the same without a loop. For example
char a[100] = "Alex:HeyGoodMorning!:1911hrs:0:1012:2017:::";
char *p;
if ((p = strtok(a, ":")) && (p = strtok(NULL, ":")))
{
puts(p);
}

Two similar programs both have errors on final line [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
I have written two similar programs for my programming class. I have shared the code for both programs due to them being similar in nature and both have compiler issues on the final line with return0; For the first string of code the error I receive is "expected a declaration" and "syntax error: 'return'". For the second code I receive the errors "return value type does not match the function type" and " 'display': 'void' function returning a value. I have deleted and added {} at different parts of both codes and tried to rearrange the code. Thanks for help in advance.
Code 1
#include "stdafx.h"
#define nums 7
void display(int *);
int main()
{
int channels[nums] = { 2,4,5,7,9,11,14 };
printf("Channels: ");
display(channels);
}
void display(int *channels)
{
int i;
for (i = 0; i < nums; i++);
{
printf("%d", *(channels + i));
}
}
return 0;
code 2
#include "stdafx.h"
#define nums 7
void display(int *);
int main()
{
int channels[nums] = { 2,4,5,7,9,11,13 };
printf("channels: ");
display(channels);
}
void display(int *channels)
{
int i;
for (i = 0; i < nums; i++)
{
printf("%d", *channels);
*channels++;
}
return 0;
}
Return value is always associated to functions. For example, if a function sums up 2 integers and returns the sum of the integers, it will look something like:
int sum( int a, int b ){
return a + b;
}
See here, the return type of the function is int, and what it returns is also an int, since addition of two integers gives you an integer. It's like 2 horses + 3 horses = 5 horses. You cannot do 2 horses + 3 lions = 5 mangoes. I hope you get the point of return value and type.
Now in code 1, the error is you put your return 0; statement out of the main() function. The program cannot detect which function is this return statement associated to. To correct this, move your return statement in the next line after you call the display() function, INSIDE main() function.
Similarly, in code 2, move the return statement from the display() function to the main() function, just as you did in the previous case. This is because void expects nothing to be returned, but you are returning an integer. This is the same like giving a millionaire a loaf of break when he says he doesn't need one, he will surely get frustrated!
Hope that helps.

Code working only in one compiler [closed]

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 was doing homework and when i thought i finally did it fail to work in Visual Studio.
I have code like this and its working perfectly fine in Dev C++, but its not in Visual Studio. Can you explain me why and how to fix it.
Edit: Its compiling in both. In VS its display always 0, and it should 0 for text and a integers and for integers this number.
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#define czy_cyfra( c ) (( c ) > '0' && ( c ) < '9' )
#define jaka_cyfra( c ) (( c ) - '0' )
int czy_liczba_calkowita(char * a)
{
if (*a == '\0')
return -1;
do
{
if (!czy_cyfra(*a))
return -1;
} while (*++a != '\0');
return 0;
}
int jaka_liczba(char * a)
{
int liczba = 0;
do
{
liczba = liczba * 10 + jaka_cyfra(*a++);
} while (*a != '\0');
return liczba;
}
int main()
{
char a[255];
printf("Wprowadz lancuch\n");
scanf_s("%s", a);
printf("%u", czy_liczba_calkowita(a) == 0 ? jaka_liczba(a) : 0);
return 0;
system("pause");
}
You have missed out an argument for scanf_s and this results in undefined behaviour - which may or may not work. It should be
scanf_s("%s", a, (unsigned)sizeof a);
The reason for the unsigned cast? The MSVC man page for scanf_s says
The size parameter is of type unsigned, not size_t.
Not all compilers check that you have supplied arguments for all the fields of XscanXX and XprintXX families. For example, MSVC 9.0 does not check these supposedly "safer" functions for the correct number of arguments, but MSVC 14.0 does, giving a compilation error.

Tokenise a String in C programming [closed]

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 new to C programming and I know there have been other explanations on how to split a string into words but none of them seem similar to my program. I'm having difficulty finding the errors in my program:
#include <stdio.h>
#include <stdlib.h>
int tokenise(char str[], int start, char result[]) {
if (str[start] == "/o") {
return -1;
} else {
result = str[start];
}
}
int main() {
const int MAX_STRING = 256;
char buffer[MAX_STRING];
fgets(buffer, MAX_STRING, stdin);
char result[256];
int start;
start = tokenise(buffer, 0, result);
while ( start != -1 ) {
printf("%s\n", result);
start = tokenise(buffer, start, result);
}
}
In your function tokenise -
if(str[start] == "/o"){
What is "/o" you compare with? It should be '\0'.
if(str[start] == '\0'){
And in else your function does not return anything , therefore , in that case UB.
You function doesn't have any loop or use recursion to iterate over array ,therefore , your logic doesn't seem to achieve anything close .
You have many problems with your code:
else {
result = str[start];
}
No return value. That is undefined behaviour.
str[start] == '\o'
Thats incorrect as you want to compare to the EOS null termination character
Do this instead:
str[start] == '\0'
Lastly, if you want your tokenise function to write into result, you need to pass a pointer to result, not the value of result.
ps: semantic errors aside, your function does nothing resembling what you want. Look into loops and their implementation.

Could anyone please help me with this c program coding? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i have got a program to do as my homework. the program is simple. it asks to reverse the digits entered by the user and then print it using while loop. the problem arises when the user enters a number starting with zeroes.
For example:
Enter the number: 0089
The reversed number is : 9800
This is how the output should be. instead i get "98" as the answer.
and thanks in advance.
When asked to do someone else's homework, I like to devise an obtuse and compact way to do it.
void reverseNumber(void)
{
char c;
((c=getchar()) == '\n')? 0 : reverseNumber(), putchar(c);
}
Rather than reading the 0089 input as a numeric value, read it as a character array. This way the zeros won't be removed.
Read the numbers as a string.
And then use atoi() (stdlib.h) to make an integer number out if the string:
/* int atoi (const char *) */
Here is working code that makes exactly what your question requires:
// input: 0321
// output: 1230
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[80] = {0}, temp_str[80] = {0};
int num, i, length = 0, temp_length = 0;
printf("Enter a reversed number (e.g. 0089): ");
scanf("%s", str);
length = strlen(str);
temp_length = length;
printf("string_length: %d\n", length);
for ( i = 0; i < length; i++ ) {
temp_str[i] = str[temp_length - 1];
/* The string length is 4 but arrays are [0][1][2][3] (you see?),
so we need to decrement `temp_length` (minus 1) */
temp_length--;
}
printf("temp_str: %s\n", temp_str);
num = atoi(temp_str);
printf("num: %d\n", num);
return 0;
}

Resources