Defining a letter as a character - c

Basically what i'm trying to do is to define letters as a number so if i give a letter to my program it treats it like a normal number.
I tried to do it using this code but it doesn't seem to work: If you have any suggestions i'd be thankful.
#include<stdio.h>
int main()
{
#define F 10
int liczba;
scanf("%d",liczba);
printf("%d",liczba);
}
I also tried to define it using the loop but then i was getting an error
#include<stdio.h>
int main()
{
int i;
for(i = 10; i<32; i++)
{
#define '55+i' i
}
}

If you want the input to be a letter,then you need a char variable instead of an int:
#include<stdio.h>
int main()
{
char liczba; //char variable
scanf("%c",&liczba); //%c for a character
printf("%d",liczba); //prints the ASCII value of the character
}
I read the comments and you said that you need A=10,B=11 etc. In that case,use
if(liczba>='A' && liczba <='Z')
printf("%d",(liczba-'A')+10);
The if is required to check if the input is between A and Z.

Related

Why did I get the wrong output and how can I fix this?

I tried to write a program to count the number of occurrences of a given character in a given string.
Here's the program:
#include <stdio.h>
#include <string.h>
int find_c(char s[], char c)
{
int count;
int i;
for(i=0; i < strlen(s); i++)
if(s[i] == c)
count++;
return count;
}
int main()
{
int number;
char s[] = "fighjudredifind";
number = find_c(s, 'd');
printf("%d\n",number);
return 0;
}
I was expecting the following output:
3
since the number of occurrences of the character 'd' in the string s is 3.
Each time I tried to run the program, a different number was displayed on the screen. For example, I got the following output while running the program one time:
-378387261
And got this output, when running the program another time:
141456579
Why did I get the wrong output and how can I fix this?
Thanks in advance!
Well, Your code is good. Only mistake is, you did not initialize the count to 0. If you do not initialize the variable will hold the garbage value and you will be performing operations on that value. As a result, in the earlier case, you got all the garbage values, when you execute the program each time.
Here is the code:
#include <stdio.h>
#include <string.h>
int find_c(char s[], char c) {
int count=0;
int i;
for(i=0; i < strlen(s); i++)
if(s[i] == c)
count++;
return count;
}
int main() {
int number;
char s[] = "fighjudredifind";
number = find_c(s, 'd');
printf("%d\n",number);
return 0;
}
In C Integers are not automatically initialized to zero.
The problem is that the count variable is not initialized.
Try initializing the count variable in the find_c function to zero.

why the atoi function is not working?

The program stops working.
Even if I put only one int.
I tried many different ways but can't figure out what is wrong.
I am trying to take input of integers separated by space.
There can be any no of integers.
#include<stdio.h>
#include<stdlib.h>
int main(void) {
int i,j=0;
int b[100];
char a[100];
fgets(a,100,stdin);
for(i=0;i<strlen(a);i++)
{
b[i] = atoi(a[j]);
j=j+2;
}
for(i=0;i<strlen(b);i++)
{
printf("%d ",b[i]);
}
}
Here is the prototype of atoi you have to use a character array but you are sending a character only.atoi(str[i])
int atoi(const char *str)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int val;
char str[20];
strcpy(str, "98993489");
val = atoi(str);
printf("String value = %s, Int value = %d\n", str, val);
return(0);
}
Do the following:
for(i = 0; i < strlen(a); i += 2)
{
b[j] = a[i];
j++;
}
though atoi() accepts only a string argument or u can say constant char pointer and str[i] is nothing but a pointer pointing at a single character of the character array.
Therefore once we pass atoi(str[i]) , that means we are passing a character to the function, which will give an error.
Therefore you must pass the address of that particular character from where you want to convert the substring into a number i.e. atoi(&str[i]).

Variable isn't storing return value

