c-strings

I have a problem writing this program...
Can anyone help me this is the first time that I am doing something with c-strings
I am beginner in c programming

Thank you

Use c-strings for the following project:

Write a C++ program that repeatedly asks for and gets a string from the user and presents the following menu of operations on the string until user chooses to exit:

1. Search for a character in the string

2. Change a character within the string

3. Display the first n characters of the string

4. Display the last n character of the string

5. Display all characters that lie between two given indices

6. Null the string

7. Exit

If option 1 is selected, the program calls a function which takes the string and a character and returns either the index within the string of the first occurrence of the character or -1 if it could not be found. Print the location found or that it was not found along with how many times it was found.

If option 2 is selected, the program calls a function that takes a string, an integer for the position of the character to be changed and a character and changes the character located at the given position in the string to the given character. Keep looping until a valid value is entered for the position.

If option 3 is selected, a function is called which takes a string and an integer n and displays the first n characters of the string. Keep looping until a valid value is entered for the number of characters to be printed.

If option 4 is selected, a function is called which takes a string and an integer n and displays the last n characters of the string. Keep looping until a valid value is entered for the number of characters to be printed.

If option 5 is selected, a function is called which takes a string and two integers and displays all the characters that lie between the two indices, inclusive. Keep looping until a valid value is entered for both numbers.

If option 6 is selected, a function is invoked which takes a string and nulls the string meaning it makes it a 0-length string. If it already has 0 length, it must display a message saying that.
Last edited on
This is mostly loops.

C-string is an array of char elements, with value 0 in element marking the end of the string.
Yeah I know but cose I am new in c++ I am a little confused with all new commands in c-strings :(
I don't think you are supposed to use "commands in c-strings", because you can do it with more basic control constructs, and learn along the way.
i want to learn but I am doing this for a few days and still cant figure it :(

1
2
3
4
5
6
7
8
9
10
11
int countChar(char str[50], char c, int times, char find)
{
	find = strchr(str, c); //I have a problem here error C2440: '=' : cannot convert from 'char *' to 'char
	times = 0;
	while(str != '\0'){
		if(*str == c)
			times++;
		str++;
	}
	return times;
	return find;
Last edited on
Your project does not require counting.
Function can return only one value.

strchr() returns a pointer: http://www.cplusplus.com/reference/cstring/strchr/
You could use it to compute the index of first match for (option 1).

PS. Line 5 compares again a pointer and char. You should dereference the 'str', like on line 6.
I was looking at that link all day now and still nothing :(
it returns a pointer to the first occurrence in the string. It wouldn't make much sense to assign that to a character. That would almost be like doing find = c


Also you can not return multiple values like you are. If you wish to return multiple values you must return a container, have a reference output variable, or have a pointer output variable.
I was looking at that link all day now and still nothing :(

Look at it the other way. There is an example program that does use strchr.
Try to explain what it does.
Topic archived. No new replies allowed.