So i want to replace a character that the user chooses and then choose what character that should be used instead. Its the first time i do something like this and its all new to me so if anyone has a good idea how to do i would be thankful. its a long code but what i am talking about basically starts from the "if(x == 6)"
What I have tried:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int x;
printf("Pick the program that should be executed:\n");
printf(" 1. Split text\n 2. Upper case to lower case\n 3. Lower case to upper case\n 4. Remove a character\n 5. Add a character\n 6. Replace a character\n 7. Statistics\n 8. Exit\n Enter an option:\n");
scanf("%d", &x);
if (x == 1) {
char str[100];
int i;
printf("Write the text that should be used:\n");
getchar();
fgets(str, 100, stdin);
printf("Input was: %s\n", str);
for (i = 0; str[i] != '\0'; i++)
{
printf("%c", str[i]);
printf("\n");
}
}
if (x == 2) {
char str[100];
int i;
printf("Write the text that should turn upper cases to lower cases: \n");
getchar();
fgets(str, 100, stdin);
printf("Input was: %s\n", str);
for (i = 0; str[i] != '\0'; i++)
if (str[i] >= 'A' && str[i] <= 'Z')
str[i] = str[i] + 32;
printf("string converted to lower case: %s", str);
getchar();
}
if (x == 3) {
char str[100];
int i;
printf("Write the text that should turn lower cases to upper cases\n");
getchar();
fgets(str, 100, stdin);
printf("Input was: %s\n", str);
for (i = 0; str[i] != '\0'; i++)
if (str[i] >= 'a' && str[i] <= 'z')
str[i] = str[i] - 32;
printf("string converted to upper case: %s\n", str);
getchar();
}
if (x == 4)
{
char str[100];
size_t i, j, len;
int r;
printf("Enter a sentence: \n");
fgets(str, 100, stdin);
if (fgets(str, 100, stdin) == NULL)
{
fprintf(stderr, ("fgets failed"));
return -1;
}
str[strcspn(str, "\n")] = 0;
printf("This is the sentence: %s\n", str);
printf("Enter character to remove: \n");
r = getchar();
if (r != EOF) {
len = strlen(str);
i = 0;
while (str[i] != '\0') {
if (str[i] == (char)r) {
for (j = i; j < len; j++) {
str[j] = str[j + 1];
}
len--;
}
else
i++;
}
}
printf("Sentence after removal: %s\n", str);
}
if (x == 5)
{
char str[100] = "";
size_t len;
int r;
printf("Enter a sentence: ");
fgets(str, 100, stdin);
if (fgets(str, 100, stdin) == NULL)
{
fputs("fgets failed\n", stderr);
return 1;
}
str[(len = strcspn(str, "\n"))] = 0; printf("This is the sentence: '%s' (len: %zu)\n", str, len);
if (len < 100 - 1)
{
printf("\nEnter character to add: ");
if ((r = getchar()) != EOF)
{
str[len++] = r;
str[len] = 0;
printf("This is the sentence: '%s' (len: %zu)\n", str, len);
}
else
fputs("(user canceled input.)\n", stderr);
}
else {
fputs("error: insufficient space to add char.\n", stderr);
return 1;
}
}
system("pause");
return main();
if (x == 6)
{
char str[100];
size_t len;
int r, i, j;
struct newlen
{
char str[50];
};
printf("Write the sentece that should be used\n");
fgets(str, 100, stdin);
if (fgets(str, 100, stdin) == NULL)
{
fputs("fgets failed\n", stderr);
return 1;
}
str[(len = strcspn(str, "\n"))] = 0;
printf("Choose the letter that should be replaced:\n");
r = getchar();
if (r != EOF) {
len = strlen(str);
i = 0;
while (str[i] != '\0')
{
if (str[i] == (char)r)
{
for (j = i; j < len; j++)
{
str[j] = str[j + 1];
}
newlen = newlen.replace(str[j + 1], r);
}
else
i++;
}
}
printf("Sentence after replacement: %s\n", str);
}
}