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 8 years ago.
Improve this question
I am reversing a String without using inbuilt function . its reversing every character but missing last character
here is the program
#include<stdio.h>
void main()
{
char str[10],rev[10];
int i,j,k;
clrscr();
printf("enter the string \n");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
k=i-1;
for(j=0;j<=i-1;j++)
{
rev[j]=str[k];
k--;
}
rev[j]='\0';
printf("reverse=%s",rev);
getch();
}
I am not getting why the last Char is missing
k = i;
You miscounted it.
Your expression k = i-1; doesn't leave space for the whole reversed string.
The code that computes the length of the string is missing a semicolon:
The compiler interprets it as
for(i=0;str[i]!='\0';i++)
k=i-1;
while the intention has probably been to have
for(i=0;str[i]!='\0';i++)
;
k = i - 1;
Demo on ideone.
Now that the code is "working", you should fix an error that could cause undefined behavior: limit the length of the input to 9 characters in scanf, like this:
scanf("%9s", str);
Without 9, the user could cause undefined behavior by entering more than nine characters, and overflow your ten-byte buffer.
Related
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 last year.
Improve this question
#include <stdio.h>
#include <string.h>
struct student_details{
char name[34];
int roll;
char section;
};
int main(){
struct student_details student[5];
for (int i = 0; i < 5; i++)
{
printf("Your full name = ");
scanf("%s", student[i].name);
printf("Your roll = ");
scanf("%d", student[i].roll);
}
return 0;
}
I think something is wrong with my code anyone please fix this.
When I run this code, it's Shows an error. after running this code this code take 1 time input and second input is skipped.
The scanf function expects to accept a format string and then pointers to the data you want to scan into. student[i].name is an array, which in this case decays into a pointer to its first element. This works.
Note: This array only contains 34 characters. With the null terminator, you want to use a width specifier with scanf to limit the input and prevent a buffer overflow. "%33s"
When you try to read the roll:
scanf("%d", student[i].roll);
student[i].roll is an int, not a pointer. But pointers are numbers, so this will compile. Your compiler should warn you about it, though. But, then the program tries to dereference this value it thinks is a pointer, and a segmentation fault occurs.
What you want to do is pass the address of student[i].roll.
scanf("%d", &student[i].roll);
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
#include<stdio.h>
int main()
{
char word[1000];
scanf("word%s", word);
printf("%s", word);
}
It seems that when I input any string, as long as I type out "word" first, I get proper output.
But is this program actually valid
It compiles and therefore is valid from a syntax perspective. It's also fine in order to check that a prefix is used.
However, there are at least two ways to get undefined behaviour:
scanf might store more than 1000 characters (read 999 and one for the final \0)
scanf might read none if the input does not start with "word"
You should therefore check the result of scanf, initialize word, and also limit the maximum number of characters that scanf reads:
#include<stdio.h>
int main()
{
char word[1000] = {0};
int ret = scanf("word%999s", word);
if ( ret == 1 ) {
printf("%s", word);
}
}
this program is valid ,but you have to be careful about buffer overflow , which means if user input more than 999 chars this will lead to undefined behavior , so I suggest this:
scanf("word%999s", word);
also as you said as long as I type out "word" first ,otherwise char word[1000] will be uninitialized.
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
So im making a program that checks if a word is a palindrome but when it comes to comparing the final strings at the end even if they are the same i get a -1 result edit: copy pasted the exact same code i used
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main()
{
char input[50];
char test[50];
int ret;
printf("Enter word or phrase to compare ");
fgets(input,sizeof(input),stdin);
strcpy(test,input);
strrev(input);
ret = strcmp(test,input);
if(ret == 0)
printf("\n this is a palindrome ");
else
printf("\n this is not a palindrome");
}
For input i used "ala" which i know is a palindrome i get the result
this is not a palindrome
Demonstration on IDEONE.
The problem is that you call strrev without stripping off the newline from your input obtained from fgets. This causes your reversed string to have the newline at the beginning of the string, which would cause a mismatch even if you intended to provide a palindrome as the input.
While there are various ways to achieve this, one way would be to look at the last byte of your input, and see if it is a newline character. If it is, remove it.
if (fgets(input,sizeof(input),stdin) == NULL) {
/* todo: ... handle error ... */
return 0;
}
len = strlen(input);
if (input[len-1] == '\n') input[--len] = '\0';
strcpy(test,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 6 years ago.
Improve this question
I have this structure after scanning and printing p and q are printed while r is not printed can you please let me know why?
struct book
{
int p;
float q;
char r;
};
int main()
{
struct book b;
scanf("%d%f",&b.p,&b.q);
scanf("%c",&b.r);
printf("%d......%f.....%c",b.p,b.q,b.r);
return 0;
}
Problem :
that's because b.r takes in the \n character entered at the end of the previous scanf() statement
scanf("%d%f",&b.p,&b.q);
Solution :
Avoid it by giving a space before %c in the scanf()
scanf(" %c",&b.r);
Why give a space ?
This would consume if there are any whitespaces (' ' or '\n' or '\0') present in the input stream
Suggestion :
next time when you don't get any output when you print, try printing it's ascii value by casting it to int, that way you'd know what value the variable is taking and see it's corresponding character in ascii table.
printf("%d",(int) b.r);
for example, without making any changes to your code except this to your printf statement :
printf("%d......%f.....%d",b.p,b.q,(int)b.r);
you'd get
input :
2
2
output :
2.....2.....10
why the 10?
because it's the ascii value of \n or the newline character
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
while i am running the above title is appearing as error
#include <stdio.h>
int main()
{
int i;
char name[20];
printf("Enter name: ");
scanf("%s",name);
printf("Your name is %s",name);
while(name[i]!="\0")
{
i++;
if(name[i]==" ")
{
strcpy(b[i],name[i]);
printf("copied name: ");
scanf("%s",b[i]);
}
}
}
while i am running this it is showing this error why? warning: comparison between pointer and integer.
"\0" is a string, '\0' is a character. As you comparing a character, you need the latter.
Also, as pointed out by chqrlie, there are many other issues - you need to check your compiler warnings/errors and fix them all. For example,
name[i]==" " is wrong with the same reason.
where is b declared?
where is i initialized??
There are many errors/ warnings in your code.
It should be '\0'. Not "\0";
You have not declared b[];
Initialize i=0;
Instead of strcpy, you can use another method which is shown in below code.
Also, you should not use scanf in last line. You should use printf. scanf is used to take input from user. printf is used to print the results.
Code-
#include <stdio.h>
int main()
{
int i=0,n;
char name[20],b[20];
printf("Enter name: ");
scanf("%s",name);
printf("Your name is %s\n",name);
while(name[i]!='\0')
{
b[i]=name[i]; // to copy from name[] to b[]. Instead of strcpy
i++;
}
printf("copied name: ");
for(n=0;n<=i;n++)
{
printf("%c",b[n]); // to show the copied result.
}
printf("\n");
return 0;
}