Computer science assignment. (strings)

In summary, The conversation discusses the creation of a program that prompts the user to enter a string and identifies the uppercase characters, replacing them with their lowercase counterparts. It also counts the number of uppercase letters entered. The code provided so far needs to be improved by properly initializing variables, using a loop to print out each character, and implementing a loop to count the uppercase letters.
  • #1
clook
35
0
i need a make a program that prompts the user to enter a string, and for it to identify the uppercase characters and replace each uppercase char with the corresponding lowercase character. it also needs to count the character

here's what i have so far:

Code:
#include<stdio.h>
#include<string.h>
main()
{

char
		letter[20];
	  
int uppercount,i;

	uppercount=0;
  		
	printf("Please enter characters:\n");
	gets(letter);

	if(letter[i]>= 'A' && letter[i] <='Z')      
    uppercount++;
	{
		letter[i] = letter[i] - 'A'+ 'a';
   
 }
	printf("%c\n", letter);
	   printf("The number of uppercase letters entered: %d", uppercount);
}

doesn't seem to work as i would like.. what changes should i make?
 
Physics news on Phys.org
  • #2
Initialize your variables and be aware the the letter in the printf will attempt to print out the address of the array as a character (you need to dereference your array by adding a and put it inside a loop since %c will print (I believe) out one character at a time). You also need some kind of loop to count the uppercase letters.
 
Last edited:
  • #3


Hello, thank you for sharing your code with me. It seems like you are on the right track, but there are a few changes that you can make to improve your program. Here are some suggestions:

1. Instead of using the "gets" function, which is known to be unsafe, you can use "fgets" to get input from the user. This function allows you to specify the maximum number of characters to be read, which can help prevent buffer overflow.

2. The "uppercount" variable should be initialized outside of the "if" statement, otherwise it will only count the uppercase letters in the first character entered.

3. In order to replace the uppercase characters with lowercase ones, you can use the "tolower" function from the <ctype.h> library. This function takes in a character as an argument and returns its lowercase equivalent.

4. You can use a "for" loop to iterate through the string and check each character for uppercase letters. This way, you can count the number of uppercase letters and replace them in one go.

Here is an updated version of your code with these changes:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main() {

char letter[20];
int uppercount = 0;

printf("Please enter characters:\n");
fgets(letter, 20, stdin);

for (int i = 0; i < strlen(letter); i++) {
if (isupper(letter)) { // checks if character is uppercase
uppercount++; // increments count
letter = tolower(letter); // replaces with lowercase equivalent
}
}

printf("The modified string is: %s\n", letter);
printf("The number of uppercase letters entered: %d\n", uppercount);

return 0;
}

I hope this helps! Keep up the good work on your computer science assignment.
 

1. What is a string in computer science?

A string is a data type used to represent a sequence of characters, such as letters, numbers, and symbols. It is often used to store and manipulate text in computer programs.

2. How do you declare a string in a computer science assignment?

In most programming languages, a string can be declared by enclosing the characters within single or double quotation marks. For example, "Hello, world!" or '12345'.

3. What are some common string manipulation operations in computer science?

Some common string manipulation operations include concatenation (joining two or more strings together), substring (extracting a portion of a string), and searching and replacing specific characters or words within a string.

4. How do you compare two strings in a computer science assignment?

In most programming languages, strings can be compared using the equality operator (==) or the greater than/less than operators (<, >). It is important to note that string comparison is case-sensitive, so "Hello" and "hello" would not be considered equal.

5. Can strings be modified in a computer science assignment?

Yes, strings can be modified through various string manipulation operations. However, it is important to remember that strings are immutable in some programming languages, meaning they cannot be changed once they are created. In these cases, new strings must be created in order to modify the existing one.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
874
  • Engineering and Comp Sci Homework Help
Replies
3
Views
661
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
918
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
Back
Top