how to swap two character of a character array? in c [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 1 year ago.
Improve this question
How can you replace two characters in a character array? For example:
charecter array : peter
Replace the two characters p and t and give the following output:
teper
its my try(it is wrong):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void swap(char*,char*);
main() {
int n,i,j;
char str[30][30];
printf("how many names?:");
scanf("%d",&n);
for(i=0;i<n;i++) {
printf("name %d?",i+1);
scanf("%s",str[i]);
}
for(i=0;i<n;i++) {
char ch1,ch2;
printf(" which letters of name %d?:",i+1);
scanf("%c%c",&ch1,&ch2);
swap(&ch1,&ch2);
printf("\n %s",str[i]);
}
}
void swap(char *a,char *b){
char temp;
temp=*a;
*a=*b;
*b=temp;
}

You can do something like this
// you can use the same logic in c++
#include <stdio.h>
void swap(char * a, char * b) {
char temp = *a;
*a = *b;
*b = temp;
}
int main() {
char str[] = "peter";
printf("before -> %s\n", str);
swap(&str[0], &str[2]); // swap p and t
printf("After -> %s\n", str);
}

Related

Implementing string to int function in c [duplicate]

This question already has answers here:
returning a local variable from function in C [duplicate]
(4 answers)
Changing address contained by pointer using function
(5 answers)
Closed 1 year ago.
I am trying to implement a function as stated in the title. I think I am very close to solution but a problem.
input: 51% are admitted.
output: x:51 (null)
but output should have been:
s:% are admitted.
My try is here:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int str2int(int);
int isdigit(int);
long str2double(int);
int driver(char *, char *);
int main(){
char *s = "51% are admitted.";
char *sPtr;
int x = driver(s, sPtr);
printf("x:%d sPtr:%s", x, sPtr);
return 0;
}
int isdigit(int ch){
return (ch>=48 && ch<=57)?1:0;
}
int str2int(int ch){
return ch-48;
}
int driver(char *s, char *sPtr){
int i=0, number=0;
while(s[i]!='\0' && isdigit(s[i])){
number = number*10 + str2int(s[i]);
i++;
}
sPtr=s+i;
printf("%s\n", sPtr);
return number;
}
The problem is, in main, sPtr seems as null but in driver function, sPtr is % is admitted which is what it should be. How can I fix the problem so that I can print the solution correctly without using a printf statement in driver function?
EDIT:
The problem is as #Johnny Mopp said, I was trying to pass a copy of that variable. Therefore, I need to pass the address of variable of *sPtr which appears char **sPtr in prototype. And the code should be:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int str2int(int);
int isdigit(int);
long str2double(int);
int driver(char *, char **);
int main(){
char *s = "51% are admitted.";
char **sPtr;
int x = driver(s, &sPtr);
printf("x:%d sPtr:%s", x, sPtr);
return 0;
}
int isdigit(int ch){
return (ch>=48 && ch<=57)?1:0;
}
int str2int(int ch){
return ch-48;
}
int driver(char *s, char **sPtr){
int i=0, number=0;
while(s[i]!='\0' && isdigit(s[i])){
number = number*10 + str2int(s[i]);
i++;
}
*sPtr=s+i;
return number;
}
Thanks for contributes of #Johnny Mopp and #paulsm4

How to pass arguments in __VA_ARGS to a 2d character 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 7 years ago.
Improve this question
I need to get result from __VA_ARGS to a function, and from there I need to pass string of each argument to a 2d character array.
If you are under C99, you can use compound literals
#include <stdio.h>
#define MACRO(...) func( \
sizeof((char *[]){__VA_ARGS__}) / sizeof(char *), \
(char *[]){__VA_ARGS__} \
)
void func(size_t n, char **p)
{
size_t i;
for (i = 0; i < n; i++) {
printf("%s\n", p[i]);
}
}
int main(void)
{
MACRO("abc", "def", "ghi");
return 0;
}
Notice that __VA_ARGS__ are evaluated twice in order to get the number of elements using sizeof, as an alternative you can send NULL as last parameter (sentinel):
#include <stdio.h>
#define macro(...) func((char *[]){__VA_ARGS__, NULL})
void func(char **p)
{
while (*p != NULL) {
printf("%s\n", *p);
p++;
}
}
int main(void)
{
macro("abc", "def", "ghi");
return 0;
}

