Thread: Looping problems!

  1. #1
    Registered User vars19's Avatar
    Join Date
    Jan 2012
    Location
    London
    Posts
    6

    Looping problems!

    I am trying to create part of a larger program however in this section i wish to count the individual chars from a string.

    I have tried for days and hours to come up with a solution however whenever i come close, the solution has some flaw in it somewhere.

    I have decided on using this layout and am now confused as to how force the program to keep checking in the 'checkAgainst' (innerMost) loop, finishing through the string and incrementing 'count' every time it returns true!. Then I will be able to reset count to 0 and continue on.

    There is however another problem and that is once a letter has been counted how am i supposed to make the program ignore this char. BUT that is another problem, I can most likely deal with that once I get there.

    Any tips or pointers in the right direction would be great!

    My code:

    Code:
    #include<stdio.h>
    #include<string.h>
    int main()
    {
    
    char word[100], temp[] = {'temp'}; int i=0, k=0, count=1; printf("Enter string: "); scanf("%s",word); printf("You entered: %s\n",word); printf("The length of your string is: %d characters\n", strlen(word)); for(i=0; i<strlen(word);i++) { //printf("I before loop is : %d\n",i);
    for (k=i+1;k<=strlen(word);k++)
    { //printf("I in loop is : %d\n:",i); //printf("K in loop is : %d\n", k);
    if(word[i] == word[k]) { printf("value of k: %d (letter)\n",i); printf("value of i: %d (compareTo)\n",k); count++; }
    printf("count is: %d\n",count); printf("2nd value of i: %d (letter)\n",i); printf("2nd value of k: %d (compareTo)\n",k); //printf("Character : %c occurs: 1 time.\n", word[i]); }
    printf("3rd value of i: %d (letter)\n",i); printf("3rd value of k: %d (compareTo)\n",k); count =1; //printf("character being looked at 3 is %c\n",word [i]); }
    return 0; }
    Last edited by vars19; 01-21-2012 at 05:14 PM. Reason: too messy

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Could you please explain more clearly what you are trying to accomplish?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User vars19's Avatar
    Join Date
    Jan 2012
    Location
    London
    Posts
    6
    yeah, sorry. I have a program which takes a string from the user. I have this string counted and a total char given.
    What i am stuck on is getting a number for each char. so

    a : 1
    b : 2
    c : 1 etc...

    the first for loop here creates 'i' which will point to the chars in my array, the second creates 'k' which i will use to compare 'i' against. I'm just not sure how to get my counter to count the results without exiting the loop.
    So i'd like the counter to keep counting until 'i' becomes a new value.

    so for i = array[0]; the program should check it against checkarray[1],[2],[3] etc before changing i to point to [1]. If you understand that!. the thing is though, i want the counter to keep incrementing until i changes, then i can print the counter value before re-setting it to check for the next letter.

    sorry if it's still unclear!

    p.s. array and checkArray just being clearer explanations for my array of 'word[]'
    Last edited by vars19; 01-21-2012 at 05:47 PM. Reason: added something

  4. #4
    Registered User vars19's Avatar
    Join Date
    Jan 2012
    Location
    London
    Posts
    6

    my revised code:

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main(){
    
    char word[100], temp[] = {'temp'};
    int i=0, k=0, count=1;
    printf("Enter string: ");
    scanf("%s",word);
    printf("You entered: %s\n",word); printf("The length of your string is: %d characters\n", strlen(word)); for(i=0; i<strlen(word);i++){
    k = i+1;
    for(k=i+1;k<=strlen(word);k++){
    if(word[i] == word[k]){
    count++;
    }
    }
    printf("Character : %c occurs: %d times.\n", word[i], count);
    //word[i] = '\0';
    count =1;
    }
    return 0;
    }
    I have taken out a few confusing parts of the code to ake it easier to understand, the code works however, If one of my chars has 2 values, the count is done but the print statement still shows the char e.g. for scanf _ ABBC
    A:1
    B:2
    B:1
    C:1

    I dont know how to get that second print to skip out the B. Itried making word[i] = null just before the count had been cleared but then it made all my values null!

  5. #5
    Registered User vars19's Avatar
    Join Date
    Jan 2012
    Location
    London
    Posts
    6
    Ah I figured it out, I will post the complete code after my assignment is handed in and marked just to prevent everyoen on my course copying me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with Looping
    By atlas07 in forum C++ Programming
    Replies: 24
    Last Post: 05-04-2010, 01:25 AM
  2. problems with nesting in looping
    By wobbles in forum C++ Programming
    Replies: 12
    Last Post: 03-29-2010, 02:30 AM
  3. Entry and looping problems
    By liukinhei in forum C Programming
    Replies: 1
    Last Post: 03-11-2008, 03:17 PM
  4. looping and condition problems
    By liukinhei in forum C Programming
    Replies: 1
    Last Post: 03-11-2008, 02:40 AM
  5. looping problems
    By purplepantsman in forum C++ Programming
    Replies: 4
    Last Post: 06-12-2003, 10:16 AM

Tags for this Thread