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
Related
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 last year.
Improve this question
I would like to ask you if it was possible to get the characters common to two strings without having to resort to a loop on the character array. I wonder why this could greatly affect the total cost (asymptotically for n-> infinite) of algorithms such as eg. Charm or Eclat (just think that it would be like adding a new cycle to those already present). Thank you.
Specifically, the algorithm I am referring to is the following. As can be seen from the photo (line 6) it is necessary to obtain the intersection by iterating on the indices i and j, so I suppose it is necessary to iterate. I guess I get an O(m + n) best assuming the insert and search operations use O(1).
If your characters are byte-encoded (US-ASCII, KOI8-R, etc), you can create array, where your char is index, and iterate 1st string, and set "1" here. Thereafter, iterate 2nd string, and print only chars, presents in the array. See the example:
void print_intersection(const unsigned char *s1, const unsigned char *s2) {
unsigned char arr[0x100], c;
bzero(arr, sizeof(arr)); // cleanup
while(c = *s1++)
arr[c] = 1;
while(c = *s2++)
if(arr[c] != 0) {
putchar(c);
arr[c] = 0; // Disable print dups
}
}
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 3 years ago.
Improve this question
The task is to output every second word from the string
I made a loop to check every character in the string and while it reaches a space the boolean will change it value to the opposite so before it reaches the next space it will copy the characters from first string to the second
int main(void)
{
char str1[max]="Hellow my name is Tom why not today";
char str2[max];
int i;
bool a=false;
for (i=0;i<strlen(str1);i++)
{
if ((int)str1[i]==32)
{
a=!a;
}
if (a==true)
{
str2[i]=str1[i];
}
}
printf ("%s\n",str2);
return 0;
}
The terminal shows: `\365\277\357\376 my\365\277\357\376 isu
You will need different indexes for str1 and str2. When 'a' is false you do not write to str2[i] for that value of i. This will result is random data in the bytes you don't write. This is why you see the garbage data between every 2nd word.
int dest_index = 0;
for (i=0;i<strlen(str1);i++)
{
if ((int)str1[i]==32)
{
a=!a;
}
if (a==true)
{
str2[dest_index++]=str1[i];
}
}
str2[dest_index] = 0; // Null terminate the destination string
printf ("%s\n",str2);
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.
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 7 years ago.
Improve this question
In Python I can define a list, and print it all with one command:
lst = [1,2,3]
print lst
[1,2,3]
Is there any equivalent command in C ? (without using loops), or do I have to print every element by himself?
Thanks!
You can use recursion like in this pseudo code:
void print_array(item array[], size_t len)
{
if (len == 0)
return;
print_item(*array);
print_array(array + 1, len - 1);
}
Modern C compilers can optimize away tail-recursion, so this is likely not much less efficient than a loop.
If it's a string then yes, you can dump the entire contents of the array as long as it's \0 (nul) terminated...fprintf and the likes dump entire arrays all of the time.
char *p;
char string[12] = "A string";
for (p = &string[0]; *p != '\0'; p++)
fprintf(stdout, "%c", *p);
Is akin to
printf("%s", string);
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 8 years ago.
Improve this question
I've found this function on the internet and I find it to be very useful
but I am new to programming, and could someone please explain briefly what does it exactly do
#include <stdio.h>
int diffcount(char* s)
{
unsigned char seen[127];
int cnt=0,i;
for(i=0;i<127;i++)
seen[i]=0;
for(i=0;s[i];i++)
{
if(!seen[(int)s[i]])
{
cnt++;
seen[(int)s[i]]=1;
}
}return cnt;
}
int main(void) {
char string[20];
scanf("%s",string);
printf("Razlicitih znakova: %d\n", diffcount(string));
return 0;
}
First of all we init an empty array of zeros int seen[127];
"seen" array is used to find out whether char with code i has been met in the array s : if seen[i]==1 than (char)i was in the string s.
After that we make a loop through char* s and check if char s[i] has already been met by looking at the value of seen[s[i]]; and if it is false we put seen[s[i]]=true (because we met it) and increase our counter.
The result of the function is the value of variable cnt
This may also help:
each char has it's code between zero and 127. For example, (int)'a' = 97.
bool in the C is just the same as int, that's why we sometimes use 0 and 1 instead of true and false