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 this simple C program, It compiles successfully but It does not execute perfectly. It takes the input and closed after a while. I don't know, why it is not displaying output?
Please help me...
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
char str1[200], str2[200];
system("cls");
printf("<==== Enter two strings ====>\n");
printf("First: ");
scanf("%s", &str1);
printf("Second: ");
scanf("%s", &str2);
char txt1[] = "\nThe string \" ";
char txt2[] = " \"and \"";
char txt3[] = " not equal.";
strcat(strcat(strcat(strcat(txt1, str1), txt2), str2), txt3);
if(strcmp(str1, str2) != 0)
{
printf("%s", txt2);
}
else
{
printf("\nString 1 and string 2 are equal.");
}
getch();
}
Passing pointers to arrays to scanf() invokes undefined behavior because it is not it excepts. & should be removed and pointers to char should be passed. Using %d with char* also invokes undefined behavior. int* should be passed for that.
txt1 has no room for extra things, so the strcat() will cause out-of-range access, which also invokes undefined behavior. You should specify the number of elements explicitly to allocate enough elements.
(optional) You should specify the maximum length to read in %s for avoiding buffer overrun.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
char str1[200], str2[200];
system("cls");
printf("<==== Enter two strings ====>\n");
printf("First: ");
scanf("%199s", str1); /* remove & and add length limit */
printf("Second: ");
scanf("%199s", str2); /* remove & and use correct specifier */
char txt1[512] = "\nThe string \" "; /* allocate enough elements */
char txt2[] = " \"and \"";
char txt3[] = " not equal.";
strcat(strcat(strcat(strcat(txt1, str1), txt2), str2), txt3);
if(strcmp(str1, str2) != 0)
{
printf("%s", txt2);
}
else
{
printf("\nString 1 and string 2 are equal.");
}
getch();
}
Related
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Why doesn't my program take input inspite of the fgets part? Any other suggestions are welcome
(1 answer)
Using scanf and fgets in the same program?
(4 answers)
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 1 year ago.
I atentionally keep optional details for a best undersanding. say we want to store a string in a variable of char* :
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
char * s;
int n;
do{
printf("string's length: ");
scanf("%d", &n);
}while(n<=0);
s = (char *) malloc((n+1) * sizeof(char));
if(s!=NULL){
printf("enter a string (length <= %d): ", n);
gets(s);
puts(s);
free(s);
}
puts("end of programme.\n");
return 0;
}
in this answer it says :
If you were to set s to point to a preexisting array or if you used malloc to allocate space, then you can write to it successfully.
so, despite all that, why the call to gets still doesn't success? how can we explain this behavior ?
The problem is that gets read the the buffer until a line break (\n), when you input a number, you press enter to confirm it, and the \n goes to the buffer. The gets read it and assume that's what you want. You need to throwaway the line breaker before call gets.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
char * s;
int n;
do{
printf("string's length: ");
scanf("%d", &n);
}while(n<=0);
scanf("%*c"); // throwaway a character, in this case, the line breaker
s = (char *) malloc((n+1) * sizeof(char));
if(s!=NULL){
printf("enter a string (length <= %d): ", n);
gets(s);
puts(s);
free(s);
}
puts("end of programme.\n");
return 0;
}
As you can see in the above mentioned title , I don't know if there is something I am doing wrong . I have just started learning c . Please help me with the code.
#include <stdio.h> //code in c language.
#include <string.h>
int main()
{
char str[20];
printf("Enter the string:");
scanf("%s",&str);
gets(str);
printf("The string is: %s \n",str);
printf("The reverse string is : %s",strrev(str));
return 0;
}
In order to get the complete string from stdin, its recommended to use fgets like follows:
#include <stdio.h> //code in c language.
#include <string.h>
int main()
{
char str[20];
printf("Enter the string:");
/* strtok removes trailing newline character from fgets() */
strtok(fgets(str, 20, stdin), "\n");
printf("The string is: %s \n", str);
/* error is still present here */
printf("The reverse string is : %s", strrev(str));
return 0;
}
The above should answer your question although strrev() is currently causing an error!
Here is a link to a previous stackOverflow question on successfully reversing a string: Reversing a string in C
This question already has answers here:
Reading string from input with space character? [duplicate]
(13 answers)
Closed 4 years ago.
I have a simple C program as follows:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[100],b[100];
char *ret;
printf("Enter the string\n");
scanf("%s",a);
printf("Enter the substring to be searched\n");
scanf("%s",b);
ret= strstr(a,b);
if(ret==NULL)
{
printf("Substring not found\n");
}
else
{
printf("Substring found \n");
}
}
When I execute the following program, scanf to read the string into b is not waiting for me to enter the substring and the print statement that prints the substring not found is being printed on the console. I tried to give %sand tried in the scanf statement and removed \n from the printf statements and nothing changed the way it executed the program. It would be great if someone solves this simple problem. Thanks in advance.
You can use scanf ("%[^\n]%*c", variable); with this scanf will read the whole line, instead of stopping when a space is reached.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[100];
char b[100];
char *ret;
printf("Enter the string\n");
scanf ("%[^\n]%*c", a);
printf("Enter the substring to be searched\n");
scanf ("%[^\n]%*c", b);
ret= strstr(a,b);
if(ret==NULL)
{
printf("Substring not found\n");
}
else
{
printf("Substring found \n");
}
}
Also you can use fgets
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[100];
char b[100];
char *ret;
printf("Enter the string\n");
fgets(a,100,stdin);//100 is the size of the string, you could use sizeof()
printf("Enter the substring to be searched\n");
fgets(b,100,stdin);//100 is the size of the string, you could use sizeof()
ret= strstr(a,b);
if(ret==NULL)
{
printf("Substring not found\n");
}
else
{
printf("Substring found \n");
}
}
try to use fgets instead of scanf, probably the reason is that the spaces are treated as delimiters, and the parts before the space are treated as a and the part right after the space will be treated as b. Therefore the programme did not prompt you for another input.
For your information: Reading string from input with space character?
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 5 years ago.
Improve this question
the que is i am expecting 3 strings from user ,but if user types only 2 strings the my code will stop as it is expecting 1 more string .but i need my code to continue anyhow with 2 strings only.so what changes should i do.
`enter code here`
#include <stdio.h>
#include <ctype.h>
int main()
{
char a[100];
char b[100];
char c[100];
printf("enter your name\n");
scanf("%s %s %s",a,b,c);
printf("%c%c%c",toupper(a[0]),toupper(b[0]),toupper(c[0]));
return 0;
}
Based on the suggestions by John Coleman:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define BUFFER_SIZE 300
int main()
{
char strings[3][100] = {{},
{},
{}};
char cInputBuffer[BUFFER_SIZE];
const char delimeter[2] = " ";
char* token;
int i = 0;
printf("Enter your name\n");
scanf("%[^\n]", cInputBuffer);
token = strtok(cInputBuffer, delimeter);
while( token != 0 && i < 3)
{
sprintf(strings[i],"%.100s",token);
token = strtok(NULL, delimeter);
i++;
}
if(i > 1)
{
printf("%c%c%c",toupper(strings[0][0]),toupper(strings[1][0]),toupper(strings[2][0]));
}
else
{
printf("Enter at least personal and family name.");
}
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 7 years ago.
Improve this question
I'm using atof(word), where word is a char type. It works when the word is a number, such as 3 or 2, but atof doesn't distinguish when word is an operator, such as "+". Is there a better way to check whether the char is a number?
I'm a newbie to CS, so I'm quite confused on how to do this properly.
If you're checking a single char, use the isdigit function.
#include <stdio.h>
#include <ctype.h>
int main()
{
printf("2 is digit: %s\n", isdigit('2') ? "yes" : "no");
printf("+ is digit: %s\n", isdigit('+') ? "yes" : "no");
printf("a is digit: %s\n", isdigit('a') ? "yes" : "no");
}
Output:
2 is digit: yes
+ is digit: no
a is digit: no
Yes there is, strtol(). Example
char *endptr;
const char *input = "32xaxax";
int value = strtol(input, &endptr, 10);
if (*endptr != '\0')
fprintf(stderr, "`%s' are not numbers\n");
The above would print "xaxax' are not numbers"`.
The idea is that this function stops when it finds any non numeric character, and makes endptr point to the place where the non numeric character appeared in the original pointer. This will not consider and "operator" as a non numeric value because "+10" is convertible to 10 since the sign is used as the sign of the number, if you want to parse the "operator" between two operands you need a parser, a simple one could be written using strpbrk(input, "+-*/"), read the manual for strpbrk().
Do you mean if a string contains only digits?
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char *str = "241";
char *ptr = str;
while (isdigit(*ptr)) ptr++;
printf("str is %s number\n", (ptr > str) && (*str == 0) ? "a" : "not a");
return 0;
}
Assuming by word, you mean a string, which in C, is either a char* or a char[].
Personally I would use atoi()
This function returns the converted integral number as an int value. If no valid conversion could be performed, it returns zero.
Example:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void is_number(char*);
int main(void) {
char* test1 = "12";
char* test2 = "I'm not a number";
is_number(test1);
is_number(test2);
return 0;
}
void is_number(char* input){
if (atoi(input)!=0){
printf("%s: is a number\n", input);
}
else
{
printf("%s: is not a number\n", input);
}
return;
}
Output:
12: is a number
I'm not a number: is not a number
However if you're just checking a single character, then just use isdigit()