Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How can I search for a number in a variable?
For Example:
Input A : 12131415
Input Search : 1
Output : 1 = 4
Input A : 12131415
Input Search : 12
Output : 12 = 1
Please Help Me. Thank You.
I'll not provide a complete solution for you but here is a basic idea that you can work with.
You could convert the numbers into strings and then count the number of times you can find one string in the other.
Here are some of the basic function you can use. This should get you started.
int A = 12131415;
int B = 1;
int count = 0;
char strA[20];
char strb[20];
sprintf(strA, "%d", A);
sprintf(strB, "%d", B);
int lenB = strlen(strB);
char* m;
char* t = A;
m = strstr(t, strB);
if (m != NULL)
{
// Found a match
++count;
}
else
{
// No more matches
return;
}
// Move pointer
t = m + lenB;
// Now look for next match
m = strstr(t, strB);
//.... and so on....
Your task is to organize the above code in a loop so that you can go through the whole string A.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to create a program which asks to the user to type in two integers, from 0 - 10. The program then turns the integers into words and the result is also printed into words.
EXAMPLE:
Please enter two integers: 2 5
two + five = seven
You can create a simple array along the lines of (that ... isn't literal, I just couldn't be bothered typing out all the numbers):
char *nums[] = { "zero", "one", "two", ... "twenty" };
Then, for a given number n where 0 <= n <= 20, you can output the number with a simple:
printf ("%s", nums[n]);
So, other than the input part (which is almost certainly covered elsewhere on SO), the code would be:
int n1 = 2, n2 = 5;
printf ("%s + %s = %s", nums[n1], nums[n2], nums[n1 + n2]);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I had an interview yesterday where they had asked me to write a function which accepts 3 arguments,1 source , 1 destination and other the length and this function should copy the value from source to destination based on the length parameter and the types of source and destination could be different.
Can someone please help me write a generic function ?
Thanks a lot in advance
You mean memcpy (or memmove)? :P
A naive implementation (using bytes):
int my_memcpy(void *dest, const void *src, size_t len)
{
if (dest == NULL || src == NULL || src == dest) return -1;
if (len == 0) return 0;
char *dest_bytes = dest;
const char *src_bytes = src;
for(size_t i = 0; i < len; i++) {
dest_bytes[i] = src_bytes[i];
}
return 0;
}
One can optimise using uint64_t pointers (taking care of the remainder with a char *) and loop unrolling to copy more data each iteration of the for loop.
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 6 years ago.
Improve this question
For example if I had the letter "B" in a array and want to count how many there are,could I make "B"=1 so I can easily count the number of b's.I do not think this is typecasting since I am do not want to make "B" itself = int B
You don't need to assign to a string, just use an ordinary variable.
int b_count = 0;
char *string = "This is a B and this is another B";
for (char *p = string; *p != 0; p++) {
if (*p == 'B') {
b_count++;
}
}
printf("There are %d B's in the string\n", b_count);
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 6 years ago.
Improve this question
How can I find a word in a char array and censor it with wildcard (*) characters?
I've tried to find the first occurrence of the word, but failed. I've tried this and it didn't work either. I'm a newbie and I've been trying this for 5 hours.
int main()
{
int w,q;
char l,m;
char *arr, *swear;
int i,a,t,k,z;
printf("Enter a word which is not allowed.");
scanf("%s", swear);
printf("Now enter a word.");
scanf("%s", arr);
for(a=0; swear[a]!='\0'; ++a); //finding length of swear
for(t=0;arr[t]!='\0';t++); // finding length of arr
for(i=0,k=0;k<t & i<t;i++,k++)
{
arr[i]=l;
swear[k]=m;
if(strstr(swear,arr))
arr[i]='*';
else
break;
}
for(z=0;z<t;z++)
{
printf("%c",arr[z]);
}
return(0);
}
So you want to overwrite all occurrences of a string with an equal length string composed of '*'. For that you need to iteratively get pointers to occurrences of the string and you also need its length to know how many '*' you have to use.
size_t swearLength = strlen(swear); // swear is assumed null-terminated
char *here = arr;
while ((here = strstr(here, swear)) {
memset(here, '*', swearLength);
here += swearLength; // to avoid searching what's already censored
}
strstr will return null if it can't find swear so the loop will terminate
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 9 years ago.
Improve this question
I wanted to find out efficiently which drawers are Full. But the output has to be a number corresponding to the binary representation. For example if only the second Drawer is full (from left to right), then the output is 4:
8 4 2 1
0 1 0 0 (Drawer Two is Full)
So I used this approach.
int DrawersFull[4] = {0,0,0,0}; //Initially all are empty
for(i=0;i<4;i++)
{
if(IsDrawerFull) // the api was provided by the interviewer
DrawerFull[i]=1;
}
I am not sure how to generate the output. Any suggestions will be helpful. Thanks.
Interviewer gave me hint that it can be done using bitwise operators but I am not sure.
This is the same as converting binary number to decimal. Try this one:
int res = 0;
for (i = 4; i >= 1; i--) {
res = res * 2 + DrawerFull[i]; // Assuming DrawerFull will contain only 1 or 0.
}
char fullDrawers = 0;
for( char i = 0; i < 4; ++i )
{
if( IsDrawerFull )
fullDrawers &= 1;
fullDrawers <<= 1;
}