![]() |
| | #1 |
| Registered User Join Date: May 2008
Posts: 4
| finding length of characters in an assigned char array ?? lets say that i have a multidimensional array defined as char names[1] [1] , the first column is to write the name of the person and the second column is to write the surname. After i write the name, this is where the question begins actually, for every consonant i must replace it by 0 and for every vowel i must put 1 , for example, if i have a name such as Jack, it must be 0100 , i've tried to write something on my own , but it didn't work my guess is that if i can find the number of the characters in the name entered, i can replace them by writing a for loop and a switch |
| charlatanksk is offline | |
| | #2 |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,475
| If you've got "char names[1] [1]" then you've already got a bug. That entire array holds as much as a single char, nothing more. You want two names so the first number has to be at least 2. You want names of more than zero characters long, so the second number has to be something sensible like 32.
__________________ My homepage Advice: Take only as directed - If symptoms persist, please see your debugger |
| iMalc is offline | |
| | #3 |
| Registered User Join Date: May 2008
Posts: 4
| sorry for that mistake, i fixed that mistake , so what needs to be done to find the length of the name that user entered? |
| charlatanksk is offline | |
| | #4 |
| CSharpener Join Date: Oct 2006
Posts: 5,242
| strlen function should help here
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
| | #5 |
| Registered User Join Date: May 2008
Posts: 4
| ok , i wrote a program but it doesn't work, i am sorry if there are terrible mistakes here, but i'm just a beginner what i want the program to do is to have names[0] as aaa and to print out 222 Code: #include <stdio.h>
#include <string.h>
#include <conio.h>
int main()
{
char names [1][20];
names[0]="aaa";
char amk=(char)names[0];
for(int i=0;i<=strlen(names[0]);i++){
switch(amk){
case 'a' :
printf("2");
break;
default:
printf("1");
break;
}
}
getch();
return 0;
}
|
| charlatanksk is offline | |
| | #6 |
| Registered User Join Date: Apr 2008
Posts: 278
| fixed version: Code: ...
char names [1][20]={{"aaa"}};
for(int i=0;i<strlen(names[0]);i++){
char amk=names[0][i];
switch(amk){
case 'a' :
printf("2");
break;
default:
printf("1");
break;
}
}
...
do it during its allocation or by using strcpy() (for example) after its allocation. Be careful with indexes [0..strlen()-1] instead of [0..strlen()]. At last, you must update the 'amk' value for each loop(char amk=names[0][i]). |
| root4 is offline | |
| | #7 |
| Registered User Join Date: May 2008
Posts: 4
| thank you for your help !! i know it went a little out of topic, but i got my answer so thank you |
| charlatanksk is offline | |
![]() |
| Tags |
| array, character, count, length, replace |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Another syntax error | caldeira | C Programming | 31 | 09-05-2008 01:01 AM |
| Sorting Linked Lists | DKING89 | C Programming | 6 | 04-09-2008 07:36 AM |
| get keyboard and mouse events | ratte | Linux Programming | 10 | 11-17-2007 05:42 PM |
| Program Crashing | Pressure | C Programming | 3 | 04-18-2005 10:28 PM |
| Character arrays | PsychoBrat | C++ Programming | 7 | 06-21-2002 12:02 PM |