Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
#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)    ///split the text 
	{
		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)   //upper case to lower case
	{
		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;     //A - Z = 65 - 90, a - z = 97 - 122

		printf("string converted to lower case: %s", str);

		getchar();

	}

	if (x == 3)  //lower case to upper case
	{
		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 getchar returns EOF, no need to go through character removal logic*/
		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;    //save len 
		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);

	}
}
Posted
Updated 30-Dec-18 13:34pm
v2

There are a lot of problems with this code. The first one is, each of the code modules you have for the menu selections should be moved into their own separate function. Second, you should not recursively call main. The code in main should be made into a big loop that continues until exit is selected and then you can break from the loop and return a value. Third, as your code to replace the character is close but needs a little work. You don't have a replace method implemented in the newlen structure and if you aren't going to then you shouldn't declare a structure to contain just one member. If you are going to implement the replace method then you should not have the loop above it to eliminate the selected character. One of those two are not necessary.
 
Share this answer
 
v2
Comments
Member 14103987 31-Dec-18 1:48am    
shouldnt newlen = newlen.replace be enough for the program to know what to do? I get an error saying what i should have a struct for the newlen but for me that doesnt make any sence
Rick York 31-Dec-18 2:21am    
Is there a member named replace in that structure? I don't see one and it is not derived from anything so, no, that is not enough for the program to know what to do.
Member 14103987 31-Dec-18 2:35am    
I thought that just having a str inside the struct would work but i was obviously wrong

struct newlen
{
char str[50];
};

What im a missing though, should i have anything more then the size of str?
C++
system("pause");
return main(); // This line prevent

if (x == 6) // this line and following from ever be executed

Rule of thumb: main is a special name in your code and should never be called from anywhere in you code, it is a bad idea.
In your case, using a loop would be more appropriate.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900