Reputation: 11
This is my first time writing in C and I have to write a code that does to upper on a string without actually using to upper.
I have done this however it only works up to 8 characters for some reason....
-----
Here is the input:
hello hello hello
hello
-----
Here is the output:
Copy: hello hello hello hello
;▒
The capitalized string is HELLO HEp▒▒▒
The original string is hello hello hello hello
#include <stdio.h>
int strLength(char* str){
int count;
for(count=0;str[count] != '\n';count++){}
return count;
}
char* copyStr(char* str,char* str2){
for(int i=0;str[i] != '\n';i++){
char n = str[i];
str2[i] = n;
}
str2[strLength(str)] = '\n';
return str2;
}
char* upper(char* str){
char str2[100];
for(int i=0;str[i] != '\n';i++){
int current = str[i];
if((current >= 97) && (current <= 122)){
char new = str[i];
str2[i] = new-32;
}
else{
str2[i] = current;
}
}
char* str3 = str2;
return str3;
}
int main(int argc, char **argv){
char input[100];
char inputcopy[100];
//get the input string
printf("Enter string: ");
fgets(input, 100, stdin);
copyStr(input,inputcopy);
printf("Copy: %s\n", inputcopy);
printf("The capitalized string is %s\n",upper(inputcopy));
printf("The original string is %s",input);
}
Upvotes: 1
Views: 56
Reputation: 126408
Obvious problems:
copyStr
upper
In C, you can't pass (as arguments) or return (as a return value) strings "directly" as they are not value types -- instead you must pass or return a pointer. Whenever you deal with pointers, you need to worry about the lifetime of the things pointed at, as if you use the pointer after the lifetime of the pointee has ended, you get undefined behavior.
So in order to "return" a string from a function, you must actually return a pointer to something that has a lifetime that extends after the function has returned (which means it can't be a pointer to a local var of the function). There are generally three way to arrange for that to happen:
copyStr
function)Each of these has its own drawbacks:
Upvotes: 2