Computer science assignment. (strings)

Click For Summary
SUMMARY

The forum discussion centers on a C program designed to prompt users for a string input, identify uppercase characters, replace them with their lowercase counterparts, and count the number of uppercase letters. The initial code provided by the user contains several issues, including the lack of variable initialization, improper use of the `printf` function, and absence of a loop to iterate through the string. Key corrections include initializing variables, using a loop to process each character, and correctly printing the modified string.

PREREQUISITES
  • Basic understanding of C programming syntax
  • Familiarity with string manipulation in C
  • Knowledge of loops and conditionals in programming
  • Experience with input/output functions in C, such as `printf` and `gets`
NEXT STEPS
  • Learn about C string handling functions, particularly `strlen` and `strcpy`
  • Research the use of `for` loops in C for iterating through arrays
  • Explore variable initialization best practices in C programming
  • Understand the implications of using `gets` and consider alternatives like `fgets` for safer input handling
USEFUL FOR

This discussion is beneficial for beginner C programmers, computer science students working on string manipulation assignments, and anyone looking to improve their understanding of character handling in C.

clook
Messages
32
Reaction score
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
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:

Similar threads

  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 21 ·
Replies
21
Views
4K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 21 ·
Replies
21
Views
5K