hi my program is to enter a number which gives the length of the string then the string and then finally a letter which should then tell me how many times that letter is in the string. Currently to help me figure out what is wrong with my code i can see that the strcmp is resulting in the same ascii number but negative. eg for the letter a the number is 97 but the strcmp is giving out -97 so the strcmo doesnt show the character as being in the string and results in the incorrect result. Any help would be greatly appreciated. thanks
#include
#include
int main(void)
{
char myChar[100], z, k;
int counter, n, g=0, r, i, l;
counter=0;
scanf("%d",&n);
while (counter<n)
{
counter++;
scanf(" %c",&myChar[counter]);
}
scanf(" %s", &z);
for(i=0;i<n+1;++i)
{
k=myChar[i];
r=strcmp(&z, &k);
l=r;
//printf("\n%c", myChar[i]);
printf("%d\n", l);
if(r==0)
{
g++;
printf("%d\n", g);
}
}
printf("\n\n%d\n", g);
return (0);
}
#include <stdio.h>
#include <string.h>
int main(void){
char myChar[100], str[64];//
int counter, n;
int i, j, k;
printf("number of charactors : ");
scanf("%d",&n);
for (i=0; i<n; ++i){
scanf(" %c", &myChar[i]);
}
printf("input string : ");
scanf("%63s", str);//"%s" : The useless one letter to input as a string. "%c" for &z
for(i=0;i<n;++i){
counter = 0;
for(j=0;str[j]!='\0'; ++j){
k = (str[j] == myChar[i]);//Comparison of each character
counter += k;//if(str[j] == myChar[i]) ++counter;
}
printf("%c is %d\n", myChar[i], counter);
}
return (0);
}
Related
I had a homework. The condition was: "Make a program in witch will be displayed how many 'A' and 'B' where typed". I tried and this is what i made:
#include <stdio.h>
int main()
{
int n, i, a=0, b=0;
char cr;
scanf ("%i", &n);
for (i=0; i<=n; i++) {scanf ("%c", &cr); if (cr='A') a++; else if (cr=='B') b++;}
printf ("A-%i\n", a);
printf ("B-%i", b);
}
The problem is when i type for example 10, it only lets me type 5 characters, not 10. Why?
The program can be made with for, while or do while.
Problems and solutions
you need to use scanf(" %c", %cr) to prevent the program from reading trailing whitespaces
You should be using if (cr == 'A') and not if (cr = 'A') because you are comparing and not assigning
the loop should be for (i = 0; i < n... not for (i = 0; i <= n.. as the loop would ask for 1+ input from the specified range
#include <stdio.h>
int main() {
int n, a = 0, b = 0;
char cr;
printf("Enter number of tries :\n");
scanf ("%i", &n);
for (int i = 0; i < n; i++) {
printf("Enter char\n");
scanf(" %c", &cr);
if (cr == 'A') a++;
else if (cr == 'B') b++;
}
printf ("A-%i\n", a);
printf ("B-%i", b);
}
using namespace std;
int main() {
int input;
int i=0;
while (1){
scanf("%d", &input);
printf("%d input:%d\n", i, input);
i++;
}
}
Stdin Inputs:
10
65
100
30
95
.
.
.
Is there a way to stop the code and escape from while loop after hitting the last Input?
Amount of Inputs can be N.
edition) Is there a way to calculate the amount of Stdin Inputs? This is my major question.
using namespace std;
int main() {
int input;
int i=0;
while (1){
scanf("%d", &input);
printf("%d input:%d\n", i, input);
i++;
if (i >= 5) break; //change 5 to your desired amount of inputs
}
}
Maybe you are reading an input that has an EOF (End of file)?
In that case you should stop when receiving EOF
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int input;
int i = 0;
while (scanf("%d", &input) != EOF){
printf("%d input:%d\n", i, input);
i++;
}
printf("End\n");
return 0;
}
Otherwise you can do a program that first reads N and then iterate N time
int main(void)
{
int input;
int i = 0;
int n = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &input);
printf("%d input:%d\n", i, input);
}
return 0;
}
I want to have unknown amount of inputs in a single line. For example, user can input:
"ans: 1 2 3 4 5"
and scanf() will store these five numbers to an array. The problem is that the program don't know how many input will there be.
#include <stdio.h>
int main()
{
int i;
int input[4];
scanf("ans: " for(i = 0, i < 3,i++){scanf(" %d", &input[i]);};
return 0;
}
Sorry I'am totally new to coding, what will be the proper way to write this? Or is this impossible?
Thanks :)
Use fgets() and sscanf() with "%n"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char input[100];
int arr[10];
//fgets(input, sizeof input, stdin);
strcpy(input, "1 2 42 56 -3 0 2018\n"); // fgets
char *pi = input;
int tmp, pp, i = 0;
while (sscanf(pi, "%d%n", &tmp, &pp) == 1) {
if (i == 10) { fprintf(stderr, "array too small\n"); exit(EXIT_FAILURE); }
pi += pp;
arr[i++] = tmp;
}
printf("got this ==>");
for (int k = 0; k < i; k++) printf(" %d", arr[k]);
puts("");
}
You asked this question way round.
You can achieve what you expect by putting scanf inside of a loop.Even you can ask user to give how many inputs he want to enter.
#include <stdio.h>
int main()
{
int i;
int input[4];
printf("Enter the number of inputs you want to give : ");
scanf("%d", &n);
for(i = 0; i < n;i++)
{
printf("Enter the input number %d : ",i);
scanf("%d", &input[i]);
}
return 0;
}
I want to scan and print two strings one after another in a loop.But I cannot do it.Only one string gets scanned and printed if i use the loop.If i try to print without the loop then the two "gets()" work properly.
#include <stdio.h>
int main()
{
int T,i,j;
char name1[100];
char name2[100];
scanf("%d",&T);
for(i=0; i<T; i++)
{
printf("Case %d: ",i+1);
//scanf("%[^\n]s",name1);
gets(name1);
/*for(j=0; j<strlen(name1); j++)
{
printf("%c",name1[j]);
}*/
puts(name1);
//scanf("%[^\n]s",name2);
gets(name2);
/*for(j=0; j<strlen(name2); j++)
{
printf("%c",name2[j]);
}*/
puts(name2);
}
}
Here you go. Use fflush(stdin). It will take two inputs and print them one after the another.
#include<stdio.h>
int main()
{
int T,i,j;
char name1[100];
char name2[100];
scanf("%d",&T);
for(i=0; i<T; i++)
{
printf("Case %d: ",i+1);
fflush(stdin);
gets(name1);
gets(name2);
puts(name1);
puts(name2);
}
return 0;
}
Edit: As suggested in the comment below, using gets() is not advisable if you do not know the number of characters you wish to read.
After taking testcase from the user, next line gets() function will take the '\n' you have to ignore the scenario.
Here's a tricky solution of this problem. Just use '\n' after %d in scanf function. scanf("%d\n",&T);
#include <stdio.h>
int main(void) {
char s1[100],s2[100];
int i,T;
scanf("%d\n",&T);
for(i = 0; i < T; i++){
printf("Case %d: ",i+1);
gets(s1);
puts(s1);
gets(s2);
puts(s2);
}
return 0;
}
You do not terminate your prints.
stdout is buffered.
Print is only performed after a "\n" or explicit flush.
try something around the lines:
#include <stdio.h>
int main()
{
int T,i,j;
char name1[100];
char name2[100];
scanf("%d",&T);
for(i=0; i<T; i++)
{
#ifdef BAD_CODE
printf("Case %d: ",i+1);
gets(name1);
puts(name1);
gets(name2);
puts(name2);
putchar("\n");
#else //better code
fgets(name1, sizeof(name1)-1, stdin);
fgets(name2, sizeof(name2)-1, stdin);
printf("Case %d: '%s' '%s'\n",i+1, name1, name2);
#endif
}
}
I was trying to co
So, I don't understand the reason why my compiler is not letting me take input when the convhex() is called from the main. It's directly printing some result.. I don't understand this.
Here's the code..
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <String.h>
void convhex();
void convert(int no, int base);
int checkValid(int base,int no);
// function prototyping done here
void convhex()
{
char ch[10];
int dec=0;
int i, res;
printf("Enter the hexadecimal number \n");
scanf("%[^\n]", ch);
// print in decimal
for(i=strlen(ch)-1;i>=0;i--)
{
if(ch[i]>65)
res=ch[i]-65+10;
else
res=ch[i]-48;
//printf("%d", res);
dec=dec+pow(16,strlen(ch)-(i+1))*res;
}
printf("\nThe number in decimal is %d \n", dec);
}
int checkValid(int base,int no)
{
int rem;
//flag;
// flag=0;
while(no>0)
{
rem=no%10;
if(rem>base)
{
//flag=1;
//break;
return 0;
}
no/=10;
}
return 1;
/*
if(flag==1)
printf("Invalid Input");
else
printf("Valid Input");
*/
}
void convert(int no, int base)
{
int temp, mod, sum=0, i=0;
temp=no;
while(temp>0)
{
mod=temp%10;
temp=temp/10;
sum=sum+pow(base,i)*mod;
i++;
}
printf("\n The number in base 10 is %d", sum);
}
int main()
{
int base, no;
printf("Enter the base \n");
scanf("%d", &base);
if(base==16)
convhex();
else
{
printf("Enter the number \n");
scanf("%d", &no);
printf("You have entered %d", no);
if(checkValid(base, no))
convert(no, base);
}
return 0;
}
// up until now our program can work with any base from 0-10 but not hexadecimal
// in case of hex, we have A-F
scanf in convhex is reading the \n left by the scanf in main.
Try this
scanf(" %[^\n]", ch);
^ An extra space will eat any number of white-spaces.
you can just remove the %[^\n] from scanf in connhex to change it to:
scanf("%s", ch)
or you could do what haccks suggested in the above post.