Hello I have a small problem during compilation.
So that problem in my program are:
brackets(str1); - Too few arguments to function call
void brackets(str,len) - Conflicting types for 'brackets'
Ny code -
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void brackets(char str[], int len);
int main()
{
char str[99];
int len;
printf("enter Mathematical exercise: \n");
gets(str);
len = srtlen(str);
brackets(str1);
return(0);
}
void brackets(str,len)
{
char str1[99];
int i,j = 0;
for(i = 0; i < len; i++)
{
if (str[i] == '(')
{
i++;
while(str[i] != ')')
{
str1[j] = str[i];
i++;
j++;
}
}
}
printf("%s\n",str1);
}
I love to know what these errors arise and how can I arrange this program will work. Thanks
Change
void brackets(str,len)
to
void brackets(char str[], int len)
and your function call should be brackets(str, len);. And also change
len = srtlen(str); // Spelling mistake.
to
len = strlen(str);
your function brackets needs two parameters, so you need to give him two:
brackets(str1);
has to become
brackets(str1, len);
You're defining the brackets function as taking 2 arguments but passing only one. Pass the second one as well.
For your second error, if you don't provide a type for the function parameters the compiler will assume it's of type int. As the first parameter have already been declared to be of type char [] there is a mismatch between the declaration (prototype) and the definition of the function.
As for the first error, when you declare a function to take N parameters, you need to call it with N arguments. The exception being functions declare to take a variable number of arguments.
This is your issue: brackets(str1);
You called brackets with just one parameter, yet you define it as this:
void brackets(char str[], int len);
You need to pass an int as a second parameter.
multiple error seems in your code
Starting from main
you not declared str1 in main but used it in brackets(str1);.i think you mean it by str because you declared it char str[99];
so make change by brackets(str);
Also void brackets(str[], len) function not specify data type of function argument please change it to void brackets(char str[], int len)
Also in main len = srtlen(str); the srtlen is not valid but make it len = strlen(str);
And last you call brackets(str1); but brackets expect two argument so please make change in main by brackets(str,len);
After above change your code is now clean without error as follows
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void brackets(char str[], int len);
int main()
{
char str[99];
int len;
printf("enter Mathematical exercise: \n");
gets(str);
len = strlen(str);
brackets(str,len);
return(0);
}
void brackets(char str[],int len)
{
char str1[99];
int i,j = 0;
for(i = 0; i < len; i++)
{
if (str[i] == '(')
{
i++;
while(str[i] != ')')
{
str1[j] = str[i];
i++;
j++;
}
}
}
printf("%s\n",str1);
}
Related
My program is supposed to convert a string to lowercase but I keep gettingg this error "Argument type void is incomplete" when trying to convert a string to lowercase and I don't know why. Could anyone explain why this is happening, thanks
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char str[25] ="HELLOWORLD";
void upperToLower(char *str){
for (size_t i = 0; i < strlen(str); ++i) {
printf("%c", tolower((unsigned char) str[i]));
}
}
int main(void) {
upperToLower(str);
printf("%s\n", upperToLower(str));
return 0;
}
OUTPUT
I guess upperToLower is supposed to modify the string in place. Try the following code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char str[25] ="HELLOWORLD";
void upperToLower(char *str){
for (size_t i = 0; i < strlen(str); ++i) {
str[i] = tolower((unsigned char) str[i]);
}
}
int main(void) {
upperToLower(str);
printf("%s\n", str);
return 0;
}
You could also make upperToLower return the pointer that was passed to it (like strcpy does with its first parameter). That allows the the upperToLower call to be done as part of the printf call like this:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char str[25] ="HELLOWORLD";
char *upperToLower(char *str){
for (size_t i = 0; i < strlen(str); ++i) {
str[i] = tolower((unsigned char) str[i]);
}
return str;
}
int main(void) {
printf("%s\n", upperToLower(str));
return 0;
}
The return type of the function upperToLower() is void, so it cannot be used as a value in expressions.
The function upperToLower() prints things inside that, so you won't need to externally print something about that except for the newline character.
Try this:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char str[25] ="HELLOWORLD";
void upperToLower(char *str){
for (size_t i = 0; i < strlen(str); ++i) {
printf("%c", tolower((unsigned char) str[i]));
}
}
int main(void) {
upperToLower(str);
upperToLower(str); /* put this out of printf() */
printf("\n"); /* and print just newline character here */
return 0;
}
Yes. As specified in C specification the type void is an incomplete type.
The void type comprises an empty set of values; it is an incomplete
object type that
cannot be completed.
As result it is not possible to form any value of type void. Pointers of type void* can be used, but they cannot be dereferenced.
Your function returns void, you're literally passing nothing as a parameter.
I need to read a word from main function and convert the characters in UCASE if the first character is LCASE and vice versa using the user defined function.I tried ways for returning the array from function but still I am lacking some core ideas. Please debug this program and explain the way it works.
#include <stdio.h>
#include <string.h>
int* low (char str)
{
int i;
for (i=1; i<strlen(str);i++)
{
if(str[i]<91)
{
str[i]=str[i]+32;
}
else
{
}
}
return &str;
}
int* high (char str[50])
{
int i;
for (i=0; i<strlen(str);i++)
{
if(str[i]>91)
{
str[i]=str[i]-32;
}
else
{
}
}
return &str;
}
void main()
{
char str[50];
char* strl;
printf("Enter any string....\n");
scanf("%s",str);
if (str[0]<91)
{
*strl=low(str);
}
else
{
*strl=high(str);
}
printf("Converted string is %s.",*strl);
}
There is already a problem here:
So if you are saying this code is perfect and you want us to debug it and explain how (on earth) this works, then here you go.
In function int* low (char str), you have if(str[i]<91). Thats a problem right there. str is a char received as an argument, and hence str[i] is a straight compile-time error.
Another one to deal with is the return statement.
You have a statement:
return &str;
which would return the address of str, which by the way is a char, whereas function low is supposed to return a pointer to an int.
The same is applicable to high function as well.
Suggestion: Leave aside this bad code and get a beginner level C programming book first. Read it and the try some codes out of it.
A few inputs for improvement: (Which you may not comprehend)
change
void main()
to
int main(void)
Why? Refer this legendary post: What should main() return in C and C++?
Secondly, int both functions you are using strlen() in loop which will always return a fixed value. So, instead of
for (i=0; i<strlen(str);i++)
I'd suggest,
size_t strlength = strlen(str);
for (i=0; i < strlength; i++)
You can try the code and method as below:
#include <stdio.h>
#include <string.h>
char* caseConverter (char *str)
{
int i;
for (i=0; i<strlen(str);i++)
{
if(str[i]>=65 && str[i]<=90)
{
str[i]=str[i]+32; //To lower case
}
else if((str[i]>=97 && str[i]<=122))
{
str[i]=str[i]-32; //To upper case
}
else
printf("%c is not an alphabet \n",str[i]);
}
return str;
}
void main()
{
char inputStr[50]= "Stubborn";
char* opStr= caseConverter(inputStr);
printf("Converted string is %s",opStr);
}
I am trying to run below program in an online C compiler. But I get segmentation error. Can you help me fix this
#include <stdio.h>
#include <string.h>
int main()
{
char string[15] = "Strlwr in C";
printf("%s",tolower(string));
return 0;
}
Following is the prototype of tolower
int tolower(int c);
You should pass an int or something like char which can safely convert to int. Passing char * (Type of string) like you do leads to UB.
To convert a string to lowercase, you need to convert each character separately. One way to do this is:
char string[15] = "Strlwr in C";
char lstr[15];
int i = 0;
do {
lstr[i] = tolower(string[i]);
} while(lstr[i] != '\0');
printf("%s", lstr);
You are using tolower incorrectly. This function returns int and gets int as a parameter (here is it's declaration: int tolower(int c);). What you want to do is call it on each char of your char array, and print each one:
char string[15] = "Strlwr in C";
for(int i = 0; i < strlen(string); i++)
printf("%c",tolower(string[i]));
Read cplusplus.com/reference/cctype/tolower It takes a single int as parameter, not char and not array.
You probably want to use a loop on "string", which processes each in turn.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
int i;
char string[15] = "Strlwr in C";
for (i=0; i< sizeof(string)/sizeof(char); i++)
{
string[i]=(char)(tolower((int)string[i]));
}
printf("%s\n",string);
return 0;
}
Output:
strlwr in c
My code is suppose to initialise any word you type but it is refusing to compile
.
I don't understand the error messages it is giving to me.
1 initialize.c:24:23: error: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the
address with & [-Werror,-Wint-conversion]
2 initialize.c:21:23: error: format string is not a string literal (potentially insecure) [-Werror,-Wformat-security]
3 initialize.c:21:23: error: format string is not a string literal (potentially insecure) [-Werror,-Wformat-security]
printf(toupper(s[i]));
#include <stdio.h>
#include<cs50.h>
#include<string.h>
#include<ctype.h>
void initialize(string s);
int main(int argc, string argv[])
{
printf("May I have your name?");
string name = GetString();
initialize(name);
}
void initialize(string s)
{
int space = 1;
for (int i = 0;i < strlen(s); i++)
{ if(space == 1)
{
printf(toupper(s[i]));
space -= 1;
}
if(strncmp(s[i]," ",1 ) )
{
space += 1;
}
}
}
printf expects a format string with type const char* as its 1st argument, so:
change
printf(toupper(s[i]));
to
printf("%c", toupper(s[i]));
And as #Matt McNabb pointed, strncmp has the similar problem here. Because you tend to compare the 1st char only, you chould change
if(strncmp(s[i]," ",1 ) )
to
if (s[i] == ' ')
to make it clearer and more effective.
here is the code, after making it portable
But not handling user input errors
It has the necessary corrections so it cleanly compiles
#include <stdio.h>
#include <stdlib.h>
//#include <cs50.h>
#include <string.h>
#include <ctype.h>
void initialize(char * s);
int main( void )
{
printf("May I have your name?");
char name[50] = {'\0'};
fgets(name, sizeof(name), stdin ); // should check returned value
// to assure the line input was successful
initialize(name);
return 0;
} // end function: main
void initialize(char * s)
{
int space = 1;
for (size_t i = 0;i < strlen(s); i++)
{
if(space == 1)
{
printf("%c", toupper(s[i]));
space -= 1;
}
if( ' ' == s[i] ) // always place literal on left so compiler
// catches any place where '=' was used
// when it should have been '=='
{
space += 1;
}
}
} // end function: initialize
I am trying to use a function with a string as a parameter. I am running into a couple of error messages. First, it says that string[i] is not an array, pointer, or vector, despite the fact that string is a character array. Secondly, it says that I am doing a pointer to integer conversion. Here is my code:
#include <stdio.h>
#include <string.h>
void example (char string) {
int i;
for (i = 0; i < strlen(string); i++) {
printf (string[i]);
}
}
int main (void) {
example("I like pie");
return 0;
}
void example(char string) should be void example(char *string). You declared it to take a character, you want it to take a character pointer or array.
Also, you need to tell printf you are giving it a character: printf("%c", string[i]);.
Your API is wrong it should be
void example (char *string) { // string is a pointer
int i;
size_t n = strlen(string);
for (i = 0; i < n; i++) {
printf ("%c",string[i]); // print character using %c
}
}
Calculate the string length before the loop , calling strlen() in each iteration is not a good idea.
PS: what string points to is read-only you can't modify it
You should use void example(char *string) instead of void example (char string).
#include <stdio.h>
#include <string.h>
void example (char *string) {
int i;
for (i = 0; i < strlen(string); i++) {
printf ("%c",string[i]);
}
printf("\n");
}
int main (void) {
example("I like pie");
return 0;
}
Your function example just receives a character. To get a string you can use a pointer. Also you can use "%s" format specifier in printf instead of using the for loop and strlen() function.
#include <stdio.h>
#include <string.h>
void example (char *string) {
int i;
printf ("%s\n",string);
}