Since, i am kinda very new to C-language I have to build a program which work user-friendly. Like user will give instructions of i need A to Z alphabets and after execute he will get result by using while condition. Is that possible ?.
#include <stdio.h>
int main()
char c;
for(c='A'; c<='Z'; ++c)
printf("%c ",c);
return 0;
}
Like that if I can write it using hard coded values, What if I have to give input From which Alphabet to Alphabet you want result and it give result till user want using while loop.
#include <stdio.h>
int main()
{
char c;
char b;
printf("Enter the first letter\n");
c=getchar();
getchar();
printf("Enter the last letter\n");
b=getchar();
getchar();
for ( ;c<=b; ++c)
{
printf("%c", c);
}
return 0;
}
Related
for(int i=0;i<num; i++)
{
char word[32];
scanf(" %[^\n]s",word);
makeDictionary(word, readDictionary);
}
I have a program where I want to ask user for certain strings n times (with spacing allowed). However, when they input for example n = 2, it only loops once and exists. I know there is something wrong with my scanf.
I do Java and I'm a beginner at C. The way strings are done is very different.
This code can help you:
#include <stdio.h>
#include <string.h>
int main()
{
int N;
do
{
printf("Give me number of words :");
scanf("%d",&N);
}while(N<1);
for(int i=0;i<N;i++)
{char str[20];
printf("\nGive me the %d word :",i+1);
scanf("%s[^\n]",str);
printf("%s",str);
}
}
I started learning C language yesterday and made a simple binary coverter. It works fine on my PC, however, it doesn't work on most online code runners(compilers) except for could9. It looks like the scanf function are not compatible with online IDE in general?
Here's the code.
#include <stdio.h>
#include <string.h>
int main(){
int a;
int b;
int c[255];
char opt[100];
printf("10進数の値を入力してください > ");
scanf("%d", &a);
printf("計算式を表示しますか? y(yes) or n(no) > ");
scanf("%s", opt);
if(strcmp(opt, "y") == 0){
printf("\n計算式: \n");
}
int i = 0;
while(a > 0){
b = a / 2;
c[i] = a % 2;
if(strcmp(opt, "y") == 0){
printf("%d ÷ 2 = %d 余り %d\n", a, b, c[i]);
}
a = b;
i++;
}
printf("\n2進数: ");
int j;
for(j = i-1; j >= 0; j--){
printf("%d", c[j]);
}
printf("\n");
return 0;
}
Any advice will be much appreciated.
My psychic powers tell me you are expecting these online compilers to prompt you for input (like running the program in a terminal window). Unfortunately, this is not the case for most online compilers. Instead, there is a text box somewhere where you type in all of the input that you want in your program's standard input.
For example, take this small program:
#include <stdio.h>
int main()
{
char c;
printf("Gimme a character! ");
fflush(stdout);
scanf(" %c", &c);
printf("You typed in '%c'! Yay! :)\n", c);
}
Compiling and running this in a terminal could produce the following in the window (input is in bold):
Gimme a character! f
You typed in 'f'! Yay! :)
But running this in an online compiler that doesn't prompt you for input could look like this:
Gimme a character! You typed in ' '! Yay! :)
By typing input into the text box provided (where it is depends on the online compiler) you can give input to the program that way.
STDIN:
f
Output:
Gimme a character! You typed in 'f'! Yay! :)
Problem Statement
I'm facing difficulty in solving a programming contest problem, which reads as follows:
You are given T name(s) in english letters. Each name will include some
of the uppercase letters from A to Z, some of the lowercase letters
from a to z and some spaces. You have to transform the name(s) from
lowercase to uppercase. Letters that are originally uppercase
will remain the same and the spaces will also remain in their
places.
Sample Input-Output
If I type this in...
5
Hasnain Heickal Jami
Mir Wasi Ahmed
Tarif Ezaz
Mahmud Ridwan
Md Mahbubul Hasan
the computer should output this...
Case 1: HASNAIN HEICKAL JAMI
Case 2: MIR WASI AHMED
Case 3: TARIF EZAZ
Case 4: MAHMUD RIDWAN
Case 5: MD MAHBUBUL HASAN
Note that exactly one space is required between the semi-colon and the initial letter of the name.
My Coding
This is what I've coded in C:
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
int main(void)
{
int T, i;
char string [100];
scanf("%d", &T);
for (i=0; i<T; i++)
{
gets(string);
printf("Case %d: ", i);
while (string[i])
{
putchar (toupper(string[i]));
i++;
}
printf("\n");
}
getch();
return 0;
}
Now, this code fails to produce the desired output. Where am I doing it wrong? Is there any matter with my syntax? Can somebody guide me? Please bear in mind that I'm a middle-schooler and just a beginner in C.
You need to cycle over each letter of the string one-by-one.
In this code below, I have done that with variable K, which goes from 0 to the length of the string.
Variable I keeps track of the number of strings.
int main(void)
{
int T, i, k;
char string [100];
scanf("%d", &T);
for ( i = 0; i < T; ++i)
{
gets (string);
for(k=0; k<strlen(string); ++k)
{
putchar (toupper(string[k]));
}
}
getch();
return 0;
}
In response your question: IDEOne Link
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void)
{
int T, i,k;
char string [100];
scanf("%d ", &T);
for ( i = 0; i < T; ++i)
{
gets (string);
printf("[%d] : %s\n", i, string);
for(k=0; k<strlen(string); ++k)
{
putchar (toupper(string[k]));
}
putchar('\n');
}
return 0;
}
Please go through the code and implement the test cases scenarios as per your requirement.
#include <stdio.h>
#include<string.h>
int main(){
char string[100];
int i;
scanf("%s",string);
for(i=0;i<strlen(string);i++){
string[i]=string[i]-32;
}
printf("%s",string);
return 0;
}
So i am just starting to learn c programming and i decided to practice by making my program speak to the end-user by asking questions and reacting to
them
I first applied if-else statement for the program to react on the age of the person.
then, when i get to the whats your favorite color part with the switch statements it would just close upon pressing any button.
My code:
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <ctype.h>
main()
{
char name[25], c;
int a;
clrscr();
printf("Hello, whats your name? \n");
scanf("%s",name);
printf("nice to meet you %s!!!\n");
printf("Whats your age?");
scanf("%d",&a");
{
if((a <= 21) && (a >= 0))
printf("Young!\n");
else if((a <= 100) && (a >= 22))
printf("old!\n");
else
printf("that's not an age!\n");
}
printf("whats your favorite color? \n"); //this is where the program stops//
scanf("%c",&c);
switch(tolower(c)){
case 'r':printf("Fiery!");break;
case 'o':printf("oranggerrr!!");break;
.
. //basically applied the whole rainbow and put some reactions//
.
getch();
return 0;
}
Ok, so I compiled the program online here, making some changes, as the program you have given only compiles in the old Turbo C.
I compiled the following program:
#include <stdio.h>
//#include <conio.h>
//#include <dos.h>
#include <ctype.h>
main()
{
char name[25], c;
int a;
//clrscr();
printf("Hello, whats your name? \n");
scanf("%s",name); //still dont get why it worked without the "&"//
printf("nice to meet you %s!!!\n");
printf("Whats your age?");
scanf("%d",&a);
{
if((a <= 21) && (a >= 0))
printf("Young!\n");
else if((a <= 100) && (a >= 22))
printf("old!\n");
else
printf("that's not an age!\n");
}
printf("whats your favorite color? \n"); //this is where the program stops//
scanf("%c",&c);
switch(c){
case 'r':printf("Fiery!");break;
case 'o':printf("oranggerrr!!");break;
// .
//. //basically applied the whole rainbow and put some reactions//
//.
}
getchar();
return 0;
}
Okay, so when I executed it, I got a segmentation fault, because of this line:
printf ("nice to meet you %s!!!\n");
to
printf ("nice to meet you %s!!!\n", name);
Then everything worked perfectly.
Now your doubt:
scanf ("%s", name);
That works because name is a char array, and the name of an array is equal to the address of the first element in that array. Check this comment. Also, you can see the question.
Improvements:
Change scanf ("%c", &c); to scanf (" %c", &c);
NOTE: I cannot exactly reproduce your problem as I do not have Turbo C. Also, maybe your program crashed because of tolower. Note that tolower return the changed char, so you will have to do: c = tolower (c);
#include <stdio.h>
#include <string.h>
int main(void) {
int i, j, t;
scanf("%d", &t); // enter the number of test cases
getchar();
char input[11111];
for(i=0; i<t; i++){
scanf("%[^STOP]", input); // take input till STOP will come
printf("%s\n", input);
// this code print first input as many time as number of test cases in the code that will we provide through variable t;
}
return 0;
}
Change your code to
#include <stdio.h>
#include <string.h>
int main(void) {
int i, j, t;
char ch;
scanf("%d", &t); // enter the number of test cases
getchar();
char input[11111];
for(i=0; i<t; i++){
scanf("%[^STOP]", input); // take input till STOP will come
while((ch=getchar())!='\n'&& ch!= EOF);
printf("%s\n", input);
// this code print first input as many time as number of test cases in the code that will we provide through variable t;
}
return 0;
}
The problem was caused because of buffering. scanf() sends \n to the input buffer and the next scanf() reads this. That is what caused the problems.
If you want to know more about this, just search for input buffer in Stack Overflow or in Google. This problem has been discussed over and over many times.