I am trying to make a program that converts a hex string into decimal. However I am having an issue assigning a returned integer value from the findLength function. Going by the printf statements I can tell that findLength(theString) will yield the correct value however length is showing a value of 0 despite the fact that I have length = findlength(theString).
This isn't a homework problem, I'm just absolutely stumped as to why this simple assignment isn't working. I've already declared length so I know that's not the issue. I'm also getting no compiler messages. Any help would be greatly appreciated.
Edit: I know convert doesn't do anything useful and the for loop needs to be fixed however that shouldn't be effecting the findLength return right?
Second Edit:
I've always submitted a string of '324' to be tested.
#include <stdio.h>
int convert(char s[], int theLength);
int findLength(char s[]);
int main(){
char theString[100];
int result;
int i;
int length;
printf("%s","Hello, please enter a string below. Press enter when finished.");
scanf("%s",theString); //Apparently scanf is bad but we'll learn better input methods later.
//For my tests I submitted a string of '324'.
length = (findLength(theString)); //length = findLength('324')
printf("%d",findLength(theString)); //yields 3
printf("%d",length); //yields value of 0 always.
result = convert(theString, length);
printf("%d\n result is",result);
return 0;
} //End of main
int convert(char s[], int theLength){ //This function will eventually converts a string of hex into ints. As of now it does nothing useful.
int i;
int sum;
for(i = theLength; i=0; i--){
sum = sum + s[i];
printf("%d\n",sum);
}
return sum;
} //End of convert
int findLength(char s[]){
int i;
for(i = 0; s[i]!='\0'; ++i){
}
return(i);
} //End of findLength
The variable length is storing the correct value. I think what has you confused is how you've laid out your printf statements. If you were to try something like the below it would be much easier to see that your code works.
#include <stdio.h>
int findLength(char s[]);
int main(){
char theString[100];
int result;
int i;
int length;
printf("Hello, please enter a string below. Press enter when finished.\n");
scanf("%s",theString);
length = (findLength(theString));
printf("findLength(theString) = %d\n",findLength(theString));
printf("length = %d\n",length);
return 0;
}
int findLength(char s[]){
int i;
for(i = 0; s[i]!='\0'; ++i){
}
return(i);
}
Just to clarify in your post you have
printf("%d",findLength(theString));
printf("%d",length);
printf("%d\n result is",result);
Note the \n before the %d in the last printf statement. This is 0 because your convert function needs to be fixed and this is the value of result NOT length.

Count number of same letters in a string

I have been reading "The C Programming Language" and I got to this problem, that my output is 0 for any given string I send.
My function looks like this:
int number_of_repeating(char *word,char k){
int b=0,len=0,i;
gets(word);
len=strlen(word);
for(i=0;i<len;i++){
if(word[i]==k)
b++;
}
return b;
}
Problem:
I send him word for example: Jhonny, and character n, so it should count number of n's in the word (in this case the output should be 2).
What am I doing wrong?
#include <stdio.h>
int number_of_repeating(char *word,char k){
int b=0,len=0,i;
gets(word); //<------- You need to remove this one because it may overwrite
len=strlen(word);
for(i=0;i<len;i++){
if(word[i]==k)
b++;
}
return b;
}
int main(void) {
// your code goes here
printf("%d",number_of_repeating("johnny",'n'));
return 0;
}
if you're passing the string in there is no reason to call gets(), that could be it or your types could be wrong.

Find the number of occurrence of each character in a string In C

I have a problem in C where i have to find number of occurrence of each character in a string.Suppose i have string like "amitamt" and output should be like "a2m2it2" .I have a routine from which i can find no of occurrence of a particular character.
int count_chars(const char* string, char ch)
{
int count = 0;
int i;
int length = strlen(string);
for (i = 0; i < length; i++)
{
if (string[i] == ch)
{
count++;
}
}
return count;
}
But I am not sure how could I count each character of string
If you have an ASCII string, create an int array of size 256. Then loop through the string and increment the value in the int array on position x. While x is the ASCII value of the character in your string you're looping through.
if i have any mistakes like syntax please excuse as im working on vb , Im unable to figure out where to put braces or brackets ,
and I belive strchr makes your task easier
#include <stdio.h>
#include <string.h>
int str_occ (char *pch ,char a)
{
int i = 0;
char *p;
p=strchr(pch,a);
while (p!=NULL)
{
i = i+1;
p = strchr(p+1,a);
}
return i;
}
To explain the code *pch is the string you have to pass ,char a is the alphabet you are searching to find how many times its occurring and int i returns the value of number of occurrences
say sample
int main()
{
char a[]="hello world";
int i;
i=str_occ(a,'l');
printf("%d",i);
}
output is 3
You can make the code as per your requirements, keep caling the function inside a loop , I mean rotate your elements

Resources