Reputation: 1
#include <stdio.h>
#include <string.h>
char changeincase(int, int, int);
int main() {
int size, index1, index2;
char string[1000], newstring[1000];
printf("enter the size of the string :");
scanf("%d", &size);
printf("Enter the string : ");
scanf("%s", string);
printf("Enter the first index :");
scanf("%d", &index1);
printf("Enter the second index :");
scanf("%d", &index2);
newstring[5] = changeincase(index1, index2 ,size);
printf("%s", newstring);
}
char changeincase(i, j, n) {
char str[1000];
if (str[i] >= 65 && str[i] <= 90) {
str[i] = str[i] + 32;
}
if (str[j] >= 65 && str[j] <= 90) {
str[j] = str[j] + 32;
}
}
The above code is supposed to convert the case of the string at a specified index but it is not working. It is generating some random characters every time I run the code. Please help me out.
Upvotes: 0
Views: 527
Reputation: 132
I see few mistakes in your code as also pointed out in comments in your questions. Here's my solution(It will inverter the case of the string in between the specified index i.e. lower to upper and upper to lower. Including the indexes.) Hopefully this is what you are looking for.
#include <stdio.h>
#include<string.h>
void changeincase(int ,int,int, char *);
int main()
{
int size,index1,index2;
printf("enter the size of the string :");
scanf("%d",&size);
char string[size+1];
printf("Enter the string : ");
scanf("%s",string);
printf("Enter the first index :");
scanf("%d",&index1);
printf("Enter the second index :");
scanf("%d",&index2);
changeincase(index1,index2 ,size, string);
printf("%s",string);
}
void changeincase(int startIndex,int endIndex,int n, char *str)
{
//char str[1000];
for(int i = startIndex; i<=endIndex; i++){
int ascii = (int)str[i];
if(ascii>=65&&ascii<=90){
str[i] = (char)(ascii+32);
}else if(ascii>=97&&ascii<=122){
str[i] = (char)(ascii-32);
}
}
}
Upvotes: -1
Reputation: 144949
The function changeincase
should take pointers to the source and destination strings as well as the boundary index values.
You should not use explicit character values such as 65
and 90
. Using 'A'
and 'Z'
is more readable and portable, and for real portability, you should use the functions from <ctype.h>
.
Here is a modified version:
#include <ctype.h>
#include <stdio.h>
void changeincase(char *dest, const char *src, int start, int end) {
int i;
for (i = 0; src[i]; i++) {
if (i >= start && i <= end) {
unsigned char c = src[i];
dest[i] = tolower(c);
} else {
dest[i] = src[i];
}
}
dest[i] = '\0'; set the null terminator
}
int main() {
int index1 = 0, index2 = 0;
char string[1000] = "", newstring[1000];
printf("Enter the string: ");
scanf("%999s", string);
printf("Enter the first index:");
scanf("%d", &index1);
printf("Enter the second index:");
scanf("%d", &index2);
changeincase(newstring, string, index1, index2);
printf("%s", newstring);
return 0;
}
Upvotes: 0
Reputation: 14157
You could invert case for a single character with a following function:
#include <ctype.h>
int invert_case(int c) {
if (islower(c)) return toupper(c);
else if (isupper(c)) return tolower(c);
else return c;
}
This code avoids using magic constants.
Another issue is updating a local variable str
. The changes are invisible outside the function. Just make str
an argument:
void changeincase(char* str, int i, int j) {
// char str[1000];
...
}
Upvotes: 3
Reputation: 419
In the function changeincase(i,j,n) you have defined an array of characters i.ei str[1000]. That array is neither initialized nor its values are defined by user but you are using it so it will automatically give you some garbage value. That's the problem.
Upvotes: 0