Reputation: 267
I have the following code:
void toCapital(char name[], int size){
int i = 0;
char *wholeName = name;
for (i = 0; i < size ; i++){
wholeName[i] = toupper(wholeName[i]);
printf("%c", wholeName[i]);
}
}
main()
{
char miNombre[] = "Jason Martin Marx";
toCapital(miNombre, sizeof(miNombre));
}
And the output is:
JASON MA
This code takes a char array and converts all the strings inside into upper case. However, for some reason it stops halfway. Even if i increase the number of times to run the loop, it just adds gibberish at the end instead of the following letter.
If i was to edit the array into something like "Jason Martin Marx Jason Martin Marx" (doubling the string size) then it would print out the upper cased string once as "JASON MARTIN MARX" but not the second time.
Here is the whole code as requested:
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
void myName(char name[], int size){
int i;
for (i = 0; i < size -1; i++){
char currentLetter = name[i];
if (currentLetter == 'a' || currentLetter == 'e' || currentLetter == 'i' || currentLetter == 'o' || currentLetter == 'u' ||
currentLetter == 'A' || currentLetter == 'E' || currentLetter == 'I' || currentLetter == 'O' || currentLetter == 'U'){
printf("Character [%c] located at position %i is a vowel\n", currentLetter, i);
}
else if (currentLetter == ' '){
printf("Character [%c] located at position %i is a space\n", currentLetter, i);
}
else if (currentLetter == '$' || currentLetter == '%'){
printf("Character [%c] located at position %i is a symbol\n", currentLetter, i);
}
else{
printf("Character [%c] located at position %i is a consonant\n", currentLetter, i);
}
}
}
void pyramidA(char name[], int size){
int i;
char *wholeName = name;
int pointer = size-1;
char spaces[80] = "";
for (i = 0; i < (size / 2) ; i++){
printf("%i %s [%s] \n", pointer, spaces, wholeName);
wholeName++; ///erases first letter
wholeName[strlen(wholeName) - 1] = '\0'; /// erases last letter
pointer = pointer - 2;
strcat(spaces," ");
}
}
void toUpper(char name[], int size){
int i = 0;
char *wholeName = name;
printf("%s", wholeName);
for (i = 0; i < size ; i++){
wholeName[i] = toupper(wholeName[i]);
printf("%c", wholeName[i]);
}
}
main()
{
char miNombre[] = "Jason $ Martin % Marx ";
myName(miNombre, sizeof(miNombre));
printf("\n");
pyramidA(miNombre, sizeof(miNombre));
printf("\n");
toUpper(miNombre, sizeof(miNombre));
}
Upvotes: 0
Views: 251
Reputation: 42149
The problem is quite simply that you are modifying the string yourself. You probably think that assigning a pointer to another pointer creates a copy, but it doesn't:
char *wholeName = name;
// …
wholeName[strlen(wholeName) - 1] = '\0'; // <- modifies string in 'name'
If you want a temporary copy of name
in wholeName
, you must duplicate it, e.g.:
char *wholeName = malloc(size);
name = strcpy(wholeName, name);
// at the end of the function:
free(name);
It would be good style to check the return value of malloc
. Also, you must free
the same pointer that was returned by malloc
, and since you do wholeName++
inside the loop I recycled the name
pointer above to store the starting position. (The naming of the pointers is now quite misleading.)
Upvotes: 4
Reputation: 96976
The result may be compiler-dependent:
$ gcc -std=c99 -pedantic test.c
test.c: In function 'toCapital':
test.c:6:5: warning: implicit declaration of function 'toupper' [-Wimplicit-function-declaration]
test.c:7:5: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
test.c:7:5: warning: incompatible implicit declaration of built-in function 'printf' [enabled by default]
test.c: At top level:
test.c:11:1: warning: return type defaults to 'int' [enabled by default]
Here's a result from testing the resulting binary on my end:
bash-3.2$ ./a.out
JASON MARTIN MARX^@bash-3.2$
Other than the warnings, I think using sizeof()
on an array declared on the stack should be okay. I think your problem is elsewhere, but I don't know where.
EDIT
Now that we have all the code, your function pyramidA()
is manipulating the contents of miNombre
. That's why your string is truncated.
Use strcpy()
within the functions to work with a copy of the string miNombre
. This leaves miNombre
unchanged.
Upvotes: 0
Reputation: 689
Another thing you can do on gcc is create the output of the preprocessor by compiling with the -E option. There might be something going on that is not obvious by looking at the code before compiling. I ran your code and the output is not the same as your are getting. gcc -E foo.c -o foo.i. Edit foo.i and see if your code has been morphed by the preprocessor in any way.
================ output below ==================
JASON MARTIN MARX
Upvotes: 1