URI Online Judge | 1759 Ho Ho Ho - c

Hello this is my solution to to uri 1759
#include <stdio.h>
int main()
{
int i,j;
scanf("%d", &i);
for (j=1;j<=i;j++)
{
printf("Ho ");
while (j==i)
{
printf("\b");
printf("!");
break;
}
}
return 0;
}
I don't know what I got wrong ..can you please help me? the link of the problem https://www.urionlinejudge.com.br/judge/en/problems/view/1759

Your solution may look correct, it isn't identical to the desired output, because of the trailing space that you erase by inserting a backspace. So, the solution is to re-craft your logic to avoid having to erase that trailing space.. One way of doing that, which I don't advise to really use, is like this:
#include <stdio.h>
int main()
{
int i,j;
scanf("%d", &i);
for (j=1;j<=i;j++,printf("Ho%s",(j>i)?"!":" "));
return 0;
}

Related

I didn't write printf but in output it duplicates the input

enter image description here
#include <stdio.h>
char s[50];
int n,i;
int main(){
scanf("%d",&n);
for (i=0;i<n;i++){
scanf("%s",s);
}
return 0;
}
it shouldn't give the output but it duplicates the input and gives output
for example try to write:4 hello how are you it won't give, but if you copy paste the input as in the example it will give that:(guys I wrote each of them in separate line but site doesn't show it like this. Also, this bug won't happen if you write it one by one you have to copy paste it.)
Input:
4
hello
how
are
you
Output:
hello
how
are
you
Here's a better example from an online compiler:
https://godbolt.org/z/joYdsdc11 As you can see (right window below stdin/input), there is no output.
Here is a fixed program (I also removed global variables since using them is bad practice):
#include <stdio.h>
int main (void){
int n;
char s[50];
scanf("%d",&n);
printf("%d\n", n);
for (int i=0;i<n;i++){
scanf("%s",s);
puts(s);
}
return 0;
}
Output:
4
hello
how
are
you
https://godbolt.org/z/sjcMrq4vb

Beginner C language: Problem with condition in while loop... same logic different result

