Returning String and Char from a Function
The is my code which is working, however, I want to convert everything to function but having difficulties.
Code:
#include <stdio.h>#include <math.h>
#include <stdlib.h>
#define N 20
int main (void)
{
char v, u, str[N];
printf("insert the string element\n");
scanf("%s", str);
printf("insert a character from the element\n");
scanf(" %c", &v);
printf("\n");
printf("insert a character which you want to swap with\n");
scanf(" %c", &u);
printf("\n");
int i;
for (i=0; str[i]!='\0'; i++){
if (str[i] == v){
str[i] = u;
}
else if (str[i] == u){
str[i] = v;
}
else
continue;
}
printf("the new string is:\n");
puts(str);
return 0;
}
In this other code, I'm trying to implement function on the first line but can't do it.
Code:
#include <stdio.h>#include <math.h>
#include <stdlib.h>
#define N 20
char* stringP(char text[], char*);
int main (void)
{
char v, u, str[N];
str = ("insert the string element\n");
printf("insert a character from the element\n");
scanf(" %c", &v);
printf("\n");
printf("insert a character which you want to swap with\n");
scanf(" %c", &u);
printf("\n");
int i;
for (i=0; str[i]!='\0'; i++){
if (str[i] == v){
str[i] = u;
}
else if (str[i] == u){
str[i] = v;
}
else
continue;
}
printf("the new string is:\n");
puts(str);
return 0;
}
char* stringP(char text[], char news[N])
{
do{
printf("s", text);
scanf("s", &news);
}
while (news < N);
return news;
}
Please help. I want to have at least 4 functions in this code