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 2 years ago.
Improve this question
I'm trying to write a function, that deletes each character in s1, that matches any character in the string s2.
#include <stdio.h>
#include <string.h>
void squeeze(char s1[], char s2[])
{
int i1, i2, j, contains_char;
for(i1 = j = 0; s1[i1] != '\0'; ++i1){
contains_char = 0;
for(i2 = 0; s2[i2] != '\0'; ++i2)
contains_char += s1[i1] == s2[i2];
if(!contains_char)
s1[j++] = s1[i1];
}
s1[j] = '\0';
}
int main()
{
char test[5] = "Test";
char x[2] = "et";
squeeze(test, x);
printf("%s", test);
}
But when running the code it prints nothing. When i debugged it in gdb i found out that the variable s2 in squeeze contains the string "etTest".
Can someone tell me why this is happening ?
Your variable s2 is not containing "etTest". It`s just your 2 variable (s1 and s2), storing in one memory location in stack. (comments about s2[3] is right )
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 1 year ago.
Improve this question
Can anyone please help me.I want to remove character from char* in C.
For example, char *str this str equals to "apple" and first i remove 1. character and define a new variable then remove 2. character and define new variable and so on until the str ends.
How can ı do that ? I'm completely new to C, and just can't seem to figure it out
Assuming you only need to remove characters from the left.
Because strings in C are arrays of characters, and arrays are pointers to the first element of the array, you can create variables that point to specific indices in you original char array.
For example
char* myString = "apple";
char* subString = myString+1;
printf("%s\n%s\n", myString, substring);
would produce the output
apple
pple
We could generalize this to store all substrings obtained by removing characters from the left in an array of char* pointers (pointers to pointers). Like so:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char** GetSubStrings(char* input)
{
int length = strlen(input);
char** result = calloc(length, sizeof(char*));
for(int i = 0; i < length; i++)
{
result[i] = input + i;
}
return result;
}
int main()
{
char* myString = "apple";
char** subStrings = GetSubStrings(myString);
for(int i = 0; i < strlen(myString); i++)
{
printf("%s\n", subStrings[i]);
}
free(subStrings);
return 0;
}
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 2 years ago.
Improve this question
i was googling an exercise that takes the 2 arrays and print the output without character duplication then i found this one which it was more easy to read and modify but the thing is i did not understand the meaning of int temp[256 + 128] = {0}; and temp[+str[i]] = 1;
here is the full code
#include <unistd.h>
void remove_dup(char *str, char *str2)
{
int temp[256 + 128] = {0};
int i;
i = 0;
while (str[i])
{
if (temp[(int)str[i]] == 0)
{
temp[+str[i]] = 1;
write(1, &str[i], 1);
}
i++;
}
i = 0;
while (str2[i])
{
if (temp[+str2[i]] == 0)
{
temp[+str2[i]] = 1;
write(1, &str2[i], 1);
}
i++;
}
}
int main(int argc, char **argv)
{
if(argc == 3)
remove_dup(argv[1], argv[2]);
write(1, "\n", 1);
return(0);
}
This:
int temp[256 + 128] = {0};
Creates an array of int of size 256+128 = 384, explicitly initializes the first element to 0, and implicitly initializes the rest to 0.
And this:
temp[+str[i]] = 1;
Contains an example of the unary + operator which is analogous to the unary - operator. This operator effectively does nothing, so this expression is the same as:
temp[str[i]] = 1;
Which uses str[i] as the index into array temp and assigns the value 1 to that element.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
This is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int appar(char c[], char x);
int main() {
char c[] = "hello everyone!";
int b = appar(c, 'h');
printf("nbr of h is %d ", b);
return 0;
}
int appar(char c[], char x) {
int i = 0, cmpt = 0;
int q = strlen(c);
for (i; i < q; i++) {
if (c[i] == 'x')
cmpt++;
}
return cmpt;
}
I run and compile the program, but I receive "nbr of h is 0".
What's the wrong in this code?
Change c[i]=='x' to c[i]==x
You want to compare with the variable x, not the character constant 'x'
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
So I've got a problem with int and string in C.
My task is to write a function that will output the error rate in string.
For example if the string is "aaabbbcccdxm" then the output should be "1/12".
By "error" I mean any letter from n to z, and any letter from a to m is "good".
I thought that I could do it by using a for loop to check every letter in the string, and then if to add value to int error which would be numbers of bad letters, but I don't know how to convert that "int error" value to string with output error value/string dimension. Any ideas?
You can use printf to format your output. I recommend reading the man 3 printf on a linux machine or from google.
Here is what such a program could look like:
#include <stdio.h>
#include <string.h>
int main()
{
const char * input_str = "aaabbbcccdxm";
int size = strlen(input_str);
int error = 0;
for (int i = 0; i < size; ++i)
{
if (input_str[i] >= 'n')
error++;
}
printf("%d/%d\n", error, size);
return 0;
}
size_t errors(const char *str, const char *legal)
{
size_t errcnt = 0;
while(str && *str)
{
errcnt += !strchr(legal, *str++);
}
return errcnt;
}
int main()
{
char *str = "aaabbbcccdxm";
printf("%zu/%zu\n", errors(str,"abcdefghijklm"), strlen(str));
return 0;
}
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
Write a definition of the function bitwise_swap that uses only bit
wise assignment operators to swap the values of two strings.
I tried to iterate over each char, changing into an int and swapping
using
a ^= b;
b ^= a;
a ^= b;
Once I had the char int value but it didn't seem to work.
Thanks in advance for the help
It sounds like you attempted something like this, which should work fine.
void bitwise_swap(char * restrict lhs, char * restrict rhs, size_t length) {
size_t i;
for (i=0; i<length; ++i) {
lhs[i] ^= rhs[i];
rhs[i] ^= lhs[i];
lhs[i] ^= rhs[i];
}
}
#include <stdio.h>
#include <string.h>
//#include <stdbool.h>
int main()
{ int m,n,t,i;
char a[]="National University";
char b[]="India";
char c[100];
char d[100];
m=strlen(a);
n= strlen(b);
if(m>n)
{ t=m;
// strcpy(&c,&a);
for(i=0;i<n;i++)
d[i]=b[i];
for(i=0;i<m-n;i++)
d[n+i]=32;
for(i=0;i<t;i++)
{
a[i]=a[i]^d[i];
d[i]=d[i]^a[i];
a[i]=a[i]^d[i];
}
printf("a= %s \t b=%s" ,a,d);
}
else
{ t=n;
// strcpy(&d,&b);
for(i=0;i<m;i++)
c[i]=a[i];
for(i=0;i<n-m;i++)
c[m+i]=32;
for(i=0;i<t;i++)
{
c[i]=c[i]^b[i];
b[i]=b[i]^c[i];
c[i]=c[i]^b[i];
}
printf("c= %s \t d=%s" ,c,b);
}
return 0;
}
This way you can do it.You just need a loop for swapping each character.
EDIT: Now its dynamic. You need not to specify length manually and I am appending NULL character at the end of shorter string. Look result at :http://ideone.com/B7lsz4