My code should delete all vowels from the string that i give. But it does not delete if the vowel is the last character of the string.
#include <stdio.h>
#include <string.h>
int main()
{
int i=0;
char str[100];
printf("Enter a string :\n");
gets(str);
int len=strlen(str);
while(str[i]!='\0')
{
printf("%c",str[i]);
if(str[i]=='A'||str[i]=='a'||str[i]=='E'||str[i]=='e'||str[i]=='I'||str[i]=='i'||str[i]=='O'||str[i]=='o'||str[i]=='U'||str[i]=='u')
printf("\b");
i++;
}
return 0;
}
Like if i provide the string Hello it prints Hllo where it should print Hll ...But if i change the while condition to (i
I guess printing \b doesn’t do what you think it does. It does not delete the last printed character, it just prints an additional ‘backspace’ character, which on some output devices (such as console) moves backwards by one character. (Then, the next character overwrites the one you wanted ‘deleted’.)
Don’t do that. Instead, move the ‘if’ statement so that you don’t print those vowels in the first place!
You can try this
#include <stdio.h>
#include <string.h>
int main()
{
int i=0;
char str[100];
printf("Enter a string :\n");
fgets(str,100, stdin);
int len=strlen(str);
while(str[i]!='\0')
{
if(str[i]=='A'||str[i]=='a'||str[i]=='E'||str[i]=='e'||str[i]=='I'||str[i]=='i'||str[i]=='O'||str[i]=='o'||str[i]=='U'||str[i]=='u'){
continue;
}else{
printf("%c",str[i]);
}
i++;
}
return 0;
}

Struct and a symbol array

can you help me?
i have a problem with char* station;
when i fill my gaps, everithing is alright, but when i am with printf("%d)Input its stations: ",i+1);. It's a problem, i mean: i enter chech-joch-chor-dsh-dsh but i need to enter chech joch chor dsh dsh(these are names of stations,it's an example).So it prints ONLY THE FIRST WORD, i dont why.. check this out please... (i understand that i need to free what i'have taken). Please, explain why it is so, why the first?.. give me a hint..
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef struct info_bus_{
int number;
int begin;
int end;
char* stations;
int time_working;
}info_bus;
int main()
{
info_bus *b=NULL;
int i,n;
char buffer[128];
printf("How many buses u have: ");
scanf("%d",&n);
b=(info_bus *)malloc(n*sizeof(info_bus));
for(i=0;i<n;i++){
printf("Input the number of a bus: ");
scanf("%d",&(b+i)->number);
printf("%d)Input when it starts to work: ",i+1);
scanf("%d",&(b+i)->begin);
printf("%d)Input when it finishes to work: ",i+1);
scanf("%d",&(b+i)->end);
printf("%d)Input its stations: ",i+1);
scanf("%127s", buffer);
b[i].stations = (char*) malloc(strlen(buffer) + 1);
strcpy(b[i].stations, buffer);
printf("Input time working: ");
scanf("%d",&(b+i)->time_working);
}
for (i=0;i<n;i++){
printf("\n[%d].the number of a bus: %d",i+1,b->number);
printf("\n[%d]. Begin at: %d",i+1,b->begin);
printf("\n[%d]. Finishes at: %d",i+1,b->end);
printf("\n[%d]. Stations: %s",i+1,b->stations);
printf("\n[%d]. Time working: %d",i+1,b->time_working);
printf("\n");
}
return 0;
}
but when i use gets()
it is:
scanf("%127s", buffer);
stops reading after encountering a newline. If you want to be able to read multiple words then use fgets():
fgets(buffer, sizeof buffer, stdin);
Note: fgets() will also read the newline character if there's room. You can remove it if necessary:
buffer[strcspn(buffer, "\n")] = 0; /* to remove the newline */
Generally, avoid using scanf() even for other inputs. It's error prone. See: Why does everyone say not to use scanf? What should I use instead?
Also, the cast of malloc() is unncessary. See: What's wrong with casting malloc's return value?

scanf is not waiting for input

I'm trying to find the bug here, but still don't get it.
I've been debugging and googling it and found some close topics, but there are only solutions which I don't need ATM, and I'm curious why this code is not working:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define BUFFER 256
int main()
{
int missionCode;
char *desc = (char*)malloc(sizeof(char)*BUFFER);
do {
printf("Please enter the mission code (or -1 for exit): ");
scanf("%d", &missionCode);
fflush(NULL);
if (missionCode==-1)
return 1;
} while (missionCode>10);
do {
printf("Please enter a string:\n");
scanf("%[^\n]s", desc); //it doesn't stop here!
fflush(NULL);
if (!strcmp("exit",desc))
return 1;
} while (strlen(desc)<20);
printf("your string:\n%s", desc);
return 0;
}
There's something wrong with the scanf\flushall in the second loop, but I don't find out what.
BTW, this is C ofcourse.
scanf("%d", &missionCode);
leaves the newline in the buffer, so
scanf("%[^\n]s", desc);
immediately finds one and stops. You can add a space
scanf(" %[^\n]s", desc);
to the format to skip initial whitespace.

Trying to call custom function in C but scanf( ) is causing an issue

Can you tell what wrong here?
#include <stdio.h>
#include <stdlib.h>
int test (void)
{
int i;
printf("Enter a number: ");
scanf("%d",&i);
return i;
}
int main (void)
{
test();
return 0;
}
This is just a simple example but for some reason main doesn't run unless I get rid of the scanf.
Always use a '\n' at the end of your printf string. This makes the output buffer flush and print the string. Add more prints in your program.
You can rewrite your program like following, and the prints will help you understand what is happening with your program.
#include <stdio.h>
#include <stdlib.h>
int test (void)
{
int i;
printf("Enter a number: \n");
scanf("%d",&i);
printf("You just eneterd : %d\n",i);
return i;
}
int main (void)
{
printf("About to call test() \n");
test();
printf("Done calling test() \n");
return 0;
}
Better get a good C programming book for understanding these basic stuff. I suggest The C programming language
I think you must make use of fflush() or making use of a '\n' character at the end of the printf function which will eventually flush the std output buffer. For checking just make use of the printf() for printing the value of the variable just after reading the value.
Hope that helps....

Resources