How to toupper char array 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 6 years ago.
Improve this question
whats wrong here?
I want to check if the char in char array islower, so if it is it should be changed in uppercase.
#include <stdio.h>
int main(int argc, char *argv[]) {
char arr[100];
scanf("%s",&arr);
for(int i=0;i<sizeof(arr);i++){
if(int islower(arr[i])){
arr[i] = toupper(arr[i]);
}
}
printf("%s",arr);
return 0;
}
To measure the length of a string properly, use strlen, not sizeof
for(int i=0;i<strlen(arr);i++){ // Don't use sizeof on this line
Here's a simpler version:
#include <stdio.h>
#include <ctype.h>
int main(int argc, char *argv[]) {
char arr[100];
scanf("%s", arr);
for(int i=0;i<strlen(arr);i++){
arr[i] = toupper(arr[i]);
}
printf("%s",arr);
return 0;
}
Or even:
#include <stdio.h>
#include <ctype.h>
int main(void) {
char arr[100];
scanf("%s", arr);
for(char* c=arr; *c=toupper(*c); ++c) ;
printf("%s",arr);
return 0;
}
You are missing an include #include <ctype.h>
Also you don't need your if statement. toupper takes care of that internally (if you really want to keep islower remove the int in your if statement).
Add the header that declares islower and toupper.
#include <ctype.h>
In addiition,
if(int islower(arr[i])){
is not right. Remove the int.
if(islower(arr[i])){
whats wrong here?
The line: if(int islower(arr[i])){ fails compile for bad expression.
Change to: if(islower(arr[i])){
And in this line in your code may be looking beyond where it should:
for(int i=0;i<sizeof(arr);i++){
as you may be looking at space past the string terminator:
|s|t|r|i|n|g|\0|<unknown contents here, part of your legal memory, but are not part of the string>
it should be:
int len = strlen(arr);
for(int i=0;i<len;i++){

Characters not showing up [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'm making a fun little text-based game for fun and for some reason some text aren't showing up for the username.
Here's my code:
#include <stdio.h>
#include <stdlib.h>
/* Forward declarations -- Prototypes */
void askCharacterName(char *name);
void printMainMessage(char* name);
int main(char* username) {
askCharacterName(username);
char* temp;
temp = &username;
printMainMessage(temp);
return (0);
}
void askCharacterName(char *name) {
char username[20];
printf("What is your desired username?");
scanf("%s", &username);
return *username;
}
void printMainMessage(char *name) {
printf("Hello %s. Welcome to Lamescape!\n", name);
}
Here is my output:
Welcome []. Welcome to Lamescape!
A couple of things:
Your main function should have a different signature.
Consider allocating your memory at a higher level so it is available in lower
levels of the program.
When modifying buffers in c, always pass their size too.
Functions with void return values are not expected to return anything.
After fixing these problems your errors went away.
#include <stdio.h>
#include <stdlib.h>
/* Forward declarations -- Prototypes */
void askCharacterName(char *name, unsigned size);
void printMainMessage(char* name, unsigned size);
int main()
{
char namebuffer[100];
askCharacterName(namebuffer, 100);
printMainMessage(namebuffer, 100);
return 0;
}
void askCharacterName(char *name, unsigned size)
{
printf("What is your desired username?");
scanf("%s", name);
}
void printMainMessage(char *name, unsigned size)
{
printf("Hello %s. Welcome to Lamescape!\n", name);
}
Passing the size has no immediate effect here. I leave it up to you to figure out how to ensure that the buffer is never used beyond its bounds.
The username in askCharacterName has its memory content at the program stack.
void askCharacterName(char *name) {
char username[20];
printf("What is your desired username?");
scanf("%s", &username);
return *username;
}
Allocating the space in heap for username in main looks to be what you are looking for.

How to cast a void pointer to point more than one struct? [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 9 years ago.
Improve this question
Having Problems about how to access other structure membera with void pointer of other function??
typedef struct
{
char Buf[20];
char Str[20];
}Sample;
typedef struct
{
char Data[20];
int i;
} Test;
Void pointer structure
typedef struct
{
void *New;
int j;
} Datastruct;
int main()
{
//i am confused with first line
Datastruct->New = &Sample;
strcpy((( sample*)Datastruct->New )->Buf,"adam");
printf(" Datastruct->New->Buf");
Datastruct->New = &Test;
strcpy((( Test*)Datastruct->New)->Data,"Eve");
printf("Datastruct->New->Data");
return 0;
}
please let me know how to access members of other structures via void pointers
The compiler is also confused about first line; you can't take the address of a type. As for following void pointers, you've got the right idea: cast it to the type of pointer you wish to treat it as.
Here is a fixed version which actually compiles and works without errors:
#include <string.h>
#include <stdio.h>
typedef struct {
char Buf[20];
char Str[20];
} Sample;
typedef struct {
char Data[20];
int i;
} Test;
typedef struct {
void *New;
int j;
} Datastruct;
int main() {
Datastruct d;
Sample s;
d.New = &s;
strcpy(((Sample*)d.New )->Buf,"adam");
printf("Datastruct->New->Buf\n");
Test t;
d.New = &t;
strcpy(((Test*)d.New)->Data,"Eve");
printf("Datastruct->New->Data\n");
return 0;
}
In your original you were confusing -> with . and types (e.g. Datastruct) with variables of that type.

Resources