Getting error `\365\277\357\376 in the terminal [closed] - c

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);

Related

How to copy all chars(include '\0') from one array to another without using strcpy function? [closed]

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
copy all characters from one character array to another without using strcpy function.
char s1[80],s2[80];
int i;
printf("input s2");
scanf("%s",&s2);
for(i=0;i<=strlen(s2);i++)
s1[i]=s2[i];
printf("s1:%s",s1);
Instead of scanf, using gets function for getting input with spaces.
#include<stdio.h>
int main()
{
char s1[80],s2[80];
int i;
printf("input s2");
// scanf("%c",&s2);
gets(s2);
printf("%d",strlen(s2));
for(i=0;i<strlen(s2);i++)
{
s1[i]=s2[i];
}
printf("s1:%s",s1);
}

Is it possible to make a string such as "G" equal a specific number like 50? [closed]

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);

How to censor a word in a char array? [closed]

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

Differences between scanf and getchar in C [closed]

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
I was trying to explain to my friend something about C coding and he asked me why his code (with "scanf") didn't work.
#include
int main() {
char x=scanf("%c",&x);
printf("%c\n",x);
return 0;
}
and this one yes
#include <stdio.h>
int main()
{
int k;
char x=getchar
printf("%c\n",x);
return 0;
}
When scanf completes, x contains the character that was read. However, that value is immediately overwritten when x is assigned the return value of scanf, which is the number of items successfully matched or EOF in the event of an error.
If you call scanf without assigning the return value to x you should get the expected result.
For example, this should work.
char x;
scanf("%c",&x);

A function to count how many different characters are in a string [closed]

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

Resources