Swapping string using bitwise operation [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 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

Related

How to store numbers in malloc array? [closed]

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 11 days ago.
Improve this question
I want to use the count variable to store numbers in the W array using malloc, but the output keeps repeating the same number entered.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int *w = malloc( sizeof(int) * 10000);
int x; int count = 0;
while (scanf("%d", &x) == 1) {
printf("%d", &x]);
*(w + x) = count;
count++;
}
free(w); w = NULL;
return 0;
}
argc, argv are not used so leave them out.
Prefer sizeof a variable (*w) opposed to a type (int). If the type changes you only need to make the change in one place.
Use constants instead of magic values.
You don't need the temporary variable x.
Prefer unsigned (size_t) types to (unsigned) int for values that should not be negative. It eliminates a possible error class.
Prefer a for to while-loop when counting things. It might be more readable like this:
for(size_t count = 0;; count++) {
if(scanf("%d", &w[count]) != 1)
break;
printf("%d\n", w[count]);
}
printf("%d", ...) takes a value not an address. This appears to be the main issue.
For readability by user add a new newline when printing the value.
#include <stdio.h>
#include <stdlib.h>
#define LEN 10000
int main() {
int *w = malloc(LEN * sizeof *w);
for(size_t count = 0; scanf("%d", &w[count]) == 1; count++)
printf("%d\n", w[count]);
free(w);
}
example session:
$ ./a.out
1
1
2
2
4
4

Need help transferring my main function code into a separate function [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 10 months ago.
Improve this question
For a school assignment, I am to Create variables in main() that will store the counts and pass pointers to these variables to your functions so that the functions can modify the variables via the pointers. This is a school assignment so rather than someone Give me the answer, I would prefer is someone could help point me in the right direction of using pointers. The Code does work, but not in the way I would like yet.
the code is as follows
void myFunction(int *letters, int *numbers, int *otherCharacters){
}
int main(int argc, char * argv[]) {
// Code for command line argument
if (argc == 2) {
int letters = 0;
int numbers = 0;
int otherCharacters = 0;
int totalCharacters;
int length = strlen(argv[1]);
for (int i = 0; i < length; ++i){
if (isalpha(argv[1][i]) != 0)
++letters;
if (isdigit(argv[1][i]) != 0)
++numbers;
if (isdigit(argv[1][i]) == 0 && isalpha(argv[1][i]) == 0)
++otherCharacters;
}
totalCharacters = letters + numbers + otherCharacters;
printf("%i letters\n%i digits \n%i other characters\n%i characters total\n", letters, numbers, otherCharacters, totalCharacters);
}
I am hoping to rather than change the values of letters, numbers, otherCharacters, and totalCharacters in the main function use pointers to do so in myFunction(). any help on how to use pointers to do so would be much appreciated. Again, I am not asking for an answer, as I would like to complete this assignment myself.
Seems like the function is supposed to look at a string and tell you how many letters, numbers, and other characters there are. It needs to take the counts as pointers, and the string.
void countCharacters(const char *string, int *letters, int *numbers, int *other) {
....
}
Because they are pointers, when incrementing them you need to dereference them first to get their values. Instead of letters++ it would be (*letters)++.
And we can replace the main code to show how you'd call this.
int main(int argc, char * argv[]) {
// Exit early to avoid deeply nesting all the code.
if (argc != 2) {
perror("please supply a string");
return 1;
}
int letters = 0;
int numbers = 0;
int other = 0;
// Pass in the string (already a pointer) and the counts as pointers.
countCharacters(argv[1], &letters, &numbers, &other);
int total = letters + numbers + other;
printf("%i letters\n%i digits \n%i other characters\n%i characters total\n", letters, numbers, other, total);
}

printing strings with pointer arithmetics [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 1 year ago.
Improve this question
I'm trying to print random words with the use of rand().
I think that I've made a mistake on the pointer arithmetic since I get a weird output.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main ()
{
srand(time(0));
const int randomBit = rand() % 2;
char * sz[] ={"good", "bad"};
switch (randomBit)
{
case 0:
for (char *i=*sz; *i<(sz+3); i++)
{
printf("%c", *i);
}
break;
case 1:
for (char *i=*(sz+3); *i!=0x0; i++)
{
printf("%c", *i);
}
break;
default:
break;
}
return 0;
}
What's my mistake?
Thank you.
Given this declaration ...
char * sz[] ={"good", "bad"};
... this code has undefined behavior:
for (char *i=*(sz+3); *i!=0x0; i++)
The expression *(sz+3) is equivalent to sz[3], but array sz has only two elements, so the maximum index is 1.
Likewise, the expression *sz is equivalent to sz[0]. That one is semantically ok but stylistically poor. Likewise stylistically poor is splitting out two separate cases when you could instead cover both with the same code by using sz[randomBit] to select which string to print.
Furthermore, it is unclear why you are printing character by character. Perhaps that's part of the assignment, but in the real world a programmer would probably write ...
printf("%s", sz[randomBit]);
... instead of that entire switch statement.
Your code is incredibly complicated (and wrong).
What's wrong with this:
int main()
{
srand(time(0));
const int randomBit = rand() % 2;
char* sz[] = { "good", "bad" };
printf("%s\n", sz[randomBit]);
return 0;
}
or if you are not allowed to use the %s format specifier as part of your assignement:
for (char* i = sz[randomBit]; *i != 0; i++)
{
printf("%c", *i); // or putchar(*i);
}

Error saying assignment to expression with array type [closed]

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 2 years ago.
Improve this question
How do I resolve this? Thank you
struct node
{
char sym[100];
}s[20];
int main()
{
for(int i=0;i<5;i++)
s[i].sym=i;
return 0;
}
Not sure what the problem is, When I use an array to store the integer value/ ascii value I get this error. Why is that so? For a non array variable, the input is taken without errors
What you effectively have in your code is no different to:
char sym[100];
sym = 0;
You cannot assign an integer to an array. Your code would be more likely to work with:
int main() {
for (int i = 0; i < 5; i++) {
s[i].sym[SOMETHING] = i;
}
return 0;
}
Although, of course, you need to decide the correct value of SOMETHING.
You cannot put int in the array (char)
Try this...
#include <iostream>
using namespace std;
struct node {
char sym[100];
}s;
int main() {
int i = 0;
for(char a = '1'; a <= '5'; a++){
s.sym[i] = a;
cout << s.sym[i] << " ";
i++;
}
cout << endl;
return 0;
}

C Function automatically concatenates char array arguments [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 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 )

Resources